Created
February 26, 2013 10:06
-
-
Save rojepp/5037423 to your computer and use it in GitHub Desktop.
contiguousGroupBy
This file contains hidden or 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
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