f#

Return record option as null when calling from C#

别说谁变了你拦得住时间么 提交于 2019-12-10 09:33:16
问题 Is it ever possible to return record option value from F# to C# as null value? I want to encapsulate some logic in F# assembly, and hide as much as I can behind facade being "more natural to C#". Here's some synthetic example: type Data = { DataField1: int; DataField2: string } And code to return to C# would look like this: type SomeFacade() = let data = Some { DataField1 = 1; DataField2 = "hello" } member x.GetData() = if Option.isSome data then Option.get data else null But it is not

F# DataTable to SQL using SqlBulkCopy

社会主义新天地 提交于 2019-12-10 09:32:43
问题 I have an F# program that creates a DataTable , populates it with one row and then writes the data to SQL Server using bulk insert ( SqlBulkCopy ). Although it's working, I can't really figure out how to include a loop that will generate a number of list items / data rows which I can then insert in one statement, rather than having to bulk insert a single row at a time (which is the current case) here's my code: open System open System.Data open System.Data.SqlClient let lcpSqlConnection =

F# Core library source code has a flag for compiling tuples as structs but I can't make it work

妖精的绣舞 提交于 2019-12-10 08:51:55
问题 This is follow-up question to this proposal. Apparently, the F# Core library source has a flag that allows 2-tuples to be compiled as structs. See here. I did the following: Added a define TUPLE_STRUXT flag to FSharp.Core.fsproj and FSharp.Compiler.fsproj . Built with build.bat . Replaced contents of C:\Program Files (x86)\Microsoft SDKs\F#\3.1\Framework\v4.0 and C:\Program Files (x86)\Reference Assemblies\Microsoft\FSharp\.NETFramework\v4.0\4.3.1.0 with appropriate built files. My project

F# type functions and a [<GeneralizableValue>] attribute

大憨熊 提交于 2019-12-10 07:43:20
问题 What is the difference between this two F# type functions: let defaultInstance1<'a when 'a:(new: unit->'a)> = new 'a() [<GeneralizableValue>] let defaultInstance2<'a when 'a:(new: unit->'a)> = new 'a() 回答1: let defaultInstance1<'a when 'a:(new: unit->'a)> = new 'a() [<GeneralizableValue>] let defaultInstance2<'a when 'a:(new: unit->'a)> = new 'a() let x1 = defaultInstance1 // value restriction let x2 = defaultInstance2 回答2: Here is a good blog: http://blogs.msdn.com/b/mulambda/archive/2010/05

Parsing function application with FParsec using OperatorPrecedenceParser?

谁都会走 提交于 2019-12-10 04:19:22
问题 The question is similar to this one, but I want to parse an expression with function application using the OperatorPrecedenceParser in FParsec . Here is my AST: type Expression = | Float of float | Variable of VarIdentifier | BinaryOperation of Operator * Expression * Expression | FunctionCall of VarIdentifier (*fun name*) * Expression list (*arguments*) I have the following input: board→create_obstacle(4, 4, 450, 0, fric) And here is the parser code: let expr = (number |>> Float) <|> (ident

F# compiler and runtime version number confusion

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 04:05:05
问题 I am unable to figure out which version of the F# Core runtime library should be used in combination with the Mono platform (.NET 3.5). Installed with Microsoft .Net 4.5 come two versions of the FSharp Core runtime, 2.3.0.0 and 4.3.0.0. My guess is that the first one is for use in the .NET framework 2.0 and later and that the second one can only be used by .NET 4.0 or later. Is this assumption correct? Since FSharp is now open source and available on Github, I can check out a recent tag,

Tail recursion and exceptions in F#

∥☆過路亽.° 提交于 2019-12-10 03:50:41
问题 I've been googling for ages and still can't find the answer. From what I understand F# 3.0 runnning on .NET 4.5 will not use tail recursion for a recursive method if the invoker has wrapped the call in a try/catch and/or try/finally block. What is the situation if there is a try/catch or try/finally a few levels up the stack? 回答1: If you wrap the body of some (tail) recursive function in a try ... with block then the function is not tail recursive anymore, because the call frame cannot be

FSharp.Data.JsonProvider - Getting json from types

一笑奈何 提交于 2019-12-10 03:45:36
问题 The FSharp.Data.JsonProvider provides a means to go from json to an F# type. Is it possible to go in the reverse direction i.e. declare an instance of one of the types created by FSharp.Data.JsonProvider, set the field values to be what I need, and then get the equivalent json? I've tried things like this, type Simple = JsonProvider<""" { "name":"John", "age":94 } """> let fred = Simple( Age = 5, // no argument or settable property 'Age' Name = "Fred") 回答1: The latest version of F# Data now

How to eliminate time spent in JIT_TailCall for functions that are genuinely non-recursive

时间秒杀一切 提交于 2019-12-10 03:42:10
问题 I am writing a 64 bit F# solution and profiling has revealed a surprisingly & unexpectedly large amount of time spent in JIT_TailCall ...it is in fact dominating the runtime (circa 80%). This appears together with its evil cousin JIT_TailCallHelperStub_ReturnAddress . I have definitely traced the source to passing a struct type (custom value types) in a method or property call across an assembly boundary. I am certain of this because if I bypass the method call and assign my struct to the

Railway oriented programming with Async operations

て烟熏妆下的殇ゞ 提交于 2019-12-10 03:41:37
问题 Previously asked similar question but somehow I'm not finding my way out, attempting again with another example. The code as a starting point (a bit trimmed) is available at https://ideone.com/zkQcIU. (it has some issue recognizing Microsoft.FSharp.Core.Result type, not sure why) Essentially all operations have to be pipelined with the previous function feeding the result to the next one. The operations have to be async and they should return error to the caller in case an exception occurred.