I’m learning F#. Recently, I read about the improvement about .NET 4.5 about tasks.
So, I took a peek at how to use it in F#.
In fsi:
use System.Threading.Tasks
Parallel.ForEach([1;2],fun x->x+1);;
Gives a lot of errors.
In addition, in the source code of Fsharp core lib. I saw:
module Parallel =
open System.Threading.Tasks
[<CompiledName("Choose")>]
let choose f (array: 'T[]) =
checkNonNull "array" array
let inputLength = array.Length
let lastInputIndex = inputLength - 1
let isChosen : bool [] = //(Deliberately offside to avoid scrollbars)
Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked inputLength
let results : 'U [] =
Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked inputLength
However, I am unable to use Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked. Why?
I got:
error FS1092: The type ‘Array’ is not accessible from this code
location.
Your use of
ForEachmethod is confusing, because your lambda function returns a result. TheForEachoperation can be used to perform some imperative action for all element from the input, so the lambda function should returnunit. The following will work fine:I also added
ignore, becauseForEachreturns some value as a result (and we don’t use it).Regarding the F# core library code – the code is compiled as part of F# core library, so it can use some internal functions that are not exposed in the public API and so you can’t use them. In your case, you’ll need to use
Array.zeroCreateinstead ofArray.zeroCreateUnchecked.