f#

How To Apply a Function to an Array of float Arrays?

有些话、适合烂在心里 提交于 2019-12-30 11:08:07
问题 Let's suppose I have n arrays, where n is a variable (some number greater than 2, usually less than 10). Each array has k elements. I also have an array of length n that contains a set of weights that dictate how I would like to linearly combine all the arrays. I am trying to create a high performance higher order function to combine these arrays in F#. How can I do this, so that I get a function that takes an array of arrays (arrs is a sample), a weights array (weights), and then computed a

Semantics of “>>” operator in F#

偶尔善良 提交于 2019-12-30 10:45:31
问题 In Microsoft's F# samples, they use the ">>" operator as follows: test |> Seq.iter (any_to_string >> printfn "line %s"); What does the ">>" operator do in this context? Is each item of the sequence (an array in this case) get passed to any_to_string implicitly? Is this similar to (fun item -> printfn "line %A" item) ? 回答1: An equivalent piece of code could be written the following way: test |> Seq.iter(fun x -> printfn "line %s" (any_to_string x)) In other words, the >> operator simply does

Semantics of “>>” operator in F#

那年仲夏 提交于 2019-12-30 10:45:02
问题 In Microsoft's F# samples, they use the ">>" operator as follows: test |> Seq.iter (any_to_string >> printfn "line %s"); What does the ">>" operator do in this context? Is each item of the sequence (an array in this case) get passed to any_to_string implicitly? Is this similar to (fun item -> printfn "line %A" item) ? 回答1: An equivalent piece of code could be written the following way: test |> Seq.iter(fun x -> printfn "line %s" (any_to_string x)) In other words, the >> operator simply does

Creating a generic List <T> in F#

会有一股神秘感。 提交于 2019-12-30 08:18:10
问题 I am trying to create a standard .NET List<T> in F# like this: module Csv open System; type Sheet () = let rows = new List<Object>() but I get the following error: No constructors are available for the type List<Object> C:\…\Csv.fs : 6 What am I doing wrong? 回答1: As a simpler alternative to what others suggest, you can use the type named ResizeArray<T> . This is a type alias for System.Collections.Generic.List<T> defined in the F# core libraries: type Sheet () = let rows = new ResizeArray

How to create a F# based Visual Studio Add-In?

。_饼干妹妹 提交于 2019-12-30 06:58:05
问题 Even with F# installed, Visual Studio 2008 (and probably 2010) only provides Add-In project templates for C#, VB.NET and C++. So, how to create a F# based Add-In? I have tried to create a F# class library project with a new class implementing IDTExtensibility2, setting the correct references (EnvDTE, Extensibility, ...), copying a few project attributes from the C# Add-In (mainly the command line for debugging), and creating the .AddIn manifest file by hand, but no success. When VS is

Overloading + operator in F#

寵の児 提交于 2019-12-30 06:47:16
问题 So i have this: open System open System.Linq open Microsoft.FSharp.Collections type Microsoft.FSharp.Collections.List<'a> with static member (+) (First : List<'a>) (Second : List<'a>) = First.Concat(Second) let a = [1; 2; 3; 4; 54; 9] let b = [3; 5; 6; 4; 54] for x in List.(+) a b do Console.WriteLine(x) and I want to convert the last line into for x in a + b do Console.WriteLine(x) but doing so gives me a The type 'int list' does not support any operands named '+' The documentation and

FSharpChoice in C#

不想你离开。 提交于 2019-12-30 06:42:09
问题 I am trying to use FSharpChoice type in a C# project. I have created a choice like so var a = FSharpChoice<T1,T2,T3>.NewChoice1Of3(instofT1); now how do I get instofT1 out of the choice type. I see I can do a IsChoice1Of3 but how do i get to the value in the choice object? 回答1: Cast the value to FSharpChoice<T1,T2,T3>.Choice1Of3 and use the Item property. See Compiled Form of Union Types for Use from Other CLI Languages in the F# spec for more information about how discriminated unions are

F# asynchronous event handlers for WPF similar to C#'s async and await

拈花ヽ惹草 提交于 2019-12-30 06:37:10
问题 How does one code an asynchronous WPF (or Windows Forms) event handler in F#? Specifically, is there any coding pattern that approximates C# 5's async and await? Here is a complete C# WPF app: using System; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; class Program { static int IncrementSlowly(int previous) { Thread.Sleep(3000); if (previous == 2) throw new Exception("Oops!"); return previous + 1; } static async void btn_Click

Running F# code with Mono

白昼怎懂夜的黑 提交于 2019-12-30 06:23:09
问题 I have F# 2.0.0 installed, and use mono 2.8. let rec fib n = match n with | 1 | 2 -> 1 | n -> fib(n-1) + fib(n-2) let n = 40 let x = fib(n) printfn "%d" x I compiled this code with fsc.exe to get fib.exe. Running this with mono fib.exe gives me this error. mono fact.exe Could not load file or assembly 'FSharp.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. Unhandled Exception: System.TypeLoadException: Could not load type '.$Factorial' from

F# and interface covariance: what to do? (specifically seq<> aka IEnumerable<>)

99封情书 提交于 2019-12-30 05:59:25
问题 I'm trying to call a .NET method accepting a generic IEnumerable<T> from F# using a seq<U> such that U is a subclass of T. This doesn't work the way I expected it would: With the following simple printer: let printEm (os: seq<obj>) = for o in os do o.ToString() |> printfn "%s" These are the results I get: Seq.singleton "Hello World" |> printEm // error FS0001; //Expected seq<string> -> 'a but given seq<string> -> unit Seq.singleton "Hello World" :> seq<obj> |> printEm // error FS0193; //seq