github-tui/lib/list_extra.ml
2024-03-24 13:21:30 +00:00

18 lines
372 B
OCaml

let in_between ~sep list =
let rec loop = function
| [] -> []
| x :: xs -> sep :: x :: loop xs
in
match list with
| [] | [ _ ] -> list
| x :: xs -> x :: loop xs
let generate n f =
let rec loop i = if i = n then [] else f i :: loop (i + 1) in
loop 0
let rec take n = function
| _ when n <= 0 -> []
| [] -> []
| x :: xs -> x :: take (n - 1) xs