Last active
August 29, 2015 13:57
-
-
Save atifaziz/9838444 to your computer and use it in GitHub Desktop.
GroupAdjacent for sequences 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
open System.Collections.Generic | |
let groupAdjacent projection (source : _ seq) = seq { | |
let rec loop (e : IEnumerator<_>) g (members : List<_>) = seq { | |
members.Add(e.Current) | |
if e.MoveNext() then | |
let key = projection e.Current | |
if key = g then | |
yield! loop e key members | |
else | |
yield g, seq members | |
yield! loop e key (List()) | |
else | |
yield g, seq members | |
} | |
use e = source.GetEnumerator() | |
if e.MoveNext() then | |
yield! loop e (projection e.Current) (List()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment