# spotlib / lib / base.mli

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73``` ```(** Basic functinons. They are accessible via [open Spotlib.Spot] *) (** {6 Common types } *) type ('a, 'b) result = [ `Ok of 'a | `Error of 'b ] (** Result/Either monad *) (** {6 Function compositions and other operators } *) val ( ** ) : ('b -> 'c) -> ('a -> 'b) -> ('a -> 'c) (** Functional composition. Haskell's (.) Use [power] if you want to use the original [( ** )]. *) val ( *> ) : ('a -> 'b) -> ('b -> 'c) -> ('a -> 'c) (** Functional composition in swapped order. Haskell's (>>>) *) external power : float -> float -> float = "caml_power_float" "pow" "float" external (&) : ('a -> 'b) -> 'a -> 'b = "%apply" external (&~) : (f:'a -> 'b) -> 'a -> 'b = "%apply" (** Haskell's (\$) *) external (|>) : 'a -> ('a -> 'b) -> 'b = "%revapply" external (|!) : 'a -> ('a -> 'b) -> 'b = "%revapply" (** F#'s (|>). (|!) is Jane Street style. *) external id : 'a -> 'a = "%identity" external (!&) : _ -> unit = "%ignore" (** {6 Imperative operations } *) val imp : 'a -> ('a ref -> 'b) -> ('a * 'b) val imp_ : 'a -> ('a ref -> 'b) -> 'a (** IMPerative application tamed a bit functionally. A sort of. [imp init f] creates a reference of [init], then run the function [f] over this reference. It returns the final contents of the reference. *) val with_ref : 'a ref -> 'a -> (unit -> 'b) -> 'b (** [with_ref r v f] run a thunk [f] with setting [r := v] temporarily. [r] is set back to its original value after [f ()] finishes or raises an exceptoin. *) (** {6 Misc things } *) val sprintf : ('a, unit, string) format -> 'a (** [Printf.sprintf] is available without tying [Printf.] *) val (!%) : ('a, unit, string) format -> 'a (** [Printf.sprintf], prefix style. Bought from ITPL. *) val (!!%) : ('a, Format.formatter, unit) format -> 'a (** [Format.eprintf], prefix style. *) val memoize : ('c -> 'd) -> 'c -> 'd (** Memozation by hash *) val memoize_rec : (('a -> 'b) -> 'a -> 'b) -> 'a -> 'b (** Memozation by hash with fixed point. You can call memoized self inside: memoize_rec (fun f -> .... f v ....) w *) val time : ('a -> 'b) -> 'a -> 'b * float (** simple profiling *) ```