I'm currently writing a bit of F#. I've created a method that is the equivalent of Ruby's Enumerable#each_slice
method and was wondering if somebody has a better (i.e. more elegant, more concise, more readable) solution.
Here it is:
let rec slicesBySize size list =
match list with
| [] -> [] // case needed for type inference
| list when list.Length < size -> [list]
| _ ->
let first = list |> Seq.take size |> List.ofSeq
let rest = list |> Seq.skip size |> List.ofSeq
[first] @ slicesBySize size rest
Thanks for any and all feedback/help.