package sugar

  1. Overview
  2. Docs
val (>---------) : 'a result -> (error -> 'a result) -> 'a result

Combinator used to introduce an error handler block to "clean errors".

There's a secret message behind the form of this combinator. It has the same number of characters sufficient for the whole block in the next line. For example:

let program1 () =
 do_something ()
 >---------
 ( fun e ->
   return ()
 )

let program2 () =
 do_something ()
 >---------
 ( function
   e -> return ()
 )

So beyond the clean aesthetics similar to markdown, we are implying that a developer should never handle errors in an open anonymous function.

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

Combinator for map with semantic similar to bind

As its name sugests, this is an alias for the function map. Its intended use is to help integrate with functions that are not error aware.

For example, considere the function let double x = x + x in the code fragments bellow:

open Sugar.Option

let twenty =
  match Some 10 with
  | None -> None
  | Some n -> Some (double n)

 let using_bind_combinator =
  Some 10
  >>=
  ( fun n ->
    return (double n)
  )

let using_map_combinator =
  Some 10
  >>| double
val (<$>) : ('a -> 'b) -> 'a result -> 'b result
val (<*>) : ('a -> 'b) result -> 'a result -> 'b result
val (>>>) : 'a result -> 'b result Lazy.t -> 'b result

Ignore operator.

This is a type of semicolon combinator that can be used to ignore any result. It is supposed to be used with the keyword lazy as in >>>lazy. It can be used to replace the creation of an anonymous function to discard the previous value, like:

(* instead of writing *)
do_something ()
>>=
( fun _ ->
  do_something_else ()
)

(* you can write *)
( do_something ()      ) >>>lazy
( do_something_else () )

For more information, look at (>>).

val (>>) : unit result -> 'b result Lazy.t -> 'b result

A sequential semicolon combinator.

This operator is supposed to be used with the keyword lazy, as >>lazy. To reduce cognitive load, it's interesting to treat >>lazy as one word.

To chain monadic expressions that are not related to each other. Instead of writing code like:

puts "hello"
>>=
( fun () ->
  puts "monadic"
)
>>=
( fun () ->
  puts "world"
)

You can use these form:

( puts "hello"   ) >>lazy
( puts "monadic" ) >>lazy
( puts "world"   )

Or this alternative form that has exactly the structure as the example above:

( puts "hello"   )>>lazy(
  puts "monadic" )>>lazy(
  puts "world"
)

Notice that the expression on the left must be of type unit result. If you wish to ignore other type of results, use >>>lazy

OCaml

Innovation. Community. Security.