diff --git a/.merlin b/.merlin index 68e740a..4648f95 100644 --- a/.merlin +++ b/.merlin @@ -4,7 +4,5 @@ S tests/ B _build/src B _build/tests/ B _build/bench/ -PKG oUnit qcheck -PKG benchmark -FLG -safe-string -FLG -w+a-4-44-48-60@8 +PKG oUnit qcheck result benchmark +FLG -safe-string -w+a-4-44-48-60@8 diff --git a/_oasis b/_oasis index d0a407a..ce3ec32 100644 --- a/_oasis +++ b/_oasis @@ -30,7 +30,7 @@ Flag bigarray Library "sequence" Path: src/ Modules: Sequence, SequenceLabels - BuildDepends: bytes + BuildDepends: bytes, result Library "invert" Path: src/invert diff --git a/opam b/opam index 36beb29..37ede30 100644 --- a/opam +++ b/opam @@ -27,6 +27,7 @@ remove: [ depends: [ "ocamlfind" "base-bytes" + "result" "ocamlbuild" {build} "qcheck" {test} "qtest" {test} diff --git a/src/Sequence.ml b/src/Sequence.ml index 1ed134b..2def117 100644 --- a/src/Sequence.ml +++ b/src/Sequence.ml @@ -230,6 +230,24 @@ let intersperse elem seq k = |> OUnit.assert_equal [1;0;2;0;3;0;4;0;5;0] *) +let keep_some seq k = + seq + (function + | Some x -> k x + | None -> ()) + +let keep_ok seq k = + seq + (function + | Result.Ok x -> k x + | Result.Error _ -> ()) + +let keep_error seq k = + seq + (function + | Result.Error x -> k x + | Result.Ok _ -> ()) + (** Mutable unrolled list to serve as intermediate storage *) module MList = struct type 'a node = diff --git a/src/Sequence.mli b/src/Sequence.mli index 5a09e41..b6a2fdb 100644 --- a/src/Sequence.mli +++ b/src/Sequence.mli @@ -231,6 +231,19 @@ val filter_count : ('a -> bool) -> 'a t -> int val intersperse : 'a -> 'a t -> 'a t (** Insert the single element between every element of the sequence *) +val keep_some : 'a option t -> 'a t +(** [filter_some l] retains only elements of the form [Some x]. + Same as [filter_map (fun x->x)] + @since NEXT_RELEASE *) + +val keep_ok : ('a, _) Result.result t -> 'a t +(** [keep_ok l] retains only elements of the form [Ok x]. + @since NEXT_RELEASE *) + +val keep_error : (_, 'e) Result.result t -> 'e t +(** [keep_error l] retains only elements of the form [Error x]. + @since NEXT_RELEASE *) + (** {2 Caching} *) val persistent : 'a t -> 'a t diff --git a/src/SequenceLabels.mli b/src/SequenceLabels.mli index 31af7d1..809c054 100644 --- a/src/SequenceLabels.mli +++ b/src/SequenceLabels.mli @@ -204,6 +204,19 @@ val filter_count : f:('a -> bool) -> 'a t -> int val intersperse : x:'a -> 'a t -> 'a t (** Insert the single element between every element of the sequence *) +val keep_some : 'a option t -> 'a t +(** [filter_some l] retains only elements of the form [Some x]. + Same as [filter_map (fun x->x)] + @since NEXT_RELEASE *) + +val keep_ok : ('a, _) Result.result t -> 'a t +(** [keep_ok l] retains only elements of the form [Ok x]. + @since NEXT_RELEASE *) + +val keep_error : (_, 'e) Result.result t -> 'e t +(** [keep_error l] retains only elements of the form [Error x]. + @since NEXT_RELEASE *) + (** {2 Caching} *) val persistent : 'a t -> 'a t