f#

F# and ADO.NET - idiomatic F#

為{幸葍}努か 提交于 2020-01-12 02:43:10
问题 I'm just starting to learn F#. I wrote this F#/ADO.NET code last night. In what ways would you improve the syntax - make it feel like idiomatic F#? let cn = new OleDbConnection(cnstr) let sql = "SELECT * FROM People" let da = new OleDbDataAdapter(new OleDbCommand(sql, cn)) let ds = new DataSet() cn.Open() let i = da.Fill(ds) let rowCol = ds.Tables.[0].Rows let rowCount = rowCol.Count printfn "%A" rowCount for i in 0 .. (rowCount - 1) do let row:DataRow = rowCol.[i] printfn "%A" row.["LastName

F# pattern match directly against let binding

倾然丶 夕夏残阳落幕 提交于 2020-01-11 10:04:34
问题 Is it possible in F# to pattern match directly against a let binding? For example, this compiles without any warnings: let value = match arg with | 1 -> "value1" | 2 -> "value2" | _ -> failwith "key not found" Whereas the following gives the warning "This rule will never be matched" against the lines matching key2 and _ : let key1 = 1 let key2 = 2 let value = match arg with | key1 -> "value1" | key2 -> "value2" | _ -> failwith "key not found" Is this because although they're immutable, the

Functions that look pure to callers but internally use mutation

ぐ巨炮叔叔 提交于 2020-01-11 08:27:09
问题 I just got my copy of Expert F# 2.0 and came across this statement, which somewhat surprised me: For example, when necessary, you can use side effects on private data structures allocated at the start of an algorithm and then discard these data structures before returning a result; the overall result is then effectively a side-effect-free function. One example of separation from the F# library is the library's implementation of List.map, which uses mutation internally; the writes occur on an

Asynchronous BinaryReader and BinaryWriter in .Net?

时光怂恿深爱的人放手 提交于 2020-01-11 05:01:04
问题 I'd like to read and write bytes and structured value types, asynchronously, without having to worry about decoders and byte-shifting: is there something out there that would allow me to do that? 回答1: It's not possible with BinaryReader or BinaryWriter . You could read concurrently from the underlying BaseStream , but the documentation states the following: Using the underlying stream while reading or while using the BinaryReader can cause data loss and corruption. For example, the same bytes

Asynchronous BinaryReader and BinaryWriter in .Net?

ⅰ亾dé卋堺 提交于 2020-01-11 05:01:04
问题 I'd like to read and write bytes and structured value types, asynchronously, without having to worry about decoders and byte-shifting: is there something out there that would allow me to do that? 回答1: It's not possible with BinaryReader or BinaryWriter . You could read concurrently from the underlying BaseStream , but the documentation states the following: Using the underlying stream while reading or while using the BinaryReader can cause data loss and corruption. For example, the same bytes

How to merge sorted sequences?

你。 提交于 2020-01-11 01:16:09
问题 Here’s a problem I’ve really been struggling with. I need to merge two sorted sequences into a single sorted sequence. Ideally, the algorithm should be lazy-evaluated, and not require caching more than one item from each sequence. This is not a terribly difficult problem to solve, and I’ve been able to engineer a number of solutions in F#. Unfortunately, every solution I’ve come up with has one of several problems. Recursive calls to subsequence generators using yield!. This produces elegant

Delegate/Func conversion and misleading compiler error message

心不动则不痛 提交于 2020-01-10 19:35:31
问题 I thought that conversions between F# functions and System.Func had to be done manually, but there appears to be a case where the compiler (sometimes) does it for you. And when it goes wrong the error message isn't accurate: module Foo = let dict = new System.Collections.Generic.Dictionary<string, System.Func<obj,obj>>() let f (x:obj) = x do // Question 1: why does this compile without explicit type conversion? dict.["foo"] <- fun (x:obj) -> x // Question 2: given that the above line compiles

F# Create custom attribute to expression

帅比萌擦擦* 提交于 2020-01-10 19:34:27
问题 In F#, how can I create a custom attribute to apply on expressions? I looked for resources everywhere, but I didn't find nothing. For exemple The attribute [<Entrypoint>] can be applied to some expression, and by consequence the compiler can infer that expression should be of type array string -> int . How can I create a custom attribute to work simillary ? 回答1: To create a custom attribute, simply declare a class that inherits from System.Attribute : type MyAttribute() = inherit System

F# Create custom attribute to expression

帅比萌擦擦* 提交于 2020-01-10 19:33:14
问题 In F#, how can I create a custom attribute to apply on expressions? I looked for resources everywhere, but I didn't find nothing. For exemple The attribute [<Entrypoint>] can be applied to some expression, and by consequence the compiler can infer that expression should be of type array string -> int . How can I create a custom attribute to work simillary ? 回答1: To create a custom attribute, simply declare a class that inherits from System.Attribute : type MyAttribute() = inherit System

Changing an immutable object F#

南笙酒味 提交于 2020-01-10 19:13:07
问题 I think the title of this is wrong but can't create a title that reflects, in the abstract, what I want to achieve. I am writing a function which calls a service and retrieves data as a JSON string. The function parses the string with a JSON type provider. Under certain conditions I want to amend properties on that JSON object and then return the string of the amended object. So if the response from the call was {"property1" : "value1","property2" : "value2", "property3": "value3" } I want to