2 people like it.

100X Faster - Takes an array and splits it to arrays of arrays

Fast version - http://www.fssnip.net/sB

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
//Fast version for http://www.fssnip.net/sB
//Check on 1000000 elements array

let FastSlice chunk arr=
  let len=arr|>Array.length
  let res=Array.init (len/chunk) (fun x->arr.[(x*chunk)..((x+1)*chunk-1)])
  len%chunk|>function
     |0->res
     |x->[|arr.[len-x..]|]|>Array.append res
val FastSlice : chunk:int -> arr:'a [] -> 'a [] []

Full name: Script.FastSlice
val chunk : int
val arr : 'a []
val len : int
module Array

from Microsoft.FSharp.Collections
val length : array:'T [] -> int

Full name: Microsoft.FSharp.Collections.Array.length
val res : 'a [] []
val init : count:int -> initializer:(int -> 'T) -> 'T []

Full name: Microsoft.FSharp.Collections.Array.init
val x : int
val append : array1:'T [] -> array2:'T [] -> 'T []

Full name: Microsoft.FSharp.Collections.Array.append
Raw view Test code New version

More information

Link:http://fssnip.net/sC
Posted:8 years ago
Author:Zhukoff Dima
Tags: slice , chunk , split , array