package batteries

  1. Overview
  2. Docs
A community-maintained standard library extension

Install

Dune Dependency

Authors

Maintainers

Sources

v3.9.0.tar.gz
md5=ea26b5c72e6731e59d856626049cca4d
sha512=55975b62c26f6db77433a3ac31f97af609fc6789bb62ac38b267249c78fd44ff37fe81901f1cf560857b9493a6046dd37b0d1c0234c66bd59e52843aac3ce6cb

doc/batteries.unthreaded/BatOption/index.html

Module BatOptionSource

Functions for the option type.

Options are an Ocaml standard type that can be either None (undefined) or Some x where x can be any value. Options are widely used in Ocaml to represent undefined values (a little like NULL in C, but in a type and memory safe way). This module adds some functions for working with options.

  • author Nicolas Cannasse
  • author David Teller
Sourcetype 'a t = 'a option
Sourceval some : 'a -> 'a option

some x returns Some x.

  • since 2.2.0
Sourceval may : ('a -> unit) -> 'a option -> unit

may f (Some x) calls f x and may f None does nothing.

Sourceval map : ('a -> 'b) -> 'a option -> 'b option

map f (Some x) returns Some (f x) and map f None returns None.

Sourceval bind : 'a option -> ('a -> 'b option) -> 'b option

bind (Some x) f returns f x and bind None f returns None.

@example "Our functions return option types. Compose them to propagate None."

  let pick_long case =
    try
      Some (List.find (fun data -> List.length data > 1000) case)
    with Not_found -> None
  let last_null data = List.rindex_of 0 data
  let interesting_positions dataset =
    List.filter_map
      (fun case -> Option.bind last_null (pick_long case))
      dataset
Sourceval apply : ('a -> 'a) option -> 'a -> 'a

apply None x returns x and apply (Some f) x returns f x

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

filter f None returns None, filter f (Some x) returns Some x if f x is true, and None otherwise.

Sourceval default : 'a -> 'a option -> 'a

default x (Some v) returns v and default x None returns x.

Sourceval (|?) : 'a option -> 'a -> 'a

Like default, with the arguments reversed. None |? 10 returns 10, while Some "foo" |? "bar" returns "foo".

Note This operator does not short circuit like ( || ) and ( && ). Both arguments will be evaluated.

  • since 2.0
Sourceval default_delayed : (unit -> 'a) -> 'a option -> 'a

Like default, but the default value is passed as a thunk that is only computed if needed.

  • since 2.1
Sourceval map_default : ('a -> 'b) -> 'b -> 'a option -> 'b

map_default f x (Some v) returns f v and map_default f x None returns x.

Sourceval map_default_delayed : ('a -> 'b) -> (unit -> 'b) -> 'a option -> 'b

Like map_default, but the default value is passed as a thunk that is only computed if needed.

  • since 2.1
Sourceval is_none : 'a option -> bool

is_none None returns true otherwise it returns false.

Sourceval is_some : 'a option -> bool

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

Sourceval get : 'a option -> 'a

get (Some x) returns x.

Sourceval get_exn : 'a option -> exn -> 'a

get_exn (Some x) e returns x and get_exn None e raises e.

Sourceval compare : ?cmp:('a -> 'a -> int) -> 'a option -> 'a option -> int

Compare two options, possibly using custom comparators for the value. None is always assumed to be less than Some _. The parameter cmp defaults to Pervasives.compare.

Sourceval eq : ?eq:('a -> 'a -> bool) -> 'a option -> 'a option -> bool

Test for equality between option types, possibly using a custom equality predicate. The parameter eq defaults to Pervasives.(=).

  • since 1.4.0
Sourceval enum : 'a option -> 'a BatEnum.t

enum (Some x) returns the singleton x, while enum None returns the empty enumeration.

Sourceval of_enum : 'a BatEnum.t -> 'a option

of_enum e consumes the first element of e, if it exists, and returns Some e. If e is empty, return None.

The Option Monad

Sourcemodule Monad : sig ... end

This module provides everything needed to write and execute computations in the Option monad.

Boilerplate code

Sourceval ord : 'a BatOrd.ord -> 'a option BatOrd.ord

Comparison between optional values

  • since 2.2.0

Printing

Sourceval print : ('a BatInnerIO.output -> 'b -> unit) -> 'a BatInnerIO.output -> 'b t -> unit
Sourcemodule Labels : sig ... end

Operations on options, with labels.

Sourcemodule Infix : sig ... end
OCaml

Innovation. Community. Security.