Skip to content

Instantly share code, notes, and snippets.

@rojepp
Created February 26, 2013 10:06
Show Gist options
  • Save rojepp/5037423 to your computer and use it in GitHub Desktop.
Save rojepp/5037423 to your computer and use it in GitHub Desktop.
contiguousGroupBy
let inline contiguousGroupBy (f:'a -> 'b) (xs:'a seq) : seq<'a list> = seq {
use e = xs.GetEnumerator()
let stop = ref (e.MoveNext() |> not)
while not !stop do
let lastval = ref (f e.Current)
let list =
[
while not !stop && !lastval = f e.Current do
let cur = e.Current
lastval := f cur
yield cur
stop := e.MoveNext() |> not
]
yield list }
//Test
contiguousGroupBy (id) "Mississippi";;
val it : seq<char list> = seq [['M']; ['i']; ['s'; 's']; ['i']; ...]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment