Module Sugar (.ml)


module Sugar: sig .. end
Basic shortcuts and syntactic sugar.


Pipelines of functions



Mnemonic (unix-reminiscent) and funny aliases for function composition.
val (||) : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c
Make a pipeline of functions. It's simply the function composition, of course, not a real pipeline (stream composition), so keep an eye on memory allocation.
val (=>) : 'a -> ('a -> 'b) -> 'b
Put a value into a pipelines of functions

Example

  "ls" => ( SSys.run || fst || SString.toList ) ;; 

Working with tuples

val identity : 'a -> 'a
val id : 'a -> 'a
val (@@) : ('a -> 'b) -> ('c -> 'd) -> 'a * 'c -> 'b * 'd
val curry : ('a * 'b -> 'c) -> 'a -> 'b -> 'c
val uncurry : ('a -> 'b -> 'c) -> 'a * 'b -> 'c

Example. Definition of the pattern matching function :

 let match_pattern pat str : bool = 
    let string_match (p,s) = (Str.string_match p s 0) in

    (pat,str) => ( (Str.regexp@@identity) || string_match ) ;; 
In this example, pat is given to Str.regexp and str is given to identity.

Example. Remove the element with the given index:

 let rmindex l i = 
    (l,l) => ((identity@@indexes) || (uncurry List.combine) || (List.filter (fun (x,j) ->j<>i)) || List.split || fst) ;;


Default for 'a option


val (|=>) : 'a option -> 'a -> 'a
Short cut for quickly open an optional value.

Example

# let x = Some 4 ;;
val x : int option = Some 4
Now you can write:
# x |=> 7 ;;
  : int = 4
instead of write:
 match x with Some v -> v | None -> 7;; 

val is_none : 'a option -> bool
Return true iff the given optional value is None
val is_some : 'a option -> bool
Return true iff the given optional value is not None

Other shortcuts



Other recurrent shortcuts.
val nothing : unit -> unit
Equivalent to function () -> ().
val skip : unit
Equivalent to ().
type identifier = int 
Equivalent to int.
type 'a filter = 'a -> 'a 
A filter is function that transform elements in the same domain (endo-function).
val raise_when_none : ?msg:string -> 'a option -> 'a
Open an 'a option value and return its content if any. Otherwise fail with the given error message or a generic one if not provided.