f#

Defining a Message Passing domain with very many message types

佐手、 提交于 2021-02-07 18:05:59
问题 Most F# Message Passing examples I've seen so far are working with 2-4 message types, and are able to utilize pattern matching to direct each message to its proper handler function. For my application, I need hundreds of unique message types due to the different nature of their handling and required parameters. So far, each message type is its own record type with a marker interface attached, because including hundreds of types in a single discriminated union would not be very pretty - and

F#: How to apply Json.NET [JsonConstructor] attribute to primary constructor?

不羁的心 提交于 2021-02-07 14:36:47
问题 I'm trying to do something in F# like the JsonConstructorAttribute example in the Json.NET documentation: public class User { public string UserName { get; private set; } public bool Enabled { get; private set; } public User() { } [JsonConstructor] public User(string userName, bool enabled) { UserName = userName; Enabled = enabled; } } The analog in F# appears to be something like the following, but I'm getting an error on the placement of [<JsonConstructor>] : [<JsonConstructor>] // ERROR!

F#: How to apply Json.NET [JsonConstructor] attribute to primary constructor?

余生长醉 提交于 2021-02-07 14:34:31
问题 I'm trying to do something in F# like the JsonConstructorAttribute example in the Json.NET documentation: public class User { public string UserName { get; private set; } public bool Enabled { get; private set; } public User() { } [JsonConstructor] public User(string userName, bool enabled) { UserName = userName; Enabled = enabled; } } The analog in F# appears to be something like the following, but I'm getting an error on the placement of [<JsonConstructor>] : [<JsonConstructor>] // ERROR!

Is the “expression problem” solvable in F#?

僤鯓⒐⒋嵵緔 提交于 2021-02-07 12:26:16
问题 I've been watching an interesting video in which type classes in Haskell are used to solve the so-called "expression problem". About 15 minutes in, it shows how type classes can be used to "open up" a datatype based on a discriminated union for extension -- additional discriminators can be added separately without modifying / rebuilding the original definition. I know type classes aren't available in F#, but is there a way using other language features to achieve this kind of extensibility?

Is it possible to implement the IDbSet<T> interface in F#?

霸气de小男生 提交于 2021-02-07 12:08:27
问题 I am attempting to make a mock implementation of IDbSet<T>, and I happen to be doing it in F#. type MockDbSet<'T when 'T : not struct>(items:seq<'T>) = let collection = ResizeArray(items) new () = MockDbSet(Seq.empty) interface IDbSet<'T> with member x.Add entity = collection.Add entity; entity member x.Attach entity = collection.Add entity; entity member x.Remove entity = collection.Remove entity |> ignore; entity member x.Create() = Unchecked.defaultof<'T> member x.Create<'TD when 'TD : not

Is it possible to implement the IDbSet<T> interface in F#?

心不动则不痛 提交于 2021-02-07 12:08:16
问题 I am attempting to make a mock implementation of IDbSet<T>, and I happen to be doing it in F#. type MockDbSet<'T when 'T : not struct>(items:seq<'T>) = let collection = ResizeArray(items) new () = MockDbSet(Seq.empty) interface IDbSet<'T> with member x.Add entity = collection.Add entity; entity member x.Attach entity = collection.Add entity; entity member x.Remove entity = collection.Remove entity |> ignore; entity member x.Create() = Unchecked.defaultof<'T> member x.Create<'TD when 'TD : not

Is it possible to implement the IDbSet<T> interface in F#?

喜欢而已 提交于 2021-02-07 12:04:21
问题 I am attempting to make a mock implementation of IDbSet<T>, and I happen to be doing it in F#. type MockDbSet<'T when 'T : not struct>(items:seq<'T>) = let collection = ResizeArray(items) new () = MockDbSet(Seq.empty) interface IDbSet<'T> with member x.Add entity = collection.Add entity; entity member x.Attach entity = collection.Add entity; entity member x.Remove entity = collection.Remove entity |> ignore; entity member x.Create() = Unchecked.defaultof<'T> member x.Create<'TD when 'TD : not

Elegant pattern matching on nested tuples of arbitrary length

折月煮酒 提交于 2021-02-07 11:46:54
问题 I'm developing a composable functional UI library in F#, and I ran into a situation where I need to be able to create "collections" of items of heterogenous types. I don't want to accomplish this by resorting to dynamic programming and casting everything to obj (technically possible here, esp. since I'm compiling with Fable). Instead I want to retain as much type safety as possible. The solution I came up with is to create a simple custom operator %%% that builds tuples and then use it as

F# sequence comparison

点点圈 提交于 2021-02-07 11:42:07
问题 I have implemented a Fibonacci Sequence generator as follows let getNext upperLimit current= let (e1, e2) = current let next = e1 + e2 if next > upperLimit then None else Some (next, (e2,next)) let fib upperLimit = (0,1) |> Seq.unfold (getNext upperLimit) |> Seq.append [0;1] and my test code is [<Test>] member Spec.``fib not exeeding 20 should be 0,1,1,2,3,5,8,13``()= let expected = seq [0;1;1;2;3;5;8;13] let result = fib 20 let expectedSameAsResult = (expected = result) printfn "Expected: %A

AsyncResult and handling rollback

♀尐吖头ヾ 提交于 2021-01-29 08:49:44
问题 (warning: this was posted also on https://forums.fsharp.org/t/asyncresult-and-handling-rollback/928) Trying to implement 2PC Transaction Like workflow in F# (see http://learnmongodbthehardway.com/article/transactions/) and hitting an issue with computation expressions (asyncResult for example) and rollback. If you have the following pseudocode: let rollbackWorkflow parX parY = … here calling rollbackService1 and rollbackService2 let executeWorkflow par1 par2 par3 = asyncResult { let! result1