Created
December 3, 2012 08:30
-
-
Save JohannesRudolph/4193650 to your computer and use it in GitHub Desktop.
F# Cond Zip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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