Skip to content

Instantly share code, notes, and snippets.

@grishace
Last active August 29, 2019 09:42
Show Gist options
  • Select an option

  • Save grishace/96b43f87c4a5337de1f1 to your computer and use it in GitHub Desktop.

Select an option

Save grishace/96b43f87c4a5337de1f1 to your computer and use it in GitHub Desktop.
Functional Friday with @dstcruz
let reqs = [| 0 .. 6 |]
let chunks = [| 4; 1; 2 |]
let splitter rq ch =
let rec splitter' r (c:seq<int>) = seq {
if not(Seq.isEmpty c) then
let cx = c |> Seq.take 1 |> Seq.exactlyOne
yield r |> Seq.take cx
yield! splitter' (Seq.skip cx r) (Seq.skip 1 c)
}
splitter' rq ch
let res = splitter reqs chunks
res.Dump()
let fsplitter r c =
let fsplitter' rq (ch:array<int>) =
Seq.fold(fun (res, acct, acctl, ci) r ->
let ac = Seq.append acct [r]
let acl = acctl + 1
if acl = ch.[ci] then
(Seq.append res [ac], Seq.empty, 0, ci + 1)
else
(res, ac, acl, ci)
) (Seq.empty, Seq.empty, 0, 0) rq
let (res, _, _, _) = fsplitter' r (Array.ofSeq c)
res
let res1 = fsplitter reqs chunks
res1.Dump()
let rec splitAt n a =
match a with
| [] -> [], []
| x::xs -> match n with
| 0 -> [], a
| n -> let ax, axs = splitAt (n-1) xs
x::ax, axs
let rec asplitter r c = seq {
let cx, cr = splitAt 1 c
match cx with
| [c0] ->
let rx, rr = splitAt c0 r
yield rx
yield! asplitter rr cr
| _ -> ()
}
let res2 = asplitter (List.ofArray reqs) (List.ofArray chunks)
res2.Dump()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment