spotlib / lib / base.mli

(** 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 I/O } *)

val with_oc : out_channel -> (out_channel -> 'a) -> 'a

(** {6 Misc things } *)

val sprintf : ('a, unit, string) format -> 'a
(** [Printf.sprintf] is available without tying [Printf.] *)

val ksprintf : (string -> 'a) -> ('b, unit, string, 'a) format4 -> 'b
(** [Printf.ksprintf] 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 *)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.