3 people like it.

n-ary Seq.map

A pattern for creating n-ary Seq.map functions.

1: 
2: 
3: 
4: 
5: 
6: 
let (<*>) fs xs = Seq.map2 (fun f x -> f x) fs xs

let map3 f xs bs cs = Seq.map2 f xs bs <*> cs
let map4 f xs bs cs ds = map3 f xs bs cs <*> ds

map4 (fun x b c d -> x + b + c + d) [1;2] [1;2] [1;2] [1;2] // [4; 8]
val fs : seq<('a -> 'b)>
val xs : seq<'a>
module Seq

from Microsoft.FSharp.Collections
val map2 : mapping:('T1 -> 'T2 -> 'U) -> source1:seq<'T1> -> source2:seq<'T2> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map2
val f : ('a -> 'b)
val x : 'a
val map3 : f:('a -> 'b -> 'c -> 'd) -> xs:seq<'a> -> bs:seq<'b> -> cs:seq<'c> -> seq<'d>

Full name: Script.map3
val f : ('a -> 'b -> 'c -> 'd)
val bs : seq<'b>
val cs : seq<'c>
val map4 : f:('a -> 'b -> 'c -> 'd -> 'e) -> xs:seq<'a> -> bs:seq<'b> -> cs:seq<'c> -> ds:seq<'d> -> seq<'e>

Full name: Script.map4
val f : ('a -> 'b -> 'c -> 'd -> 'e)
val ds : seq<'d>
val x : int
val b : int
val c : int
val d : int
Raw view New version

More information

Link:http://fssnip.net/9W
Posted:5 years ago
Author:Nick Palladinos
Tags: seq , map , zipwith , applicative functor