f#

How do I add two numeric arrays in F#

末鹿安然 提交于 2019-12-10 15:42:54
问题 I am totally new to F#. I have searched high and low but I cannot find an example for what I want. let A = [| 1.0, 2.0, 3.0, 4.0 |];; //maybe delimiter with ; let B = [| 4.0, 3.5, 2.5, 0.5 |];; let C = A + B;; //how do I define the addition operator for arrays? // expect C=[| 5.0, 5.5, 5.5, 4.5 |] I have come close with this posting, but it is not what I want. 回答1: let inline (++) a b = Array.map2 (+) a b let A = [| 1.0; 2.0; 3.0; 4.0 |];; let B = [| 4.0; 3.5; 2.5; 0.5 |];; let A1 = [| 1; 2;

Accessing a namespace containing .base in its name from F#

梦想的初衷 提交于 2019-12-10 15:29:28
问题 As the title says, I'm trying to use a class declared in a namespace which contains "base" in its name. Think of a situation like the following: open Foo.base.Bar In C# I'd just use @ before base but F# seems to ignore that and to think that @ is the infix operator used for list concatenation. Since the namespace belongs to a third-party library which I cannot modify, is there a way I can still access it from F#? 回答1: In F#, you can achieve similar thing by enclosing the special name between

Binding values with list matching (without compiler warning)

半腔热情 提交于 2019-12-10 15:22:46
问题 Let's say I have a function that takes some int parameters, but inside which I will use float32 . I'd prefer not to use the float32 i function everywhere. Instead, I want to do this: let x = float32 x let y = float32 y let w = float32 w let h = float32 h To tighten it up a bit, I could do this: let x, y, w, h = float32 x, float32 y, float32 w, float32 h I'd like to do this: let [x;y;w;h] = List.map float32 [x;y;w;h] This works, but I get a compiler warning of Incomplete pattern matching on

extern access modifiers don't work

别等时光非礼了梦想. 提交于 2019-12-10 15:21:03
问题 I'm trying to hide my P/Invoke functions, like this one: [<DllImport("kernel32.dll", SetLastError=true)>] extern bool private CreateTimerQueueTimer(IntPtr& phNewTimer, nativeint TimerQueue, WaitOrTimerDelegate Callback, nativeint Parameter, uint32 DueTime, uint32 Period, ExecuteFlags Flags) Strangely, though, the private gets ignored -- which is really annoying, because I want to hide all the unwieldy structs and enums associated with these functions. I guess I could put everything in a

What is the best way of representing an immutable list in .NET?

天大地大妈咪最大 提交于 2019-12-10 15:19:57
问题 I've recently started using F# for "real work" and rediscovered the beauty of immutable data structures such as the discriminated unions and records in F#. I've also found them to be quite straight forward to use from C#, especially as they don't require any direct dependencies on the F# runtime. However, when it comes to representing lists in these structures, I have not yet found an ideal solution. My first attempt was to type the lists as seq<'a> (IEnumerable in the C# world) which

F# lazy eval from stream reader?

只谈情不闲聊 提交于 2019-12-10 15:18:45
问题 I'm running into a bug in my code that makes me think that I don't really understand some of the details about F# and lazy evaluation. I know that F# evaluates eagerly and therefore am somewhat perplexed by the following function: // Open a file, then read from it. Close the file. return the data. let getStringFromFile = File.OpenRead("c:\\eo\\raw.txt") |> fun s -> let r = new StreamReader(s) let data = r.ReadToEnd r.Close() s.Close() data When I call this in FSI: > let d = getStringFromFile(

Reasons not to use abstract class instead of interface?

不羁的心 提交于 2019-12-10 15:11:36
问题 I'm considering using an abstract class with all abstract members instead of an interface in order to avoid explicit interface implementation boiler-plate code. So instead of type IMyInterface = abstract Name : string abstract Text : string type MyClass() = member __.Name = "name" member __.Text = "text" interface IMyInterface with member this.Name = this.Name member this.Text = this.Text I'd have [<AbstractClass>] type MyAbstractClass() = abstract Name : string abstract Text : string type

.NET 4 SpinLock

爷,独闯天下 提交于 2019-12-10 15:10:03
问题 The following test code (F#) is not returning the result I'd expect: let safeCount() = let n = 1000000 let counter = ref 0 let spinlock = ref <| SpinLock(false) let run i0 i1 () = for i=i0 to i1-1 do let locked = ref false try (!spinlock).Enter locked if !locked then counter := !counter + 1 finally if !locked then (!spinlock).Exit() let thread = System.Threading.Thread(run 0 (n/2)) thread.Start() run (n/2) n () thread.Join() !counter I'd expect the SpinLock to mutually exclude the counter and

dynamically determine type of Option when it has value 'None'

筅森魡賤 提交于 2019-12-10 15:09:07
问题 There is some difficulty in getting Option type dynamically. Suppose I have a function: let printType x = if (box x) = null then printfn "the type is 'null'" else printfn "the type is %A" (x.GetType()) And we have the output here: printType 3 // the type is System.Int32 printType (Some(3)) // the type is Microsoft.FSharp.Core.FSharpOption`1[System.Int32] printType None // the type is null printType null // the type is null How to differentiate between None and null when getting an expression

How to Convert FSharpList back to List in c#?

泄露秘密 提交于 2019-12-10 14:58:45
问题 I have an F# library that returns an FSharpList to my C# caller. I would now like my C# caller's code to convert this into a List. What is the most efficient way to do this in C#? Thanks. 回答1: Easier than I thouglt... Starting with: List<double> niceList= new List<double>(); From List to FSharpList I did this: FSharpList<double> niceSharpList = ListModule.OfSeq(niceList); and to convert back from FSharpList to List I did: List<double> niceList= niceSharpList.ToList(); 回答2: To make this work