Skip to content

Instantly share code, notes, and snippets.

@tjaskula
Last active August 29, 2015 14:16
Show Gist options
  • Save tjaskula/fb2860f6a0c50f4964f1 to your computer and use it in GitHub Desktop.
Save tjaskula/fb2860f6a0c50f4964f1 to your computer and use it in GitHub Desktop.
Euler
let rec split<'T> (input: 'T array) size =
let rec loopOn (tail : 'T array) grouped =
let lastIndex = Array.length tail - 1
let endindx = min (size - 1) lastIndex
let arrWrapper = (fun e -> [|e|])
let newGroup = tail.[0..endindx]
|> List.ofArray
|> arrWrapper
|> Array.append grouped
match tail with
| [||] -> newGroup
|> Array.filter (fun e -> List.length e > 0)
| _ -> loopOn tail.[endindx + 1..] newGroup
let initialState = [|List.empty<'T>|]
loopOn input initialState
type Custom = {Value : int}
let r = split<int> [|1..1000|] 10
let r2 = split<float> [|1.0..1000.0|] 10
let r3 = split<Custom> [|for i in 1..1000 ->
{Value = i}|] 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment