Most idiomatic way to write batchesOf size seq in F#

前端 未结 10 538
迷失自我
迷失自我 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:55

    This can be done without recursion if you want

    [0..20] 
        |> Seq.mapi (fun i elem -> (i/size),elem) 
        |> Seq.groupBy (fun (a,_) -> a)
        |> Seq.map (fun (_,se) -> se |> Seq.map (snd));;
    val it : seq> =
      seq
        [seq [0; 1; 2; 3; ...]; seq [5; 6; 7; 8; ...]; seq [10; 11; 12; 13; ...];
         seq [15; 16; 17; 18; ...]; ...]
    

    Depending on how you think this may be easier to understand. Tomas' solution is probably more idiomatic F# though

提交回复
热议问题