Microsoft (R) F# 2.0 Interactive build 2.0.0.0 Copyright (c) Microsoft Corporation. All Rights Reserved. > let fizzBuzzMapper data = - Seq.map ( fun elem -> async { return (elem, (elem % 3 = 0), (elem % 5 = 0) ) } ) data;; val fizzBuzzMapper : seq<int> -> seq<Async<int * bool * bool>> > let fizzBuzzReducer interims = - Array.foldBack (fun (elem, m3, m5) acc -> - match (m3, m5) with - | (true, true) -> "FizzBuzz" :: acc - | (true, false) -> "Fizz" :: acc - | (false, true) -> "Buzz" :: acc - | _ -> elem.ToString() :: acc ) interims [];; val fizzBuzzReducer : ('a * bool * bool) [] -> string list > [1..40] |> fizzBuzzMapper |> Async.Parallel |> Async.RunSynchronously |> fizzBuzzReducer;; val it : string list = ["1"; "2"; "Fizz"; "4"; "Buzz"; "Fizz"; "7"; "8"; "Fizz"; "Buzz"; "11"; "Fizz"; "13"; "14"; "FizzBuzz"; "16"; "17"; "Fizz"; "19"; "Buzz"; "Fizz"; "22"; "23"; "Fizz"; "Buzz"; "26"; "Fizz"; "28"; "29"; "FizzBuzz"; "31"; "32"; "Fizz"; "34"; "Buzz"; "Fizz"; "37"; "38"; "Fizz"; "Buzz"] > [1..40] |> ( fizzBuzzReducer << Async.RunSynchronously << Async.Parallel << fizzBuzzMapper ) |> List.iter (fun elem -> System.Console.WriteLine(elem));; 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz val it : unit = ()
Friday, August 26, 2011
Studying F# : async
Labels:
F#
The sample in phosphorescence: Studying F# : Accumulator, pipe and composition is written as sync program. In this article, I try to re-write that sample to async program with async { } cluase.
No comments:
Post a Comment