containers

A modular, clean and powerful extension of the OCaml standard library
back to documentation root
Legend:
Library
Module
Module type
Parameter
Class
Class type
Library containers
Module CCFun

Basic Functions

include module type of CCShimsFun_
include module type of Fun

Combinators

val id : 'a -> 'a

id is the identity function. For any argument x, id x is x.

val const : 'a -> _ -> 'a

const c is a function that always returns the value c. For any argument x, (const c) x is c.

val flip : ( 'a -> 'b -> 'c ) -> 'b -> 'a -> 'c

flip f reverses the argument order of the binary function f. For any arguments x and y, (flip f) x y is f y x.

val negate : ( 'a -> bool ) -> 'a -> bool

negate p is the negation of the predicate function p. For any argument x, (negate p) x is not (p x).

Exception handling

val protect : finally:( unit -> unit ) -> ( unit -> 'a ) -> 'a

protect ~finally work invokes work () and then finally () before work () returns with its value or an exception. In the latter case the exception is re-raised after finally (). If finally () raises an exception, then the exception Finally_raised is raised instead.

protect can be used to enforce local invariants whether work () returns normally or raises an exception. However, it does not protect against unexpected exceptions raised inside finally () such as Stdlib.Out_of_memory, Stdlib.Stack_overflow, or asynchronous exceptions raised by signal handlers (e.g. Sys.Break).

Note: It is a programming error if other kinds of exceptions are raised by finally, as any exception raised in work () will be lost in the event of a Finally_raised exception. Therefore, one should make sure to handle those inside the finally.

exception Finally_raised of exn

Finally_raised exn is raised by protect ~finally work when finally raises an exception exn. This exception denotes either an unexpected exception or a programming error. As a general rule, one should not catch a Finally_raised exception except as part of a catch-all handler.

val (|>) : 'a -> ( 'a -> 'b ) -> 'b

A 'pipe' operator. x |> f is the same as f x.

val compose : ( 'a -> 'b ) -> ( 'b -> 'c ) -> 'a -> 'c

Composition. compose f g x is g (f x).

val compose_binop : ( 'a -> 'b ) -> ( 'b -> 'b -> 'c ) -> 'a -> 'a -> 'c

compose_binop f g is fun x y -> g (f x) (f y). Example (partial order): List.sort (compose_binop fst CCInt.compare) [1, true; 2, false; 1, false].

  • since 0.6
val (%>) : ( 'a -> 'b ) -> ( 'b -> 'c ) -> 'a -> 'c

Alias to compose.

val (@@) : ( 'a -> 'b ) -> 'a -> 'b

f @@ x is the same as f x, but right-associative.

  • since 0.5
val curry : ( ('a * 'b) -> 'c ) -> 'a -> 'b -> 'c

Convert a function which accepts a pair of arguments into a function which accepts two arguments. curry f x y is f (x,y).

val uncurry : ( 'a -> 'b -> 'c ) -> ('a * 'b) -> 'c

Convert a function which accepts a two arguments into a function which accepts a pair of arguments. uncurry f (x,y) is f x y.

val tap : ( 'a -> _ ) -> 'a -> 'a

tap f x evaluates f x, discards it, then returns x. Useful in a pipeline, for instance:

CCArray.(1 -- 10)
|> tap CCArray.shuffle
|> tap @@ CCArray.sort Pervasives.compare
val (%) : ( 'b -> 'c ) -> ( 'a -> 'b ) -> 'a -> 'c

Mathematical composition. (%) f g x is f (g x).

val lexicographic : ( 'a -> 'a -> int ) -> ( 'a -> 'a -> int ) -> 'a -> 'a -> int

Lexicographic combination of comparison functions.

val finally : h:( unit -> _ ) -> f:( unit -> 'a ) -> 'a

finally h f calls f () and returns its result. If it raises, the same exception is raised; in any case, h () is called after f () terminates. If h () raises an exception, then this exception will be passed on and any exception that may have been raised by f () is lost.

val finally1 : h:( unit -> _ ) -> ( 'a -> 'b ) -> 'a -> 'b

finally1 ~h f x is the same as f x, but after the computation, h () is called whether f x rose an exception or not. If h () raises an exception, then this exception will be passed on and any exception that may have been raised by f () is lost.

  • since 0.16
val finally2 : h:( unit -> _ ) -> ( 'a -> 'b -> 'c ) -> 'a -> 'b -> 'c

finally2 ~h f x y is the same as f x y, but after the computation, h () is called whether f x y rose an exception or not. If h () raises an exception, then this exception will be passed on and any exception that may have been raised by f () is lost.

  • since 0.16
val opaque_identity : 'a -> 'a

opaque_identity x is like x, but prevents Flambda from using x's definition for optimizing it. (flambda is an optimization/inlining pass in OCaml >= 4.03).

  • since 0.18
val iterate : int -> ( 'a -> 'a ) -> 'a -> 'a

iterate n f is f iterated n times. That is to say, iterate 0 f x is x, iterate 1 f x is f x, iterate 2 f x is f (f x), etc.

  • since 2.1

Monad

Functions with a fixed domain are monads in their codomain.

module Monad (X : sig ... end) : sig ... end