Pairing operator in F#
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
// 'a seq -> ('a * 'a) seq | |
let pair (series: 'a seq) = seq { | |
use enum = series.GetEnumerator() | |
if enum.MoveNext() then | |
let first = ref (Some enum.Current) | |
while enum.MoveNext() do | |
if Option.isSome !first then | |
yield (Option.get !first), enum.Current | |
first := None | |
else | |
first := Some enum.Current | |
if Option.isSome !first then | |
let msg = sprintf "Dangling element: %A" (Option.get !first) | |
failwith msg | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment