Type mismatch with Async in F?

Seq. Reduce takes a function and a sequence and reduces the list using the function. That is, the outcome of reducing a sequence {a1;a2;a3;...;an} with function f will be f(f(...f(f(a1,a2),a3),...),an))...) However, the function you're passing can't be applied this way, because the return type ( Async failwith "Can't reduce an empty array" | 1 -> arr.0 | n -> // Create an array with n/2 tasks, each of which combines neighboring entries using f Array.

Init ((n+1)/2) (fun I -> async { // if n is odd, leave last item alone if n = 2*i + 1 then return arr.2*i else return f arr.2*i arr.2*i+1}) |> Async. Parallel |> Async. RunSynchronously |> reduce f Note that converting items to and from Async values all happens internally to this function, so it has the same type as Array.

Reduce and would be used with your normal compareVariance function rather than with aCompareVariance.

Seq. Reduce takes a function and a sequence and reduces the list using the function. That is, the outcome of reducing a sequence {a1;a2;a3;...;an} with function f will be f(f(...f(f(a1,a2),a3),...),an))...).

However, the function you're passing can't be applied this way, because the return type (Async) doesn't match the argument types (float list * float). What exactly are you trying to achieve? Also, keep in mind that async computations are great for asynchronous work, but not ideal for parallel work.

See stackoverflow.com/questions/2178608/f-as... and stackoverflow.com/questions/1871168/f-ta.... EDIT Here's one way to write a function which will reduce items more like you expected, operating sort of like this: |a1; a2; a3; a4| |f a1 a2; f a3 a4| f (f a1 a2) (f a3 a4) At each stage, all applications of f will take place in parallel. This uses Async.

Parallel, which as mentioned above is probably less appropriate than using the Task Parallel Library (and may even be slower than just doing a normal synchronous Array. Reduce). As such, just consider this to be demonstration code showing how to piece together the async functions.

Let rec reduce f (arr:_) = match arr. Length with | 0 -> failwith "Can't reduce an empty array" | 1 -> arr.0 | n -> // Create an array with n/2 tasks, each of which combines neighboring entries using f Array. Init ((n+1)/2) (fun I -> async { // if n is odd, leave last item alone if n = 2*i + 1 then return arr.2*i else return f arr.2*i arr.2*i+1}) |> Async.

Parallel |> Async. RunSynchronously |> reduce f Note that converting items to and from Async values all happens internally to this function, so it has the same type as Array. Reduce and would be used with your normal compareVariance function rather than with aCompareVariance.

I guess I had the wrong idea about reduce. I was hoping that it would work more like this for a sequence {a1;a2;a3;a4}: f( f(a1,a2), f(a3,a4) ). If it was executed like that, f(a1,a2) and f(a3,a4) could run in parallel.

– user282232 Feb 26 '10 at 17:46 @user282232 - Even in that case, you'd want the input and output types for f to match, since the outermost f acts on the output from previous reduction steps. – kvb Feb 26 '10 at 18:03 So why don't those types match? A is what it is, whether it's coming from another thread or the same one.

– user282232 Feb 26 '10 at 18:09 @user282232 - Those types can't match. An Async is a computation that will return an x; this is different from a value of type x that has already been calculated. You can convert from one to the other using async.

Return and Async. RunSynchronously, but the types can't be used interchangeably. – kvb Feb 26 '10 at 18:18 Ah, I think I get it.So when I see: for I in 0..10 -> I |> Seq.

Map (fun x -> async { return x*2 }) |> Async. Parallel |> Async. RunSynchronously What's happening is that the map returns a sequence of not-yet-performed computations, and then the Async stuff executes those in a parallel manner and returns their results in an array?

– user282232 Feb 26 '10 at 18:27.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions