f#

Agent/MailboxProcessor in C# using new async/await

孤街醉人 提交于 2019-12-08 23:15:08
问题 This question combines two topics I don't fully understand Reading through a paper about async in F#, I came across the topic of Agents/MailboxProcessors, which can be used to implement reactive state machines. Could the new async/await functionality in C#5 be used to implement something similar in C#, or is there already something analogue that would be better suited? 回答1: With a bit of pretty horrible hacking, you can use the MailboxProcessor type from C# using async . Some difficulties are

Best way to condense a list of option type down to only elements that are not none?

a 夏天 提交于 2019-12-08 22:52:28
问题 I'm unexpectedly having a bit of trouble with going from a list of 'a option down to a list containing only the elements that are Some. My initial attempt was: let ga = List.filter (fun xx -> match xx with | Some(g) -> true | None -> false) gao But of course, this result type is still 'a option list. I don't know how to use List.map to condense this, because you have to handle all cases in a match statement. I have an ugly solution, but I'm wondering if there is something better. Ugly: let

fscheck doesn't generate random enough data

巧了我就是萌 提交于 2019-12-08 22:13:07
问题 I'm playing with FsCheck so I have this implementation: let add a b = if a > 100 then failwith "nasty bug" else a + b ...and this FsCheck based test: fun (a:int) -> (add a 0) = a |> Check.QuickThrowOnFailure and the test never fails. My guess is that the 100 values produced by the random generator are never bigger than 100. Shouldn't the values be more "random"? 回答1: When you use Check.QuickThrowOnFailure , it uses the configuration Config.QuickThrowOnFailure , which has these values: >

Writing F# object expression in one single line

冷暖自知 提交于 2019-12-08 22:10:05
问题 As I was about to write a code generator for F#, I was wondering whether I could avoid stepping into the indentation mess by generating only one-line-values. As part of this effort, I was considering how I could express Object Expressions in one single line, but couldn't succeed, except in Verbose Mode. let Expr() = (let ToString = "ToString" in { new System.Object() with member this.ToString() = ToString; member this.GetHashCode() = ToString.GetHashCode()}) The issue is that I don't want to

Curious errors when combining inline with explicit member constraints

你说的曾经没有我的故事 提交于 2019-12-08 21:33:22
问题 (Updated: I have added a repro example) With code looking like the following: type Lib = static member inline tryMe (a: ^a) = let name = (^a: (static member name: string) ()) name type Test = struct val Value: string new v = {Value = v} end static member inline name with get() = "HiThere" static member works(a:Test) = Lib.tryMe a This will "just work" and compile. However, if you extend it a little bit, for instance like as follows: /// Does a bounds check and raises an error if bounds check

Can a Type Provider be passed into a function as a parameter

痞子三分冷 提交于 2019-12-08 21:09:04
问题 I am learning F# and the FSharp.Data library. I have a task which I need to read 20 CSV files. Each file has different number of columns but the records share the same nature: keyed on a date string and all the rest of the columns are float numbers. I need to do some statistical calculation on the float format data columns before persist the results into the database. Although I got all the plumbing logic working: read in the CSV via FSharp.Data CSV type provider, use reflection to get the

is an “optionalized” pipe operator idiomatic F#

不打扰是莪最后的温柔 提交于 2019-12-08 21:08:36
问题 I like to use the pipe operator '|>' a lot. However, when mixing functions that return 'simple' values with functions that return 'Option-Typed-values', things become a bit messy e.g.: // foo: int -> int*int // bar: int*int -> bool let f (x: string) = x |> int |> foo |> bar works, but it might throw a 'System.FormatException:...' Now assume I want to fix that by making the function 'int' give an optional result: let intOption x = match System.Int32.TryParse x with | (true, x) -> Some x |

No F# generics with constant “template arguments”?

浪尽此生 提交于 2019-12-08 20:40:47
问题 It just occurred to me, that F# generics do not seem to accept constant values as "template parameters". Suppose one wanted to create a type RangedInt such, that it behaves like an int but is guaranteed to only contain a sub-range of integer values. A possible approach could be a discriminated union, similar to: type RangedInt = | Valid of int | Invalid But this is not working either, as there is no "type specific storage of the range information". And 2 RangedInt instances should be of

Can F# be refactored into a pointfree style?

女生的网名这么多〃 提交于 2019-12-08 19:39:43
问题 In researching a topic related to programming I came across a pointfree refactoring tool for Haskell in the lambdabot and was wondering if F# can be refactored into a pointfree style? I am not advocating the use of pointfree style, but see it as a means to better comprehend a function. Note: pad answered an earlier version of this question, but I reworded this question as the answer is of value to others learning and using F# and I did not want this to be deleted because of some close votes.

How to tell if an F# function is pure?

可紊 提交于 2019-12-08 19:21:01
问题 Suppose I have these two F# functions: let sq x = x*x let tm = DateTime.Now Clearly sq is pure in that it will always return the same value for a given input while tm is impure because it will return a different value each time it is called. In general is there a way to determine if a particular function in F# is pure or impure without analyzing what it does, in other words reading it line by line? Alternatively is there a way to annotate a function to tell the compiler that the function is