Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename append and prepend to append_list and prepend_list, add Bwd.append #32

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/BwdLabels.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ let[@inline] init ~len ~f = BwdNoLabels.init len f

let append = BwdNoLabels.append

let prepend = BwdNoLabels.prepend
let append_list = BwdNoLabels.append_list

let prepend_list = BwdNoLabels.prepend_list

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

Expand Down
9 changes: 5 additions & 4 deletions src/BwdLabels.mli
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
{ul
{- New:
{ul
{- {!val:prepend} was added as the {i forward} version of {!val:append}.}
{- {!val:prepend_list} and {!val:append_list} for performing "textual order yoga".}
{- {!val:to_list} and {!val:of_list} were added for conversions between standard lists.}
{- {!module:Infix} was added for the infix notation.}}}
{- Changed:
{ul
{- [cons] was replaced by {!val:snoc}.}
{- {!val:append} was replaced by a new version that performs the "textual order yoga".}
{- 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:
Expand Down Expand Up @@ -52,9 +51,11 @@ 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 append : 'a t -> 'a t -> 'a t

val prepend : 'a t -> 'a list -> 'a list
val append_list : 'a t -> 'a list -> 'a t

val prepend_list : 'a t -> 'a list -> 'a list

(** {1 Comparison} *)

Expand Down
27 changes: 16 additions & 11 deletions src/BwdNoLabels.ml
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,20 @@ let init len f =
in
go 0 (len - 1)

let append xs ys =
let[@tail_mod_cons] rec append xs ys =
match ys with
| Emp -> xs
| Snoc (ys, y) -> Snoc(append xs ys, y)

let append_list xs ys =
let rec go =
function
| xs, [] -> xs
| xs, y :: ys ->
(go[@tailcall]) (Snoc (xs, y), ys)
in go (xs, ys)

let prepend xs ys =
let prepend_list xs ys =
let rec go =
function
| Emp, ys -> ys
Expand Down Expand Up @@ -163,7 +168,7 @@ let fold_left f init =
let fold_right_map f xs init =
let rec go xs init ys =
match xs with
| Emp -> init, append Emp ys
| Emp -> init, append_list Emp ys
| Snoc (xs, x) ->
let init, y = f x init in
(go[@tailcall]) xs init (y :: ys)
Expand Down Expand Up @@ -339,7 +344,7 @@ let filteri f =
let partition f xs =
let rec go xs ys zs =
match xs with
| Emp -> append Emp ys, append Emp zs
| Emp -> append_list Emp ys, append_list Emp zs
| Snoc (xs, x) ->
if f x then
(go[@tailcall]) xs (x :: ys) zs
Expand All @@ -350,7 +355,7 @@ let partition f xs =
let partition_map f xs =
let rec go xs ys zs =
match xs with
| Emp -> append Emp ys, append Emp zs
| Emp -> append_list Emp ys, append_list Emp zs
| Snoc (xs, x) ->
match f x with
| Either.Left y ->
Expand All @@ -376,19 +381,19 @@ let combine xs ys =
in go (xs, ys)

let to_list xs =
prepend xs []
prepend_list xs []

let of_list xs =
append Emp xs
append_list Emp xs

module Infix =
struct
let (<:) = snoc
let (<@) = append
let (@>) = prepend
let (<@) = append_list
let (@>) = prepend_list
let (#<) = snoc
let (<><) = append
let (<>>) = prepend
let (<><) = append_list
let (<>>) = prepend_list
end

module Notation = Infix
9 changes: 5 additions & 4 deletions src/BwdNoLabels.mli
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
{ul
{- New:
{ul
{- {!val:prepend} was added as the {i forward} version of {!val:append}.}
{- {!val:prepend_list} and {!val:append_list} for performing "textual order yoga".}
{- {!val:to_list} and {!val:of_list} were added for conversions between standard lists.}
{- {!module:Infix} was added for the infix notation.}}}
{- Changed:
{ul
{- [cons] was replaced by {!val:snoc}.}
{- {!val:append} was replaced by a new version that performs the "textual order yoga".}
{- 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:
Expand Down Expand Up @@ -52,9 +51,11 @@ val nth_opt : 'a t -> int -> 'a option

val init : int -> (int -> 'a) -> 'a t

val append : 'a t -> 'a list -> 'a t
val append : 'a t -> 'a t -> 'a t

val prepend : 'a t -> 'a list -> 'a list
val append_list : 'a t -> 'a list -> 'a t

val prepend_list : 'a t -> 'a list -> 'a list

(** {1 Comparison} *)

Expand Down
15 changes: 10 additions & 5 deletions test/TestBwdLabels.ml
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,16 @@ let test_init =
(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))
(fun (xs, ys) -> to_list (B.append (of_list xs) (of_list ys)) = L.append xs ys)
let test_append_list =
Q.Test.make ~count ~name:"append_list" Q.Gen.(pair (small_list int) (small_list int))
~print:Q.Print.(pair (list int) (list int))
(fun (xs, ys) -> to_list (B.append (of_list xs) ys) = L.append xs ys)
let test_prepend =
Q.Test.make ~count ~name:"prepend" Q.Gen.(pair (small_list int) (small_list int))
(fun (xs, ys) -> to_list (B.append_list (of_list xs) ys) = L.append xs ys)
let test_prepend_list =
Q.Test.make ~count ~name:"prepend_list" 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)
(fun (xs, ys) -> B.prepend_list (of_list xs) ys = L.prepend xs ys)
let test_equal =
Q.Test.make ~count ~name:"equal"
Q.Gen.(triple (Q.fun2 Q.Observable.int Q.Observable.int bool) (small_list small_nat) (small_list small_nat))
Expand Down Expand Up @@ -322,7 +326,8 @@ let () =
test_nth_opt;
test_init;
test_append;
test_prepend;
test_append_list;
test_prepend_list;
test_equal;
test_compare;
test_iter;
Expand Down
Loading