Source

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 [( ** )].
*)
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 Printf style errors } *)

val failwithf    : ('a, unit, string, 'b) format4 -> 'a
val invalid_argf : ('a, unit, string, 'b) format4 -> 'a

(** {6 Exception handling } *)

exception Finally of exn * exn

val protect    : f:('a -> 'b) -> 'a -> finally: ('a -> unit) -> 'b
val catch      : f:('a -> 'b) -> 'a -> [> ('b, exn) result ]
val try_ignore : f:('a -> unit) -> 'a -> unit

val try_bool   : f:('a -> 'b) -> 'a -> bool
(** [true] at success *)

val with_final : 'a -> ('a -> 'b) -> ('a -> unit) -> 'b
(** [with_final v f final] returns [f v] but runs [final v]
    before returning the result. 
    
    Exception handling:
    [final v] is called even if [f v] raises an exception.

    If [final v] raises an exception [e], [with_final v f final] does:
    - Re-throws [e] when [f v] returns normally.
    - Ignore [e] and re-throws [e'] when [f v] raises an exception [e'].

*)

(** {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 Option } *)
val from_Some : 'a option -> 'a

(** {6 Result } *)
val from_Ok : ('e -> exn) -> [< ('a, 'e) Result.t ] -> 'a
val result : ('a -> 'b) -> ('c -> 'b) -> [< ('a, 'c) Result.t] -> 'b

(** {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 memoize : ('c -> 'd) -> 'c -> 'd
(** Memozation by hash *)

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.