f#-3.0

Create Discriminated Union Case from String

僤鯓⒐⒋嵵緔 提交于 2019-12-17 16:28:16
问题 I'm trying to create DU cases from strings. The only way I can see doing this is by enumerating over the DU cases via Microsoft.FSharp.Reflection.FSharpType.GetUnionCases and then picking the UnionCase that matches the string (by using .Name ) and then making the actual DU case out of that by using FSharpValue.MakeUnion . Isn't there an easier/more elegant way of doing this? In my scenario I have a DU with a couple of hundred cases for keywords. I have to read the strings (keywords) from a

How to run code in parallel?

前提是你 提交于 2019-12-14 03:47:43
问题 How can i run these two independent loops simultaneously in parallel. let a1=Seq.map2 (fun a b->(1.0-a)/(a-b)) a2 a3 let b1=Seq.map2 (fun a b->a*b) b2 b3 回答1: You can use standard .NET tasks for this - there are no special F# functions or special syntax for spawning a computation in the background: let a1Work = Task.Factory.StartNew(fun () -> Array.map2 (fun a b->(1.0-a)/(a-b)) a2 a3) let b1 = Array.map2 (fun a b->a*b) b2 b3 let a1 = a1Work.Value I also changed your Seq.map2 to Array.map2 -

Reading value from XML file Throws an error - FAKE F#MAKE

左心房为你撑大大i 提交于 2019-12-13 17:21:01
问题 I am trying to read value from XML file from FAKE , But I am getting an error i.e. .fsx(9,16) : eror FS000: Incomplete structurd construct at or before this point in expression. Expected '->' or other token. Below is my code , I am using XMLHelper.XMLRead to read values from xml file . #r "./packages/FAKE/tools/FakeLib.dll" open Fake open Fake.XMLHelper Target "BuildMain" (fun _ -> for s in XMLHelper.XMLRead true "D:/test/Version.Config" "/version/major/minor" trace s) "BuildMain"

F# and C# Web Service cannot open namespace

梦想与她 提交于 2019-12-13 06:03:06
问题 I want to use the VS2012 F# and C# Web Service template by Daniel Mohl. However, I run into a frustrating issue of not being able to use the generated code in a script file: namespace FSharpWcfServiceApplicationTemplate open System open FSharpWcfServiceApplicationTemplate.Contracts type Service1() = interface IService1 with member x.GetData value = sprintf "%A" value member x.GetDataUsingDataContract composite = match composite.BoolValue with | true -> composite.StringValue <- sprintf "%A%A"

Is there an easy implementation to use a predicate function in a generic F# query

若如初见. 提交于 2019-12-12 16:07:40
问题 I want to use a generic query function like: let filter predicateFn = predicateFn |> fun f s -> query { for x in s do where (f(x)) select x } Where f should be a predicate function. Obviously, this cannot be translated to a sql query. But is there a solution for this problem. I have seen sample solutions for C#. Approach taken from: Web, Cloud & Mobile Solutions with F# by Daniel Mohl. Edit: example to clarify: let userSorter = fun (u:users) -> u.Login let sorted = query { for user in dbConn

Emitting Generated Types in F# type providers

谁说我不能喝 提交于 2019-12-12 14:00:27
问题 I have created a simple generating type provider that takes the path to an assembly at reorganizes the types, to bring them under the type providers namespace, (sort of Internalising if you like). The link to the code concerned is here https://github.com/colinbull/Playground Now the types seem to be provided correctly, let[<Literal>]assemblyPath = @"D:\Appdev\Playground\SimpleLib\bin\Debug\SimpleLib.dll" type T = Inno.InternalisingProvider<assemblyPath> type C = T.Class1 [<EntryPoint>] let

error FS0193: internal error: Could not load type 'anyType' from assembly 'FSI-ASSEMBLY, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'

回眸只為那壹抹淺笑 提交于 2019-12-12 00:24:17
问题 When Using Resizable array with any types i can't loop through that array , f# seems not to understand my preDefined Type . here is a sample code : type someData = { Entry:string ; id:int } let datas = new ResizeArray<someData>() let record1 = {someData.Entry = "hiLo" ;someData.id =1234 } datas.Add(record1) let record2 = {someData.Entry = "Lolo" ;someData.id =1224 } datas.Add(record2) let record3 = {someData.Entry = "Hihi" ;someData.id =1231 } datas.Add(record3) let nameOnly = new ResizeArray

F# match pattern for Expr<int>

点点圈 提交于 2019-12-11 09:40:08
问题 I try to find the correct pattern to match and run an Expr<int> using the below code: open System.Linq open Microsoft.FSharp.Quotations open Microsoft.FSharp.Quotations.Patterns let runSelectQuery (q:Expr<IQueryable<'T>>) = match q with | Application(Lambda(builder, Call(Some builder2, miRun, [Quote body])), queryObj) -> query.Run(Expr.Cast<Microsoft.FSharp.Linq.QuerySource<'T, IQueryable>>(body)) | _ -> failwith "Cannot run this query %s" (q.ToString()) let runCountQuery (q:Expr<int>) =

Cannot open namespace in Fsharp script file

浪尽此生 提交于 2019-12-11 03:45:59
问题 When in a separate Fsharp project in a SomeLib.fs file the following code is compiled: namespace SomeNameSpace type SomeType = member this.SomeMember = "Some member" and you want to reference and use this type in a script file like: #I @"c:/pathToDll/" #r "SomeLib.dll" This is not possible, although the path to the dll is correct and I checked everything. Also when the SomeLib.fs file is in the same project and referenced by #load, you still cannot open the namespace. I know you can put the

Pattern matching by function call

邮差的信 提交于 2019-12-11 03:43:41
问题 F# assigns function arguments via pattern matching. This is why // ok: pattern matching of tuples upon function call let g (a,b) = a + b g (7,4) works: The tuple is matched with (a,b) and a and b are available directly inside f. Doing the same with discriminated unions would be equally beneficial, but I cannot get it to done: // error: same with discriminated unions type A = | X of int * int | Y of string let f A.X(a, b) = a + b // Error: Successive patterns // should be separated by spaces