Skip to content

Commit

Permalink
feat: add concat and flatten
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia committed Oct 17, 2023
1 parent 1753338 commit 28551a3
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/BwdLabels.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ let append = BwdNoLabels.append

let prepend = BwdNoLabels.prepend

let concat = BwdNoLabels.concat

let flatten = BwdNoLabels.flatten

let[@inline] equal ~eq xs ys = BwdNoLabels.equal eq xs ys

let[@inline] compare ~cmp xs ys = BwdNoLabels.compare cmp xs ys
Expand Down
2 changes: 2 additions & 0 deletions src/BwdLabels.mli
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ val nth : 'a t -> int -> 'a
val nth_opt : 'a t -> int -> 'a option
val append : 'a t -> 'a list -> 'a t
val prepend : 'a t -> 'a list -> 'a list
val concat : 'a t t -> 'a t
val flatten : 'a t t -> 'a t

(** {1 Comparison} *)

Expand Down
18 changes: 18 additions & 0 deletions src/BwdNoLabels.ml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ let prepend xs ys =
(go[@tailcall]) (xs, x :: ys)
in go (xs, ys)

let concat =
let[@tail_mod_cons] rec go bs =
function
| Emp ->
begin
match bs with
| Emp -> Emp
| Snoc (bs, b) -> (go[@tailcall]) bs b
end
| Snoc (xs, x) ->
Snoc ((go[@tailcall]) bs xs, x)
in
function
| Emp -> Emp
| Snoc (bs, b) -> go bs b

let flatten = concat

let equal eq xs ys =
let rec go =
function
Expand Down
2 changes: 2 additions & 0 deletions src/BwdNoLabels.mli
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ val nth : 'a t -> int -> 'a
val nth_opt : 'a t -> int -> 'a option
val append : 'a t -> 'a list -> 'a t
val prepend : 'a t -> 'a list -> 'a list
val concat : 'a t t -> 'a t
val flatten : 'a t t -> 'a t

(** {1 Comparison} *)

Expand Down
4 changes: 4 additions & 0 deletions test/ListAsBwdLabels.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ let append xs ys = xs @ ys

let prepend xs ys = xs @ ys

let concat = List.concat

let flatten = List.flatten

let equal ~eq xs ys = L.equal ~eq (L.rev xs) (L.rev ys)

let compare ~cmp xs ys = L.compare ~cmp (L.rev xs) (L.rev ys)
Expand Down
10 changes: 10 additions & 0 deletions test/TestBwdLabels.ml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ let test_prepend =
Q.Test.make ~count ~name:"prepend" Q.Gen.(pair (small_list int) (small_list int))
~print:Q.Print.(pair (list int) (list int))
(fun (xs, ys) -> B.prepend (of_list xs) ys = L.prepend xs ys)
let test_concat =
Q.Test.make ~count ~name:"concat" Q.Gen.(small_list (small_list int))
~print:Q.Print.(list (list int))
(fun ls -> to_list (B.concat (of_list (List.map ~f:of_list ls))) = L.concat ls)
let test_flatten =
Q.Test.make ~count ~name:"flatten" Q.Gen.(small_list (small_list int))
~print:Q.Print.(list (list int))
(fun ls -> to_list (B.flatten (of_list (List.map ~f:of_list ls))) = L.flatten ls)
let test_equal =
Q.Test.make ~count ~name:"equal"
Q.Gen.(triple (Q.fun2 Q.Observable.int Q.Observable.int bool) (small_list small_int) (small_list small_int))
Expand Down Expand Up @@ -301,6 +309,8 @@ let () =
test_nth_opt;
test_append;
test_prepend;
test_concat;
test_flatten;
test_equal;
test_compare;
test_iter;
Expand Down

0 comments on commit 28551a3

Please sign in to comment.