Skip to content

Instantly share code, notes, and snippets.

@thinkbeforecoding
Created January 20, 2014 23:30
Show Gist options
  • Save thinkbeforecoding/8531483 to your computer and use it in GitHub Desktop.
Save thinkbeforecoding/8531483 to your computer and use it in GitHub Desktop.
This is an example of the Zip (applicative functor) experimental extension to Computation Expression. using for .. and ... do, sources are merges using the .Merge(xs,f) method. If Select is defined and the rest of the expression is a simple projections, the result is Select(Merge(xs,ys), projection) else it is Bind(Merge(xs,ys), rest of the expr…
open System
type ZipBuilder() =
member t.Zip(xs,ys) = List.zip xs ys
member t.For(xs,f) = List.collect f xs
member t.Yield x = [x]
member t.Select(xs,f) =
List.map f xs
member t.Zero() = []
let zip = new ZipBuilder()
let xs = [ 1; 2; 7]
let ys = [ 4; 6; 9]
let zs = [ 2; 4; 6]
//let r =
zip {
for x in xs
and y in ys
and z in zs do
yield x + y + z
} |> List.iter (printfn "%d")
// the result is [ 7; 12; 22]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment