mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-08 12:15:32 -05:00
safe version of fold_right
This commit is contained in:
parent
3127f046de
commit
fd88f10216
2 changed files with 23 additions and 0 deletions
|
|
@ -61,6 +61,26 @@ let append l1 l2 =
|
||||||
|
|
||||||
let (@) = append
|
let (@) = append
|
||||||
|
|
||||||
|
let fold_right f l acc =
|
||||||
|
let rec direct i f l acc = match l with
|
||||||
|
| [] -> acc
|
||||||
|
| _ when i=0 -> safe f (List.rev l) acc
|
||||||
|
| x::l' ->
|
||||||
|
let acc = direct (i-1) f l' acc in
|
||||||
|
f x acc
|
||||||
|
and safe f l acc = match l with
|
||||||
|
| [] -> acc
|
||||||
|
| x::l' ->
|
||||||
|
let acc = f x acc in
|
||||||
|
safe f l' acc
|
||||||
|
in
|
||||||
|
direct _direct_depth f l acc
|
||||||
|
|
||||||
|
(*$T
|
||||||
|
fold_right (+) (1 -- 1_000_000) 0 = \
|
||||||
|
List.fold_left (+) 0 (1 -- 1_000_000)
|
||||||
|
*)
|
||||||
|
|
||||||
let rec compare f l1 l2 = match l1, l2 with
|
let rec compare f l1 l2 = match l1, l2 with
|
||||||
| [], [] -> 0
|
| [], [] -> 0
|
||||||
| _, [] -> 1
|
| _, [] -> 1
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,9 @@ val append : 'a t -> 'a t -> 'a t
|
||||||
|
|
||||||
val (@) : 'a t -> 'a t -> 'a t
|
val (@) : 'a t -> 'a t -> 'a t
|
||||||
|
|
||||||
|
val fold_right : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b
|
||||||
|
(** Safe version of [fold_right] *)
|
||||||
|
|
||||||
val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
|
val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
|
||||||
|
|
||||||
val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
|
val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue