F# split sequence into sub lists on every nth element

后端 未结 8 864
时光说笑
时光说笑 2020-12-06 11:23

Say I have a sequence of 100 elements. Every 10th element I want a new list of the previous 10 elements. In this case I will end up with a list of 10 sublists.

Seq.t

8条回答
  •  一整个雨季
    2020-12-06 11:46

    Out of the top of my head:

    let rec split size list =
    if List.length list < size then
        [list]
    else
        (list |> Seq.take size |> Seq.toList) :: (list |> Seq.skip size |> Seq.toList |> split size)
    

提交回复
热议问题