[sledge] Add List.fold_diagonal

Reviewed By: ngorogiannis

Differential Revision: D28907802

fbshipit-source-id: 57b7b81c0
master
Josh Berdine 4 years ago committed by Facebook GitHub Bot
parent abe8ba847a
commit a6f3e15cec

@ -61,6 +61,15 @@ let fold_right xs init ~f = fold_right ~f ~init xs
let reduce xs ~f =
match xs with [] -> None | x :: xs -> Some (fold ~f xs x)
let fold_diagonal xs init ~f =
let rec fold_diagonal_ z = function
| [] -> z
| x :: ys ->
let z = fold ~f:(fun y z -> f x y z) ys z in
fold_diagonal_ z ys
in
fold_diagonal_ init xs
let fold_map xs init ~f =
Pair.swap (fold_map ~f:(fun s x -> Pair.swap (f x s)) ~init xs)

@ -95,6 +95,7 @@ val fold : 'a list -> 's -> f:('a -> 's -> 's) -> 's
val fold_left : 'a list -> 's -> f:('s -> 'a -> 's) -> 's
val fold_right : 'a list -> 's -> f:('a -> 's -> 's) -> 's
val reduce : 'a t -> f:('a -> 'a -> 'a) -> 'a option
val fold_diagonal : 'a list -> 's -> f:('a -> 'a -> 's -> 's) -> 's
val fold_map : 'a t -> 's -> f:('a -> 's -> 'b * 's) -> 'b t * 's
val fold2_exn : 'a t -> 'b t -> 's -> f:('a -> 'b -> 's -> 's) -> 's

Loading…
Cancel
Save