Module CCOptSource

Options

Sourcetype +'a t = 'a option
Sourceval map : ('a -> 'b) -> 'a t -> 'b t

Transform the element inside, if any.

Sourceval map_or : default:'b -> ('a -> 'b) -> 'a t -> 'b

map_or ~default f o is f x if o = Some x, default otherwise.

  • since 0.16
Sourceval map_lazy : (unit -> 'b) -> ('a -> 'b) -> 'a t -> 'b

map_lazy default_fn f o if f o if o = Some x, default_fn () otherwise.

  • since 1.2
Sourceval is_some : _ t -> bool

is_some (Some x) returns true otherwise it returns false.

Sourceval is_none : _ t -> bool

is_none None returns true otherwise it returns false.

  • since 0.11
Sourceval compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int

Compare two options, using custom comparators for the value. None is always assumed to be less than Some _.

Sourceval equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool

Test for equality between option types using a custom equality predicat.

Sourceval return : 'a -> 'a t

Monadic return, that is return x = Some x.

Sourceval (>|=) : 'a t -> ('a -> 'b) -> 'b t

Infix version of map.

Sourceval (>>=) : 'a t -> ('a -> 'b t) -> 'b t

Monadic bind.

Sourceval flat_map : ('a -> 'b t) -> 'a t -> 'b t

Flip version of >>=.

Sourceval map2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t

map2 f o1 o2 maps 'a option and 'b option to a 'c option using f.

Sourceval iter : ('a -> unit) -> 'a t -> unit

Iterate on 0 or 1 element.

Sourceval fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a

Fold on 0 or 1 element.

Sourceval filter : ('a -> bool) -> 'a t -> 'a t

Filter on 0 or 1 element.

  • since 0.5
Sourceval if_ : ('a -> bool) -> 'a -> 'a option

if_ f x is Some x if f x, None otherwise.

  • since 0.17
Sourceval exists : ('a -> bool) -> 'a t -> bool

Return true iff there exists an element for which the provided function evaluates to true.

  • since 0.17
Sourceval for_all : ('a -> bool) -> 'a t -> bool

Return true iff the provided function evaluates to true for all elements.

  • since 0.17
Sourceval get_or : default:'a -> 'a t -> 'a

get_or ~default o extracts the value from o, or returns default if o = None.

  • since 0.18
Sourceval value : 'a t -> default:'a -> 'a

Similar to the stdlib's Option.value and to get_or.

  • since 2.8
Sourceval get_exn : 'a t -> 'a

Open the option, possibly failing if it is None.

Sourceval get_lazy : (unit -> 'a) -> 'a t -> 'a

get_lazy default_fn x unwraps x, but if x = None it returns default_fn () instead.

  • since 0.6.1
Sourceval sequence_l : 'a t list -> 'a list t

sequence_l [x1; x2; ...; xn] returns Some [y1;y2;...;yn] if every xi is Some yi. Otherwise, if the list contains at least one None, the result is None.

Sourceval wrap : ?handler:(exn -> bool) -> ('a -> 'b) -> 'a -> 'b option

wrap f x calls f x and returns Some y if f x = y. If f x raises any exception, the result is None. This can be useful to wrap functions such as Map.S.find.

  • parameter handler

    the exception handler, which returns true if the exception is to be caught.

Sourceval wrap2 : ?handler:(exn -> bool) -> ('a -> 'b -> 'c) -> 'a -> 'b -> 'c option

wrap2 f x y is similar to wrap but for binary functions.

Applicative

Sourceval pure : 'a -> 'a t

Alias to return.

Sourceval (<*>) : ('a -> 'b) t -> 'a t -> 'b t

f <*> (Some x) returns Some (f x) and f <*> None returns None.

Sourceval (<$>) : ('a -> 'b) -> 'a t -> 'b t

Like map.

Alternatives

Sourceval or_ : else_:'a t -> 'a t -> 'a t

or_ ~else_ a is a if a is Some _, else_ otherwise.

  • since 1.2
Sourceval or_lazy : else_:(unit -> 'a t) -> 'a t -> 'a t

or_lazy ~else_ a is a if a is Some _, else_ () otherwise.

  • since 1.2
Sourceval (<+>) : 'a t -> 'a t -> 'a t

a <+> b is a if a is Some _, b otherwise.

Sourceval choice : 'a t list -> 'a t

choice returns the first non-None element of the list, or None.

Sourceval flatten : 'a t t -> 'a t

flatten transforms Some x into x.

  • since 2.2
Sourceval return_if : bool -> 'a -> 'a t

Apply Some or None depending on a boolean. More precisely, return_if false x is None, and return_if true x is Some x.

  • since 2.2

Infix Operators

Sourcemodule Infix : sig ... end

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
include CCShimsMkLet_.S with type 'a t_let := 'a option
Sourceval (let+) : 'a option -> ('a -> 'b) -> 'b option
Sourceval (and+) : 'a option -> 'b option -> ('a * 'b) option
Sourceval (let*) : 'a option -> ('a -> 'b option) -> 'b option
Sourceval (and*) : 'a option -> 'b option -> ('a * 'b) option

Conversion and IO

Sourceval to_list : 'a t -> 'a list
Sourceval of_list : 'a list -> 'a t

Head of list, or None.

Sourceval to_result : 'e -> 'a t -> ('a, 'e) result
  • since 1.2
Sourceval to_result_lazy : (unit -> 'e) -> 'a t -> ('a, 'e) result
  • since 1.2
Sourceval of_result : ('a, _) result -> 'a t
  • since 1.2
Sourcetype 'a sequence = ('a -> unit) -> unit
Sourcetype 'a gen = unit -> 'a option
Sourcetype 'a printer = Format.formatter -> 'a -> unit
Sourcetype 'a random_gen = Random.State.t -> 'a
Sourceval random : 'a random_gen -> 'a t random_gen
Sourceval choice_seq : 'a t sequence -> 'a t

choice_seq s is similar to choice, but works on sequences. It returns the first Some x occurring in s, or None otherwise.

  • since 0.13
Sourceval to_gen : 'a t -> 'a gen
Sourceval to_std_seq : 'a t -> 'a Seq.t

Same as Stdlib.Option.to_seq

  • since 2.8
Sourceval to_iter : 'a t -> 'a sequence

Returns an internal iterator, like in the library Iter.

  • since 2.8
Sourceval to_seq : 'a t -> 'a sequence
  • deprecated use to_iter or to_std_seq
Sourceval pp : 'a printer -> 'a t printer