Most idiomatic way to write batchesOf size seq in F#

前端 未结 10 539
迷失自我
迷失自我 2020-12-16 18:42

I\'m trying to learn F# by rewriting some C# algorithms I have into idiomatic F#.

One of the first functions I\'m trying to rewrite is a batchesOf where:

         


        
10条回答
  •  生来不讨喜
    2020-12-16 18:54

    This isn't perhaps idiomatic but it works:

    let batchesOf n l = 
        let _, _, temp', res' = List.fold (fun (i, n, temp, res) hd ->
                                               if i < n then
                                                 (i + 1, n, hd :: temp, res)
                                               else
                                                 (1, i, [hd], (List.rev temp) :: res)) 
                                           (0, n, [], []) l
        (List.rev temp') :: res' |> List.rev
    

提交回复
热议问题