3 people like it.

Split array into chunks

Splits array into chunks, returning seq of seqs. Works with F# 3.x

1: 
2: 
3: 
4: 
5: 
6: 
let chunk chunkSize (arr : _ array) = 
    query {
        for idx in 0..(arr.Length - 1) do
        groupBy (idx / chunkSize) into g
        select (g |> Seq.map (fun idx -> arr.[idx]))
    }
val chunk : chunkSize:int -> arr:'a array -> seq<seq<'a>>

Full name: Script.chunk
val chunkSize : int
val arr : 'a array
type 'T array = 'T []

Full name: Microsoft.FSharp.Core.array<_>
val query : Linq.QueryBuilder

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.query
val idx : int
property System.Array.Length: int
custom operation: groupBy ('Key)

Calls Linq.QueryBuilder.GroupBy
val g : System.Linq.IGrouping<int,int>
custom operation: select ('Result)

Calls Linq.QueryBuilder.Select
module Seq

from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
Raw view Test code New version

More information

Link:http://fssnip.net/k2
Posted:10 years ago
Author:Lakret
Tags: collections