f#

F# Constructor

*爱你&永不变心* 提交于 2020-01-03 11:58:08
问题 How do I do things in a constructor in F#? I cant quite figure it out... 回答1: I would check out Constructors (F#): Objects of class types have constructors. There are two kinds of constructors. One is the primary constructor, whose parameters appear in parentheses just after the type name. You specify other, optional additional constructors by using the new keyword. Any such additional constructors must call the primary constructor. 来源: https://stackoverflow.com/questions/984588/f-constructor

Variable length tuples in f#

穿精又带淫゛_ 提交于 2020-01-03 11:37:10
问题 Is it possible to write a function to accept a tuple of variable length? I'm trying to write a method that can be called like this: let a = sum(1,2) let b = sum(1,2,3) EDIT: Could it be interpreted as a function call with params? Or would the method need to be written in c#: double sum(params object[] double) { ... } 回答1: No - tuples are by definition not variable length, and to write a function like this you'd need something like template metaprogramming in C++ - and there isn't such a thing

Testing F# async workflows with xUnit.net's Task support

怎甘沉沦 提交于 2020-01-03 11:04:16
问题 I'm writing F# code and tests in xUnit 1.9. For normal sync stuff, I simply return unit and all is well; but now, I'm migrating the sync innards to be async workflows. In other words, my simple AAA is getting explicit Async usage pushed out into it as I refactor the system: let [<Fact>] ``Can consume using NEventStore InMemory`` () = let store = NesGateway.createInMemory () let finalDirection = playCircuit store |> Async.RunSynchronously // <----- YUCK test <@ CounterClockWise =

Testing F# async workflows with xUnit.net's Task support

試著忘記壹切 提交于 2020-01-03 11:01:53
问题 I'm writing F# code and tests in xUnit 1.9. For normal sync stuff, I simply return unit and all is well; but now, I'm migrating the sync innards to be async workflows. In other words, my simple AAA is getting explicit Async usage pushed out into it as I refactor the system: let [<Fact>] ``Can consume using NEventStore InMemory`` () = let store = NesGateway.createInMemory () let finalDirection = playCircuit store |> Async.RunSynchronously // <----- YUCK test <@ CounterClockWise =

How to approach writing an F# type provider that enforces complex schema?

守給你的承諾、 提交于 2020-01-03 10:17:09
问题 Just recently I worked with some data for traffic and travel information, that is data in Datex2 format. The project wasn't long and is over now, and I went on as usually and generated a bunch of strongly typed C# classes with xsd.exe tool, did some serializing, light processing and so forth. However, now in hindsight I became to wonder if this would have been a good case for an F# type provider and so to take my first stab on this subject. With this in mind, how should one approach a

Embedding F# Interactive Example Throwing Exception from FSharp.Compiler.Service

扶醉桌前 提交于 2020-01-03 09:59:49
问题 I am working through the Embedding F# Interactive example from http://fsharp.github.io/FSharp.Compiler.Service/interactive.html but am having an issue with the following line throwing an exception: let fsiSession = FsiEvaluationSession.Create(fsiConfig, allArgs, inStream, outStream, errStream) The exception thrown is: "An unhandled exception of type 'System.Exception' occurred in FSharp.Compiler.Service.dll Additional information: Error creating evaluation session: StopProcessing null" My

Embedding F# Interactive Example Throwing Exception from FSharp.Compiler.Service

孤街浪徒 提交于 2020-01-03 09:58:07
问题 I am working through the Embedding F# Interactive example from http://fsharp.github.io/FSharp.Compiler.Service/interactive.html but am having an issue with the following line throwing an exception: let fsiSession = FsiEvaluationSession.Create(fsiConfig, allArgs, inStream, outStream, errStream) The exception thrown is: "An unhandled exception of type 'System.Exception' occurred in FSharp.Compiler.Service.dll Additional information: Error creating evaluation session: StopProcessing null" My

F# - The type int is not compatible with type unit

落花浮王杯 提交于 2020-01-03 09:00:10
问题 Quite new to functional languages, but I'm maintaining someone else's code with a lot of F#. Can anyone offer some insight into this? let mtvCap = Rendering.MTViewerCapture(mtViewer) mtvCap.GetCapture() mtvCap.ToWpfImage() grid.Children.Add(mtvCap.ImageElement) MTViewer.ImageViewer is of type System.Windows.Controls.Image, and grid is System.Windows.Controls.Grid. Again, error is: The type int is not compatible with type unit 回答1: F# does not allow for you to silently ignore return values.

F# Code Execution Order

夙愿已清 提交于 2020-01-03 08:34:13
问题 another noob question regarding F#. If I have the following code... let ExeC = printfn "c" 3 let ExeB b = printfn "b" 2 let ExeA = printfn "a" 1 printfn "Example %d " ExeA printfn "Example %d " (ExeB 1) printfn "Example %d " ExeC The output is as follows... c a Example 1 b Example 2 Example 3 What seems unusual here is the order that the code is executing in. In a previous question Brian mentioned something about expressions, I was hoping someone could explain this a bit more. It almost seems

What is the most elegant way of bubble-sorting in F#?

冷暖自知 提交于 2020-01-03 08:25:20
问题 What is the most elegant way of bubble-sorting in F#? UPDATE As pointed out in one of the answers, bubble sorting isn't efficient in a functional language to begin with. A humourously-cynical commenter also pointed out that bubble sorting is only appropriate when the list is small and it's almost sorted anyway. However, I'm curious to see how a clever bubble-sort can be written in F#, since I've done bubble sorts in C#, C++, and Java EE in the past, and since I'm an F# newbie. 回答1: using