2 people like it.

Takes an array and splits it to arrays of arrays

To avoid too huge lists when doing recursion, SQL-IN-clauses, etc... Use the F# core version: chunkBySize

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
/// Takes an array and splits it to arrays of arrays
let slice (myArray : _[]) = 
    let chunkSize = 800
    match myArray.Length <= chunkSize with
    | true -> [|myArray|]
    | false -> 
        myArray
        |> Array.mapi (fun i x -> i,x)
        |> Array.groupBy (fun (i,x) -> i / chunkSize)
        |> Array.map (snd >> Array.map snd)
val slice : myArray:'a [] -> 'a [] []

Full name: Script.slice


 Takes an array and splits it to arrays of arrays
val myArray : 'a []
val chunkSize : int
property System.Array.Length: int
module Array

from Microsoft.FSharp.Collections
val mapi : mapping:(int -> 'T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.mapi
val i : int
val x : 'a
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.map
val snd : tuple:('T1 * 'T2) -> 'T2

Full name: Microsoft.FSharp.Core.Operators.snd

More information

Link:http://fssnip.net/sB
Posted:8 years ago
Author:Tuomas Hietanen
Tags: slice , chunk , split , array