f#

Array.create and jagged array

大憨熊 提交于 2019-12-18 08:47:27
问题 Can't understand the reason of such behavior: let example count = let arr = Array.create 2 (Array.zeroCreate count) for i in [0..count - 1] do arr.[0].[i] <- 1 arr.[1].[i] <- 2 arr example 2 |> Array.iter(printfn "%A") Print: [|2; 2|] [|2; 2|] https://dotnetfiddle.net/borMmO If I replace: let arr = Array.create 2 (Array.zeroCreate count) to: let arr = Array.init 2 (fun _ -> Array.zeroCreate count) Everything will work as expected: let example count = let arr = Array.init 2 (fun _ -> Array

MissingMethodException when testing a function that takes a function parameter

无人久伴 提交于 2019-12-18 08:46:27
问题 I am using FsUnit 2.1 (with NUnit 3.2) to write tests for an F# project. Here is a simple module: namespace Library1 module LibraryFunctions = let Execute f1 = f1() let Id x = x And here are my tests: namespace Tests open FsUnit open NUnit.Framework open Library1 [<TestFixture>] type Tests() = [<Test>] // Passes member x.``LibraryFunctions.Id should return the value``() = LibraryFunctions.Id 42 |> should equal 42 [<Test>] // Fails member x.``LibraryFunctions.Execute should return the result

F# converting Array2D to array of arrays

眉间皱痕 提交于 2019-12-18 08:26:16
问题 In F# is there a concise way of converting a float[,] to float[][]? In case this seems like a stupid thing to do it is so I can use Array.zip on the resulting array of arrays. Any help greatly appreciated. 回答1: This should do the trick: module Array2D = let toJagged<'a> (arr: 'a[,]) : 'a [][] = [| for x in 0 .. Array2D.length1 arr - 1 do yield [| for y in 0 .. Array2D.length2 arr - 1 -> arr.[x, y] |] |] 回答2: Keep in mind that Array2D can have a base other than zero. For every dimension, we

The mutable variable 'i' is used in an invalid way.?

大城市里の小女人 提交于 2019-12-18 07:58:28
问题 I am attempting to write some simple code in F#, and i get this error: Error 1 The mutable variable 'i' is used in an invalid way. Mutable variables may not be captured by closures. Consider eliminating this use of mutation or using a heap-allocated mutable reference cell via 'ref' and '!' Code: let printProcess = async { let mutable i = 1; while true do System.Console.WriteLine(i);//error is here i <- i + 1; } Why won't it let me print the variable? 回答1: You can't refer to mutables inside a

The mutable variable 'i' is used in an invalid way.?

无人久伴 提交于 2019-12-18 07:58:06
问题 I am attempting to write some simple code in F#, and i get this error: Error 1 The mutable variable 'i' is used in an invalid way. Mutable variables may not be captured by closures. Consider eliminating this use of mutation or using a heap-allocated mutable reference cell via 'ref' and '!' Code: let printProcess = async { let mutable i = 1; while true do System.Console.WriteLine(i);//error is here i <- i + 1; } Why won't it let me print the variable? 回答1: You can't refer to mutables inside a

F# Floating point ranges are experimental and may be deprecated

南楼画角 提交于 2019-12-18 06:54:34
问题 I'm trying to make a little function to interpolate between two values with a given increment. [ 1.0 .. 0.5 .. 20.0 ] The compiler tells me that this is deprecated, and suggests using ints then casting to float. But this seems a bit long-winded if I have a fractional increment - do I have to divide my start and end values by my increment, then multiple again afterwards? (yeuch!). I saw something somewhere once about using sequence comprehensions to do this, but I can't remember how. Help,

How to partition a list with a given group size?

守給你的承諾、 提交于 2019-12-18 06:53:40
问题 I'm looking for the best way to partition a list (or seq) so that groups have a given size. for ex. let's say I want to group with size 2 (this could be any other number though): let xs = [(a,b,c); (a,b,d); (y,z,y); (w,y,z); (n,y,z)] let grouped = partitionBySize 2 input // => [[(a,b,c);(a,b,d)]; [(y,z,y);(w,y,z)]; [(n,y,z)]] The obvious way to implement partitionBySize would be by adding the position to every tuple in the input list so that it becomes [(0,a,b,c), (1,a,b,d), (2,y,z,y), (3,w,y

How to partition a list with a given group size?

血红的双手。 提交于 2019-12-18 06:53:16
问题 I'm looking for the best way to partition a list (or seq) so that groups have a given size. for ex. let's say I want to group with size 2 (this could be any other number though): let xs = [(a,b,c); (a,b,d); (y,z,y); (w,y,z); (n,y,z)] let grouped = partitionBySize 2 input // => [[(a,b,c);(a,b,d)]; [(y,z,y);(w,y,z)]; [(n,y,z)]] The obvious way to implement partitionBySize would be by adding the position to every tuple in the input list so that it becomes [(0,a,b,c), (1,a,b,d), (2,y,z,y), (3,w,y

Returning arrays of different dimensions from one function; is it possible in F#?

≡放荡痞女 提交于 2019-12-18 06:48:07
问题 I am trying to convert some Python to F#, specifically numpy.random.randn. The function takes a variable number of int arguments and returns arrays of different dimensions based on the number of arguments. I believe that this is not possible because one cannot have a function that returns different types ( int[] , int[][] , int[][][] , etc.) unless they are part of a discriminated union, but want to be sure before committing to a workaround. The sanity check: member self.differntarrays ([

Parametric LINQ query

浪尽此生 提交于 2019-12-18 06:29:07
问题 This is another take on accessing dynamic objects in F# There I'm using let y = x.Where(fun x -> x.City ="London").Select("new(City,Zip)") to parametrize the query and extract the necessary items. These would correspond to columns in an SQL query, and be represented by a property of the datacontext. This is the part that I would like to pass in as a parameter. type Northwind = ODataService<"http://services.odata.org/Northwind/Northwind.svc"> let db = Northwind.GetDataContext() let query2 =