f#

Can I explicitly check for cancellation / terminate async computation?

穿精又带淫゛_ 提交于 2019-12-19 03:16:24
问题 I have an async computation like the following (see inline comments): async { //... do! Async.Sleep(100) //cancellation may happen during sleep //... but isn't checked at the end of the sleep, so regular, non-async computations are executed here } In order to force a cancellation check / terminate the entire async computation before the "regular" computation part is reached, I insert an effective no-op do! Async.Sleep(1) immediately after the do! Async.Sleep(100) . Is there a cleaner way to

In F#, is it possible to have a tryParse function that infers the target type

落花浮王杯 提交于 2019-12-19 03:16:13
问题 Presently we do this... let parseDate defaultVal text = match DateTime.TryParse s with | true, d -> d | _ -> defaultVal Is it possible to do this... let d : DateTime = tryParse DateTime.MinValue "2015.05.01" 回答1: Yes. Welcome to the world of member constraints, ref, and byref values. let inline tryParseWithDefault defaultVal text : ^a when ^a : (static member TryParse : string * ^a byref -> bool) = let r = ref defaultVal if (^a : (static member TryParse: string * ^a byref -> bool) (text, &r

Is it possible to make a field of a record private? or to make a member of record private?

早过忘川 提交于 2019-12-19 02:05:09
问题 I want to stick with record , and don't want to go back to object . So I am wondering if it is possible to make a field of a record private ? or to make a private member of record . what about other concrete types such as discriminated union ? Or, does this requirement violate the language spec? 回答1: Nope, it's not possible for single fields to be private: http://msdn.microsoft.com/en-us/library/dd233184 However, you can make all fields private and expose selected fields through properties.

How to create .NET-compatible events in F#?

人走茶凉 提交于 2019-12-18 21:55:10
问题 I am trying to publish an event from an F# type, but I want it to be seen as an event from C# or VB. It seems that the correct way to do it used to be IEvent.create_HandlerEvent, but this function doesn't exist in the newest version of F#. So what is the correct way to do it now? 回答1: Events are not my forte, but this example seems to work on F# 1.9.6.16: namespace EventExample open System type MyEventArgs(msg:string) = inherit EventArgs() member this.Message = msg type MyEventDelegate =

C# async / await method to F#?

若如初见. 提交于 2019-12-18 19:42:29
问题 I am trying to learn F# and am in the process of converting some C# code to F#. I have the following C# method: public async Task<Foo> GetFooAsync(byte[] content) { using (var stream = new MemoryStream(content)) { return await bar.GetFooAsync(stream); } } Where bar is some private field and GetFooAsync returns a Task<Foo> . How does this translate to F#? Here is what I currently have: member public this.GetFooAsync (content : byte[]) = use stream = new MemoryStream(content) this.bar

Small difference in types

女生的网名这么多〃 提交于 2019-12-18 19:22:09
问题 I have three functions that ought to be equal: let add1 x = x + 1 let add2 = (+) 1 let add3 = (fun x -> x + 1) Why do the types of these methods differ? add1 and add3 are int -> int , but add2 is (int -> int) . They all work as expected, I am just curious as to why FSI presents them differently? 回答1: This is typically an unimportant distinction, but if you're really curious, see the Arity Conformance for Values section of the F# spec. My quick summary would be that (int -> int) is a superset

Small difference in types

假装没事ソ 提交于 2019-12-18 19:22:09
问题 I have three functions that ought to be equal: let add1 x = x + 1 let add2 = (+) 1 let add3 = (fun x -> x + 1) Why do the types of these methods differ? add1 and add3 are int -> int , but add2 is (int -> int) . They all work as expected, I am just curious as to why FSI presents them differently? 回答1: This is typically an unimportant distinction, but if you're really curious, see the Arity Conformance for Values section of the F# spec. My quick summary would be that (int -> int) is a superset

Small difference in types

放肆的年华 提交于 2019-12-18 19:21:59
问题 I have three functions that ought to be equal: let add1 x = x + 1 let add2 = (+) 1 let add3 = (fun x -> x + 1) Why do the types of these methods differ? add1 and add3 are int -> int , but add2 is (int -> int) . They all work as expected, I am just curious as to why FSI presents them differently? 回答1: This is typically an unimportant distinction, but if you're really curious, see the Arity Conformance for Values section of the F# spec. My quick summary would be that (int -> int) is a superset

how to implement F#'s forward pipe operator in R? [duplicate]

本小妞迷上赌 提交于 2019-12-18 19:10:12
问题 This question already has answers here : Is it possible to get F#'s function application “|>” operator in R? [duplicate] (2 answers) Closed 4 years ago . How can you implement F#'s forward pipe operator in R? The operator makes it possible to easily chain a sequence of calculations. For example, when you have an input data and want to call functions foo and bar in sequence, you can write: data |> foo |> bar Instead of writing bar(foo(data)) . The benefits are that you avoid some parentheses

f# pattern matching with types

ぃ、小莉子 提交于 2019-12-18 19:08:24
问题 I'm trying to recursively print out all an objects properties and sub-type properties etc. My object model is as follows... type suggestedFooWidget = { value: float ; hasIncreasedSinceLastPeriod: bool ; } type firmIdentifier = { firmId: int ; firmName: string ; } type authorIdentifier = { authorId: int ; authorName: string ; firm: firmIdentifier ; } type denormalizedSuggestedFooWidgets = { id: int ; ticker: string ; direction: string ; author: authorIdentifier ; totalAbsoluteWidget: