Reputation: 1416
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.
Upvotes: 2
Views: 850
Reputation: 243041
Your use of ForEach
method is confusing, because your lambda function returns a result. The ForEach
operation can be used to perform some imperative action for all element from the input, so the lambda function should return unit
. The following will work fine:
open System.Threading.Tasks
Parallel.ForEach([1;2], fun x->
printfn "%d" (x+1)) |> ignore
I also added ignore
, because ForEach
returns 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.zeroCreate
instead of Array.zeroCreateUnchecked
.
Upvotes: 8