Skip to content

Instantly share code, notes, and snippets.

@benjamin-bader
Created November 10, 2012 02:58
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 benjamin-bader/4049674 to your computer and use it in GitHub Desktop.
Save benjamin-bader/4049674 to your computer and use it in GitHub Desktop.
Pairing operator in F#
// '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