Module Monads.ResultMonad

ResultMonad

An implementation of a monad "design pattern" for handling errors across functions, using the Result type.

val return : ?map_ok:('a -> 'a) -> 'a -> ('a, 'b) Stdlib.result

return ?map_ok x wraps the value x in an Ok result. If map_ok is provided, it applies the function f to x before wrapping it.

  • parameter map_ok

    a function to apply to the value before wrapping it in Ok.

  • parameter x

    the value to wrap.

  • returns

    the value x wrapped in an Ok result.

val fail : ?map_error:('a -> 'a) -> 'a -> ('b, 'a) Stdlib.result

fail ?map_error e wraps the value e in an Error result. If map_error is provided, it applies the function f to e before wrapping it in Error.

  • parameter map_error

    a function to apply to the value before wrapping it in Error.

  • parameter e

    the value to wrap in Error.

  • returns

    the value e wrapped in an Error result.

val bind : ('a, 'b) Stdlib.result -> ('a -> ('c, 'b) Stdlib.result) -> ('c, 'b) Stdlib.result

bind m f applies the function f to the value inside m if m is Ok, otherwise returns the same Error value. Note: This function doesn't accumulate errors.

  • parameter m

    the result to bind.

  • parameter f

    the function to apply to the value inside m.

  • returns

    the result of applying f to the value inside m.

val (>>=) : ('a, 'b) Stdlib.result -> ('a -> ('c, 'b) Stdlib.result) -> ('c, 'b) Stdlib.result

( >>= ) m f is an infix operator for bind.

val let* : ('a, 'b) Stdlib.result -> ('a -> ('c, 'b) Stdlib.result) -> ('c, 'b) Stdlib.result

( let* ) m f is a syntactic sugar for bind in monadic let binding.

val apply : ('a, 'b) Stdlib.result -> ('a -> 'c) -> ('c, 'b) Stdlib.result

apply m f applies the function f to the value inside m if m is Ok, and wraps the result in Ok.

  • parameter m

    the result to apply the function to.

  • parameter f

    the function to apply to the value inside m.

  • returns

    the result of applying f to the value inside m.

val (>>|) : ('a, 'b) Stdlib.result -> ('a -> 'c) -> ('c, 'b) Stdlib.result

( >>| ) m f is an infix operator for apply.

val let+ : ('a, 'b) Stdlib.result -> ('a -> 'c) -> ('c, 'b) Stdlib.result

( let+ ) m f is a syntactic sugar for apply in monadic let binding.

val compose : ('a, 'b) Stdlib.result -> ('a -> ('c, 'b) Stdlib.result) -> ('c -> ('d, 'b) Stdlib.result) -> ('d, 'b) Stdlib.result
val (>=>) : ('a, 'b) Stdlib.result -> ('a -> ('c, 'b) Stdlib.result) -> ('c -> ('d, 'b) Stdlib.result) -> ('d, 'b) Stdlib.result
val bind_error : ('a, 'b) Stdlib.result -> ('b -> ('a, 'c) Stdlib.result) -> ('a, 'c) Stdlib.result

bind_error m f applies the function f to the value inside m if m is Error, otherwise returns the Ok value.

  • parameter m

    the result to bind.

  • parameter f

    the function to apply to the value inside m.

  • returns

    the result of applying f to the value inside m.

val (>>!) : ('a, 'b) Stdlib.result -> ('b -> ('a, 'c) Stdlib.result) -> ('a, 'c) Stdlib.result

( >>! ) m f is an infix operator for bind_error.

val fold_left : ('a -> 'b -> ('a, 'c) Stdlib.result) -> 'a -> 'b list -> ('a, 'c) Stdlib.result

fold_left f acc l is a monadic fold left over the list l with the function f, threading an accumulator of type result.

  • parameter f

    the function to fold with.

  • parameter acc

    the initial accumulator.

  • parameter l

    the list to fold over.

  • returns

    the result of folding over the list l.

val fold_left2 : ('a -> 'b -> 'c -> ('a, 'd) Stdlib.result) -> 'a -> 'b list -> 'c list -> ('a, 'd) Stdlib.result

fold_left2 f acc l1 l2 folds the lists l1 and l2 from the left with the function f, threading an accumulator of type result.

  • parameter f

    the function to fold with.

  • parameter acc

    the initial accumulator.

  • parameter l1

    the first list to fold over.

  • parameter l2

    the second list to fold over.

  • returns

    the result of folding over the lists l1 and l2.

val fold_right : ('a -> 'b -> ('a, 'c) Stdlib.result) -> 'a -> 'b list -> ('a, 'c) Stdlib.result

fold_right f acc l folds the list l from the right with the function f, threading an accumulator of type result.

  • parameter f

    the function to fold with.

  • parameter acc

    the initial accumulator.

  • parameter l

    the list to fold over.

  • returns

    the result of folding over the list l.

val fold_right2 : ('a -> 'b -> 'c -> ('a, 'd) Stdlib.result) -> 'a -> 'b list -> 'c list -> ('a, 'd) Stdlib.result

fold_right2 f acc l1 l2 folds the lists l1 and l2 from the right with the function f, threading an accumulator of type result.

  • parameter f

    the function to fold with.

  • parameter acc

    the initial accumulator.

  • parameter l1

    the first list to fold over.

  • parameter l2

    the second list to fold over.

  • returns

    the result of folding over the lists l1 and l2.

val map : ('a -> ('b, 'c) Stdlib.result) -> 'a list -> ('b list, 'c) Stdlib.result

map f l maps the function f over the list l, returning a result of type result.

  • parameter f

    the function to map over the list.

  • parameter l

    the list to map the function over.

  • returns

    the result of mapping the function over the list.

val filter_map : ('a -> 'b option) -> 'a list -> ('b list, 'c) Stdlib.result

filter_map f l maps the function f over the list l, filtering out None values, and returning a result of type result.

  • parameter f

    the function to map and filter over the list.

  • parameter l

    the list to map the function over.

  • returns

    the result of mapping the function over the list.

val iter : ('a -> (unit, 'b) Stdlib.result) -> 'a list -> (unit, 'b) Stdlib.result

iter f l applies the function f to each element of the list l, returning a result of type result wrapping unit value.

  • parameter f

    the function to apply to each element of the list.

  • parameter l

    the list to apply the function to.

  • returns

    the result of applying the function to each element of the list.

val partition : ('a -> bool) -> 'a list -> ('a list * 'a list, 'b) Stdlib.result

partition f l partitions the list l into two lists based on the predicate f, returning a result of type result.

  • parameter f

    the predicate to partition the list with.

  • parameter l

    the list to partition.

  • returns

    the result of partitioning the list.

val partition_map : ('a -> ('b, 'c) Stdlib.Either.t) -> 'a list -> ('b list * 'c list, 'd) Stdlib.result

partition_map f l partitions the list l into two lists based on the function f which returns an Either type, returning a result of type result.

  • parameter f

    the function to partition the list with.

  • parameter l

    the list to partition.

  • returns

    the result of partitioning the list.

val for_all : ('a -> (bool, 'b) Stdlib.result) -> 'a list -> (bool, 'b) Stdlib.result

for_all f l checks if all elements of the list l satisfy the predicate f, returning a result of type result.

  • parameter f

    the predicate to check each element of the list.

  • parameter l

    the list to check.

  • returns

    the result of checking if all elements satisfy the predicate.