Skip to content

Instantly share code, notes, and snippets.

@JohannesRudolph
Created December 3, 2012 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohannesRudolph/4193650 to your computer and use it in GitHub Desktop.
Save JohannesRudolph/4193650 to your computer and use it in GitHub Desktop.
F# Cond Zip
open System.Linq
let a = [0; 5; 10]
let b = [0 .. 10]
// enumerates left and yields element pairs combined by consuming right until 'consume' is false or either sequence is exhausted
let condZip (left:seq<'a>) (right:seq<'b>) consume project =
seq {
use en = right.GetEnumerator()
let hadNext = ref(en.MoveNext())
for l in left do
while (!hadNext && consume l en.Current) do // had next is used to ensure we do not drop elements when until returns false
yield project l en.Current
hadNext := en.MoveNext()
}
let result = condZip a b (fun l r -> r <= l) (fun l r -> (r, l))
let expected = [(0, 0);(1, 5);(2, 5);(3, 5);(4, 5);(5, 5);(6, 10);(7, 10);(8, 10);(9, 10);(10, 10)]
Enumerable.SequenceEqual(result, expected)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment