f#

Public Mutable Field in Object

拈花ヽ惹草 提交于 2019-12-24 05:32:27
问题 Is it possible to create a simple public mutable field in F#? I'm creating a library that I will be accessing from a C# program and I need to be able to set a field from C#. //C# Equivalent public class MyObj { public int myVariable; } //F# type MyObj = //my variable here member SomeMethod() = myVariable <- 10 //C# Usage MyObj obj = new MyObj(); obj.myVariable = 5; obj.SomeMethod() 回答1: type MyObj() = [<DefaultValue>] val mutable myVariable : int member this.SomeMethod() = this.myVariable <-

Public Mutable Field in Object

依然范特西╮ 提交于 2019-12-24 05:32:18
问题 Is it possible to create a simple public mutable field in F#? I'm creating a library that I will be accessing from a C# program and I need to be able to set a field from C#. //C# Equivalent public class MyObj { public int myVariable; } //F# type MyObj = //my variable here member SomeMethod() = myVariable <- 10 //C# Usage MyObj obj = new MyObj(); obj.myVariable = 5; obj.SomeMethod() 回答1: type MyObj() = [<DefaultValue>] val mutable myVariable : int member this.SomeMethod() = this.myVariable <-

F# Generic Map.count

狂风中的少年 提交于 2019-12-24 05:22:11
问题 This is a follow-up on this previous question, but with a different twist. I would like to write a function which, given an object oMap, returns its count if oMap happens to be of type Map<'k,'v> , and -1 otherwise. My constraint : oMap type can only be 'discovered' at runtime. As apparently "there is no built-in way to pattern match on a generic Map." (see link to previous question), I am using reflection for this. namespace genericDco module Test1 = let gencount (oMap : obj) : int = let

F# explicit member constraints: The type variable ^T could not be generalized because it would escape its scope

妖精的绣舞 提交于 2019-12-24 04:58:49
问题 I'm attempting to use explicit member constraints in F#. The documentation says "F# supports the complete set of constraints that is supported by the common language runtime", but if I actually compile a class with such an explicit constraint, such as the following, I get quite the exotic error. type MyType<'T when ^T: (static member ( + ) : ^T * ^T -> ^T)> = member this.F a b = a + b reports error FS0670: This code is not sufficiently generic. The type variable ^T when ^T : (static member (

F# Async.Parallel |> Async.RunSynchronously only uses one of the eight CPU core?

我的梦境 提交于 2019-12-24 04:55:08
问题 I created a dotnet core application and run the following code of release build. However, the total CPU usage of the PC is around only 20% and process dotnet run takes only 12% (I have eight logical CPUs and I don't see any one of it use 100% either). Isn't the CPU the bottleneck of the code? open FSharpx.Control [<EntryPoint>] let main argv = let ag = new BlockingQueueAgent<int option>(500) let enqueue() = async { for i = 0 to 1000 do ag.Add (Some i) } async { do! [ for i = 0 to 1000 do

How can I pass complex expression to parametrized active pattern?

戏子无情 提交于 2019-12-24 04:52:07
问题 I defined the active pattern "Expression" as follows: let (|Expression|_|) expression _ = Some(expression) Now I'm trying to use it in this way: match () with | Expression((totalWidth - wLeft - wRight) / (float model.Columns.Count - 0.5)) cw when cw <= wLeft * 4. && cw <= wRight * 4. -> cw | Expression((totalWidth - wLeft) / (float model.Columns.Count - .25)) cw when cw <= wLeft * 4. && cw > wRight * 4. -> cw | Expression((totalWidth - wRight) / (float model.Columns.Count - .25)) cw when cw >

F# missing type constraint

末鹿安然 提交于 2019-12-24 04:38:08
问题 In the following code, note the type constraint for get_Zero: type Wrapper<'t> = { Data : 't[] } let compute<'t when 't : (static member get_Zero : unit -> 't) and 't : (static member (~-) : 't -> 't) and 't : (static member (+) : 't * 't -> 't)> (wrapper : Wrapper<'t>) = wrapper.Data |> Seq.mapi (fun i value -> (i, value)) |> Seq.sumBy (fun (i, value) -> if i % 2 = 0 then value else -value) Even though I already have an explicit type constraint, I'm still getting the following compiler error

Fire and no-wait (without do!) vs Fire and await (do!) got huge difference performance?

假如想象 提交于 2019-12-24 04:32:21
问题 The following code takes about 20 seconds to run. However, it took less than a second after uncommenting the do! . Why there is such a huge difference? Update: it takes 9 seconds when using ag.Add . I've updated the code. open FSharpx.Control let test () = let ag = new BlockingQueueAgent<int option>(500) let enqueue() = async { for i = 1 to 500 do //do! ag.AsyncAdd (Some i) // less than a second with do! ag.AsyncAdd (Some i) // it takes about 20 seconds without do! //ag.Add (Some i) // This

Speech Recognition Engine does not shut down - invalid operation exception

牧云@^-^@ 提交于 2019-12-24 03:58:15
问题 Decided to wire up a simple speech writing application as a test bed for learning Speech Recognition + F#. To allow the speech recognition to be started or stopped I wired up the following methods: let Activate () = sp.RecognizeAsync(RecognizeMode.Multiple) let Deactivate () = sp.RecognizeAsyncCancel() sp.RecognizeAsyncStop() I can start the engine fine with the default grammar dictionary. The problem comes whens I call the deactivate method. I often get the following exception: An unhandled

Target the XNA framework in ordinary Visual Studio 2012 project

爱⌒轻易说出口 提交于 2019-12-24 03:38:16
问题 I have a C# XNA host project, with an F# client project that will contain all my functionality. It won't build because the F# project has references to mscorlib.dll and System.dll that the XNA project doesn't agree with (the actual error -- well, its really a warning that in my opinion should be an error -- is at the bottom of the post). How can I tell the F# project to target the actual XNA platform (specifically for the Xbox 360)? C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft