Created
July 20, 2010 15:56
-
-
Save t0yv0/483152 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let MapReduce (map: 'T1 -> 'T2) | |
(reduce: 'T2 -> 'T2 -> 'T2) | |
(zero: 'T2) | |
(inputs: seq<'T1>) = | |
let ( >>= ) x f = async.Bind(x, f) | |
Async.FromContinuations <| fun (ok, no, _) -> | |
let n = Seq.length inputs | |
let reducer = | |
MailboxProcessor.Start <| fun inbox -> | |
let rec loop n s = | |
if n = 0 then | |
async.Return (ok s) | |
else | |
inbox.Receive() >>= fun x -> | |
try loop (n - 1) (reduce s x) | |
with e -> async.Return (no e) | |
loop n zero | |
for x in inputs do | |
async { return try reducer.Post (map x) with e -> no e } | |
|> Async.Start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The reductions/joins are not completely parallel.