package containers
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=1e7992cb2e59c0d2290d1b6c3a31531b3f310be6170b8ef3dde17ccd876b5b79
sha512=bb124e69ad0690f88393e18eee499be07761e767593558867aab32f643466b43258ced503170b154ca3b56dbd68987abd6d9438cf473707ec9866511589a5b84
doc/containers/CCFun/index.html
Module CCFun
Source
Basic operations on Functions
Combinators
id
is the identity function. For any argument x
, id x
is x
.
const c
is a function that always returns the value c
. For any argument x
, (const c) x
is 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
.
negate p
is the negation of the predicate function p
. For any argument x
, (negate p) x
is not (p x)
.
Exception handling
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.
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.
and_p f g x
is (f x) && (g x)
. Produces a predicate which is a conjunction of the two predicates.
or_p f g x
is (f x) || (g x)
. Produces a predicate which is a disjunction of the two predicates.
compose f g x
is g (f x)
. Composition.
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]
.
curry f x y
is f (x,y)
. Convert a function which accepts a pair of arguments into a function which accepts two arguments.
uncurry f (x,y)
is f x y
. Convert a function which accepts a two arguments into a function which accepts a pair of arguments.
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 Stdlib.compare
Lexicographic combination of comparison functions.
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.
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.
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.
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).
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.
with_return f
is f return
, where return
is a function that can be invoked to exit the scope early.
For example:
let find_array arr x =
let@ return = with_return in
for i = 0 to Array.length arr-1 do
if arr.(i) = x then return i;
done;
-1
Infix
Infix operators.
include module type of Infix
(f %> g) x
or (%>) f g x
is g (f x)
. Alias to compose
.
(f % g) x
or (%) f g x
is f (g x)
. Mathematical composition.
let@ x = foo in bar
is the equivalent of foo @@ fun x -> bar
. It can be very useful for resource management, alongside with protect
.
x ||> f
is f (fst x) (snd x)
like ||>
but for tuples of size 3
Monad
Functions with a fixed domain are monads in their codomain.