Skip to content

Instantly share code, notes, and snippets.

@FunctionalFirst
Created April 11, 2012 22:36
Show Gist options
  • Save FunctionalFirst/2363222 to your computer and use it in GitHub Desktop.
Save FunctionalFirst/2363222 to your computer and use it in GitHub Desktop.
let groupsOf n (items: seq<_>) =
use e = items.GetEnumerator()
let rec loop i acc =
seq {
match i, acc, e.MoveNext() with
| 0, [], _ | _, [], false -> ()
| _, _, false ->
yield seq (List.rev acc)
| 0, _, true ->
yield seq (List.rev acc)
yield! loop (n - 1) [e.Current]
| _, _, true ->
yield! loop (i - 1) (e.Current::acc)
}
loop n []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment