f#

Correctly awaiting in F# an async C# method with return type of Task<T>

爷,独闯天下 提交于 2019-12-31 08:48:06
问题 I'd like to be able to consume a C# library from F#. Mostly this has been pretty straightforward. However, if I try to call a function that returns a Task<T> I am not able to get the returned value. So, I have C# method with the following definition: public async Task<TEvent> ReadEventAsync<TEvent>(string streamName, int position) where TEvent: class And I am trying to consume this method from F# as follows: let readEventFromEventStore<'a when 'a : not struct> (eventStore

F#: Is there a way to extend the monad keyword list?

北城余情 提交于 2019-12-31 08:11:40
问题 Inside an F# monad, if you say let! , the compiler translates that to a Bind member that you've defined on the monad builder. Now I see there are Query monads, as shown here on MSDN, where you can say: query { for student in db.Student do select student count } and the select and count , for example, will be translated to the QueryBuilder members Linq.QueryBuilder.Select and Linq.QueryBuilder.Count. My question is, is this mapping of keywords to members hardwired into the F# compiler, or is

Can I use different workflows simultaneously in F#?

独自空忆成欢 提交于 2019-12-31 04:37:08
问题 I need my state to be passed along while being able to chain functions with the maybe workflow. Is there a way for 2 workflows to share the same context? If no, what is the way of doing it? UPDATE: Well, I have a state that represents a segment of available ID's for the entities that I am going to create in the database. So once an ID is acquired the state has to be transformed to a newer state with the next available ID and thrown away so that nobody can use it again. I don't want to mutate

Unknown need for type annotation or cast

坚强是说给别人听的谎言 提交于 2019-12-31 03:26:26
问题 I know I must be missing something really obvious here. B.GetInstance().Call() generates the error: Lookup on object of indeterminate type based on information prior to this program point. A type annotation may be needed prior to this program point to constrain the type of the object. This may allow the lookup to be resolved. I'm using v1.9.9.9. type A() = member x.Call() = B.GetInstance().Call() and B() = static member GetInstance() = new B() member x.Call() = () I just discovered that this

FParsec: how to combine parsers so that they will be matched in arbitrary order

霸气de小男生 提交于 2019-12-31 03:19:25
问题 The task is find particular key-value pairs and parse them. The pairs can occur in any order. My partially working attempt: open FParsec type Parser<'a> = Parser<'a, unit> type Status = Running | Done type Job = { Id: int Status: Status Count: int } let ws = spaces let jobId: Parser<int> = ws >>. skipStringCI "Job id" >>. ws >>. skipChar '=' >>. ws >>. pint32 let status: Parser<Status> = ws >>. skipStringCI "Status" >>. ws >>. skipChar '=' >>. ws >>. ( (skipStringCI "Running" >>% Running) <|>

polymorphism with types for common fields

不羁岁月 提交于 2019-12-31 02:22:08
问题 Is this question solvable through functional idiomatic approach, could generics or discriminated unions be the answer? Is it possible to have polymorphism with passing different types to a function while the function is consuming some common fields. Idea is to be able to call and reuse the function with different types and use the common attributes/fields. type Car = { Registration: string Owner: string Wheels: int customAttribute1: string customAttribute2: string } type Truck = {

Is my rec function tail recursive?

橙三吉。 提交于 2019-12-31 02:17:07
问题 Is this function tail-recursive ? let rec rec_algo1 step J = if step = dSs then J else let a = Array.init (Array2D.length1 M) (fun i -> minby1J i M J) let argmin = a|> Array.minBy snd |> fst rec_algo1 (step+1) (argmin::J) In general, is there a way to formally check it ? Thanks. 回答1: This function is tail-recursive; I can tell by eyeballing it. In general it is not always easy to tell. Perhaps the most reliable/pragmatic thing is just to check it on a large input (and make sure you are

Weird None behaviour in type providers

喜欢而已 提交于 2019-12-31 01:52:26
问题 have a type provider with three properties 'a', 'b', and 'c' of type 'string', 'string option' and 'int option', respectively. When I have an instance with "", None, and Some 1 in those properties, this fails: (row1.a, row1.b, row1.c) |> should equal ("", None, Some 1) But all of these work fine: row1.a |> should equal "" row1.b |> should equal None row1.c |> should equal (Some 1) ("", None, Some 1) |> should equal ("", None, Some 1) How is this possible? What can make the None in b be

Generic type annotation in F#

喜欢而已 提交于 2019-12-31 01:50:18
问题 I got the following error: Error 2 Value restriction. The value 'gbmLikelihood' has been inferred to have generic type val gbmLikelihood : (float -> '_a -> float [] -> float) when '_a :> seq<float> Either make the arguments to 'gbmLikelihood' explicit or, if you do not intend for it to be generic, add a type annotation. and this type is exactly what I want. What do I have to do to make it work, and why doesn't it just work without intervention? EDIT: The error comes from this file (its short,

FsUnit `should equal` fails on `Some []`

て烟熏妆下的殇ゞ 提交于 2019-12-31 00:55:07
问题 When I run this FsUnit test with NUnit 2.6.3, let f xs = Some (List.map ((+) 2) xs) [<Test>] let test() = f [] |> should equal (Some []) I get: Result Message: Expected: <Some([])> But was: <Some([])> Result StackTrace: at FsUnit.TopLevelOperators.should[a,a](FSharpFunc`2 f, a x, Object y) The test fails even though the Expected and Actual in the message are the same. What happened? 回答1: The reason is that FsUnit uses untyped mechanism under the hood so Expected is inferred as object by the