Skip to content

Commit

Permalink
feat: add init (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia authored Oct 20, 2023
1 parent 9930253 commit 22c52bc
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/BwdLabels.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ let nth = BwdNoLabels.nth

let nth_opt = BwdNoLabels.nth_opt

let[@inline] init ~len ~f = BwdNoLabels.init len f

let append = BwdNoLabels.append

let prepend = BwdNoLabels.prepend
Expand Down
5 changes: 3 additions & 2 deletions src/BwdLabels.mli
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
{ul
{- [cons] was replaced by {!val:snoc}.}
{- {!val:append} was replaced by a new version that performs the "textual order yoga".}
{- The indices count from the right rather than the left:
{!val:nth}, {!val:nth_opt}, {!val:iteri}, {!val:mapi}, and {!val:filteri}.}
{- All indices count from the right rather than the left:
{!val:nth}, {!val:nth_opt}, {!val:init}, {!val:iteri}, {!val:mapi}, and {!val:filteri}.}
{- All iteration functions work from the right rather than the left:
{!val:iter}, {!val:map}, {!val:fold_left}, {!val:fold_right}, {!val:exists}, {!val:mem},
{!val:find}, {!val:filter}, and other similar functions.}}}
Expand Down Expand Up @@ -41,6 +41,7 @@ val compare_length_with : 'a t -> len:int -> int
val snoc : 'a t -> 'a -> 'a t
val nth : 'a t -> int -> 'a
val nth_opt : 'a t -> int -> 'a option
val init : len:int -> f:(int -> 'a) -> 'a t
val append : 'a t -> 'a list -> 'a t
val prepend : 'a t -> 'a list -> 'a list

Expand Down
11 changes: 11 additions & 0 deletions src/BwdNoLabels.ml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ let nth_opt xs i =
| Snoc (xs, _), i -> (go[@tailcall]) (xs, i - 1)
in go (xs, i)

let init len f =
if len < 0 then invalid_arg "Bwd.init"
else
let[@tail_mod_cons] rec go i =
if i >= len then Emp
else
let v = f i in
Snoc ((go[@tailcall]) (i+1), v)
in
go 0

let append xs ys =
let rec go =
function
Expand Down
5 changes: 3 additions & 2 deletions src/BwdNoLabels.mli
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
{ul
{- [cons] was replaced by {!val:snoc}.}
{- {!val:append} was replaced by a new version that performs the "textual order yoga".}
{- The indices count from the right rather than the left:
{!val:nth}, {!val:nth_opt}, {!val:iteri}, {!val:mapi}, and {!val:filteri}.}
{- All indices count from the right rather than the left:
{!val:nth}, {!val:nth_opt}, {!val:init}, {!val:iteri}, {!val:mapi}, and {!val:filteri}.}
{- All iteration functions work from the right rather than the left:
{!val:iter}, {!val:map}, {!val:fold_left}, {!val:fold_right}, {!val:exists}, {!val:mem},
{!val:find}, {!val:filter}, and other similar functions.}}}
Expand Down Expand Up @@ -41,6 +41,7 @@ val compare_length_with : 'a t -> int -> int
val snoc : 'a t -> 'a -> 'a t
val nth : 'a t -> int -> 'a
val nth_opt : 'a t -> int -> 'a option
val init : int -> (int -> 'a) -> 'a t
val append : 'a t -> 'a list -> 'a t
val prepend : 'a t -> 'a list -> 'a list

Expand Down
4 changes: 4 additions & 0 deletions test/ListAsBwdLabels.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ let nth_opt xs i =
try L.nth_opt (L.rev xs) i with
| Invalid_argument _ -> invalid_arg "Bwd.nth_opt"

let init ~len ~f =
try L.init ~len ~f:(fun i -> f ((len-1) - i)) with
| Invalid_argument _ -> invalid_arg "Bwd.init"

let append xs ys = xs @ ys

let prepend xs ys = xs @ ys
Expand Down
6 changes: 6 additions & 0 deletions test/TestBwdLabels.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ let test_nth_opt =
trap (fun () -> B.nth_opt (of_list xs) i)
=
trap (fun () -> L.nth_opt xs i))
let test_init =
Q.Test.make ~count ~name:"init"
Q.Gen.(pair small_signed_int (Q.fun1 Q.Observable.int (opt int)))
~print:Q.Print.(pair int Q.Fn.print)
(fun (len, Fun (_, f)) -> trap (fun () -> to_list (B.init ~len ~f)) = trap (fun () -> L.init ~len ~f))
let test_append =
Q.Test.make ~count ~name:"append" Q.Gen.(pair (small_list int) (small_list int))
~print:Q.Print.(pair (list int) (list int))
Expand Down Expand Up @@ -299,6 +304,7 @@ let () =
test_snoc;
test_nth;
test_nth_opt;
test_init;
test_append;
test_prepend;
test_equal;
Expand Down

0 comments on commit 22c52bc

Please sign in to comment.