f#

Purpose of tick (apostrophe) in F# value names?

喜夏-厌秋 提交于 2020-03-21 11:31:10
问题 I'm going through a tutorial on Function Composition, and I keep seeing the ' operator used at the end of a value declaration. I know that it means a generic when it precedes a parameter, but what does it mean when you see it like: let add x y = x + y let myFunc' = add 10 The only thing I can see is that the ' is just another character in the identifier. Is that right? Because if I use that same example, using myFunc gives a not defined error, where myFunc' does resolve. 回答1: Yes, as @Lee

How to ignore the return value of an Async function in an Async block?

孤人 提交于 2020-03-02 07:59:50
问题 The m1 and m2 in the following functions have compiling errors. let m p = async { return p * 2 } let m1 () = async { do! m 2 } // ERR: was expected 'int' but here has type 'unit' let m2 () = async { do! m 2 |> ignore } // ERR: expecting 'Async<int>->Async<'a>' but given 'Async<int>->unit' m is called at the last line. How to ignore its return value? Is the following the only way (will executing of it be optimized by the compiler?)? let m1 () = async { let! x = m 2 () } 回答1: You can use Async

F# Reactive wait for Observable to complete

 ̄綄美尐妖づ 提交于 2020-03-01 05:41:26
问题 I found a variety of SO questions on this but couldn't figure out an F# solution. I need to block wait for an event to fire at me to check the data it returns. I am using Rx to receive event 3 times: let disposable = Observable.take 3 ackNack |> Observable.subscribe ( fun (sender, data) -> Console.WriteLine("{0}", data.AckNack) Assert.True(data.TotalAckCount > 0u) ) I would like to either turn results into a list, so they can be checked later on by the test framework (xUnit), or wait for all

F#, Pipe-forward first argument

寵の児 提交于 2020-02-29 12:20:31
问题 Quite similar to this question: F# pipe first parameter I am currently learning F# and functional programming, and I want to know if there is an easy way to pipe-forward a first argument (instead of last argument). For example, if I want to pipe forward the last argument, it looks very nice and clean: [4;5;6] |> List.append [1;2;3] // Result: [1;2;3;4;5;6] If I want to pipe-forward the first argument, I can use a "fun x ->" function, but I am just curious if there is a cleaner way. [1;2;3] |>

Purpose of a single case discriminated union

江枫思渺然 提交于 2020-02-29 10:20:06
问题 I'm defining a monadic observable/reactive parser. This behaves quite differently to a normal parser as it is a continuous query. The underlying type is: IObservable<'a> -> IObservable<'b> From looking at various parser implementations in functional languages, it seems as though the more appropriate way to define things is a single case discriminated union: type Pattern<'a,'b> = Pattern of (IObservable<'a> -> IObservable<'b>) Which means I then need to extract the underlying function to use

Define the cons (::) operator for custom collections

青春壹個敷衍的年華 提交于 2020-02-27 22:20:31
问题 I am using the fairly popular FSharpx.Collections package, and in particular the NonEmptyList type. This type provides the NonEmptyList.cons function, but I want to use the :: operator as with regular List , i.e. head :: tail . Since tail must already be a NonEmptyList<'a> , there shouldn't be any conflict with List 's :: operator. However, it seems I cannot define the operator. This: let ( :: ) h t = NonEmptyList.cons h t results in a compilation error: Unexpected symbol '::' in pattern.

F#: In real terms, what is the difference between a “string” and a “string option”?

北城余情 提交于 2020-02-27 02:47:17
问题 In real terms, what is the difference between a "string" and a "string option"? Aside from minor sytnax issues, the only difference I have seen is that you can pass a "null" to string while a string option expects a "none". 回答1: I don't particularly like the answer I've typed up below, because I think the reader will either see it as 'preaching to the choir' or as 'some complex nonsense', but I've decided to post it anyway, in case it invites fruitful comment-discussion. First off, it may be

Error F# - c# async calls : converting Threading.Tasks.Task<MyType> to Async<'a>

这一生的挚爱 提交于 2020-02-24 05:06:09
问题 When I try to call an async method that is in C# library from my F# code. I get the following compilation error. This expression was expected to have type Async<'a> but here has type Threading.Thread.Tasks.Task SendMessageAsync is in C# library and returns Threading.Thread.Tasks.Task<MyType> let sendEmailAsync message = async { let! response = client.SendMessageAsync(message) return response } 回答1: For converting between Task<'T> and Async<'T> there is a built-in Async.AwaitTask function. To

How to consume BlockingCollection<'a>.TryTake in F#

Deadly 提交于 2020-02-24 04:22:35
问题 How do I go about using the TryTake method on a BlockingCollection<'a> passing in a timeout period in milliseconds? Heres the signature: BlockingCollection.TryTake(item: byref, millisecondsTimeout: int) : bool is it possible to use the Tuple method of avoiding passing a ref type like on the Dictionary.TryGet methods? i.e. let success, item = myDictionary.TryGetValue(client) Im struggling with this particular signature, any suggestions would be great. Cheers! 回答1: I believe that you can only

How to directly invoke F# compiler on .NET Core?

谁说我不能喝 提交于 2020-02-24 03:25:30
问题 UPD.: I want to invoke F# compiler (i.e. fsc) from .NET Core SDK directly. I know about dotnet build & co, but I don't want to involve them when I only need to compile a simple problem, i.e. in cases when fsc file.fs would be enough. I've tried to search in .NET Core SDK (on my Mac, it was in /usr/local/share/dotnet/sdk/2.2.102/FSharp) and found a fsc.exe file there. Unfortunately, when I try to start it with dotnet /usr/local/share/dotnet/sdk/2.2.102/FSharp/fsc.exe, it gives me an error: