TheSharperDev

Educating about C# and F#

Seq.allPairs - F#

Seq.allPairs combines two sequences into one. Pairing all elements from the first and second sequence.

let allPairs s1 s2 = 
    (s1, s2)
    ||> Seq.allPairs

[<EntryPoint>]
let main argv =
    let seq1 = seq { 1 .. 4 }
    let seq2 = seq { 5 .. 8 }
    let result = allPairs seq1 seq2
    result 
        |> Seq.iter (fun x -> printfn "%O" x)
    0

Output:

(1, 5)
(1, 6)
(1, 7)
(1, 8)
(2, 5)
(2, 6)
(2, 7)
(2, 8)
(3, 5)
(3, 6)
(3, 7)
(3, 8)
(4, 5)
(4, 6)
(4, 7)
(4, 8)

Notes

  • I’m creating sequences using the seq { } style.
  • I can transform a list to a seq using List.toSeq
let seq1 = [1;2;3;4] |> List.toSeq