f#

Getting rid of the “pyramid of doom” in F#

别来无恙 提交于 2019-12-29 08:32:08
问题 I have several verbal expressions that I've packaged into one function: open FsVerbalExpressions open FsVerbalExpressions.VerbalExpression open System.Text.RegularExpressions open System let createOrVerbExFromList (verbExList: VerbEx list) = let orVerbEx = verbExList |> List.reduce (fun acc thing -> verbExOrVerbEx RegexOptions.IgnoreCase acc thing) //simpleVerbEx orVerbEx let k12VerbEx = let kTo12 = ["SCHOOL"; "DIST"; "SD"; "HS"; "BD OF ED"] kTo12 |> List.map (fun word -> VerbEx(word)) |>

“Chaining” asynchronous functions in F#

僤鯓⒐⒋嵵緔 提交于 2019-12-29 07:55:12
问题 I have created a function in F# to recover historical data from Yahoo (the classic asynchronous example for F#): let getCSV ticker dStart dEnd = async { let query = getFileUrl ticker dStart dEnd let req = WebRequest.Create(query) use! resp = req.AsyncGetResponse() use stream= resp.GetResponseStream() use reader = new StreamReader(stream) let content = reader.ReadToEnd() let ts = parseData content return ts } Now, I can run this function asynchronously by doing the following: let test= ["MSFT"

Use SqlDataReader in F#

依然范特西╮ 提交于 2019-12-29 07:44:32
问题 In C# I use sql scripts to add data into a List where T would be a class with fields/properties mapped to the sql script. How could I do this in F#? This piece uses a stored procedure in the standard way. using (conn) { conn.Open(); using (SqlCommand cmd = new SqlCommand("dbo.query_here", conn)) { cmd.CommandText = "dbo.query_here"; cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.CommandTimeout = 600; cmd.Parameters.Add(new SqlParameter("@x1", Convert.ToString(x))); cmd

Split seq in F#

守給你的承諾、 提交于 2019-12-29 07:37:06
问题 I should split seq<a> into seq<seq<a>> by an attribute of the elements. If this attribute equals by a given value it must be 'splitted' at that point. How can I do that in FSharp? It should be nice to pass a 'function' to it that returns a bool if must be splitted at that item or no. Sample: Input sequence: seq: {1,2,3,4,1,5,6,7,1,9} It should be splitted at every items when it equals 1, so the result should be: seq { seq{1,2,3,4} seq{1,5,6,7} seq{1,9} } 回答1: All you're really doing is

Example of the difference between List.fold and List.foldBack

爷,独闯天下 提交于 2019-12-29 07:27:30
问题 My understanding of the difference between List.fold and List.foldBack is that foldBack iterates over its list in a reverse order. Both functions accumulate a result from the items in the list. I'm having trouble coming up with a good example where it is preferable to foldBack over a list. In the examples I have come up with, the results are the same for both fold and foldBack, if the function logic does the same thing. [<Fact>] let ``List.foldBack accumulating a value from the right to the

Is it possible to mix .cs (C#) and .fs (F#) files in a single Visual Studio Windows Console Project? (.NET)

让人想犯罪 __ 提交于 2019-12-29 04:09:27
问题 How to do it? Is it possible to call a function from one F# code into C# without using a separated dll file or project? 回答1: You can't include two different languages in the same project, but you can merge them using ilmerge. To do this, put both projects in the same solution and reference the F# module as you would any dll. As part of your deployment script, run ilmerge to combine the exe file and dll file into a single exe file. See this Code Project article that details how to use ilmerge

in f# match statement how do I match to the type byte[]?

点点圈 提交于 2019-12-28 22:44:54
问题 I'm trying to lookup DbType enumeration values from .net types. I'm using a match statement. However I cannot figure out how to match on the type byte[]. let dbType x = match x with | :? Int64 -> DbType.Int64 | :? Byte[] -> DbType.Binary // this gives an error | _ -> DbType.Object If there is a better way to map these types, I would be open to suggestions. 回答1: byte[] , byte array , and array<byte> are all synonymous, but in this context only the last will work without parentheses: let dbType

How to access protected member

六眼飞鱼酱① 提交于 2019-12-28 15:49:48
问题 I have following code in extending type (in F#) which invokes a protected method of a class it inherits from (in C#) but I get the exception (see below). Is there a workaround for this? let getPagereference id = this.ConstructPageReference(id) The member or object constructor 'ConstructPageReference' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda

This expression should have type 'unit', but has type 'ConsoleKeyInfo'

蓝咒 提交于 2019-12-28 12:57:41
问题 I just wanted to pause in an F# console application, so I wrote: Console.ReadKey() But this gives the warning: This expression should have type 'unit', but has type 'ConsoleKeyInfo'. What can I do to fix this? 回答1: Solution: Console.ReadKey() |> ignore Explanation: Console.ReadKey() returns an object of type 'ConsoleKeyInfo' but you're using it as a statement without assigning the return value to anything. So F# warns you that you're ignoring a value. ignore takes any type and returns nothing

This expression should have type 'unit', but has type 'ConsoleKeyInfo'

試著忘記壹切 提交于 2019-12-28 12:56:51
问题 I just wanted to pause in an F# console application, so I wrote: Console.ReadKey() But this gives the warning: This expression should have type 'unit', but has type 'ConsoleKeyInfo'. What can I do to fix this? 回答1: Solution: Console.ReadKey() |> ignore Explanation: Console.ReadKey() returns an object of type 'ConsoleKeyInfo' but you're using it as a statement without assigning the return value to anything. So F# warns you that you're ignoring a value. ignore takes any type and returns nothing