spotlib

Useful functions for OCaml programming used by @camlspotter
Library spotlib
Module Spotlib . Spot
include module type of struct include Base end
Common types
type ('a, 'b) poly_result = [
| `Ok of 'a
| `Error of 'b
]

Result/Either monad using polymorphic variant. Obsolete. Use the primitive type result instead.

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 (*<) : ( 'b -> 'c ) -> ( 'a -> 'b ) -> 'a -> 'c

Same as ( ** ).

val (*>) : ( 'a -> 'b ) -> ( 'b -> 'c ) -> 'a -> 'c

Functional composition in swapped order. Same as Haskell's (>>>)

val power : float -> float -> float

Replacement of ( ** )

val (&) : ( 'a -> 'b ) -> 'a -> 'b

Haskell's ($).

val (&~) : ( f:'a -> 'b ) -> 'a -> 'b

Haskell's ($), with label 'f'.

val id : 'a -> 'a
val (|-) : 'a -> ( 'a -> unit ) -> 'a

"tee". v |-- f is v but f v is run before v is returned

val flip : ( 'a -> 'b -> 'c ) -> 'b -> 'a -> 'c

flip of Haskell

val (~~) : ( 'a -> 'b ) -> f:'a -> 'b

Super flip of Haskell. The first argument is labeled and becomes commutative.

val flipf : ( 'a -> 'b ) -> f:'a -> 'b

Super flip of Haskell. The first argument is labeled and becomes commutative.

val flip2 : ( 'a -> 'b -> 'c -> 'd ) -> 'b -> 'c -> 'a -> 'd

flip f x2 x3 x1 = f x1 x2 x3

Imperative operations
val with_ref : 'a -> ( 'a ref -> 'b ) -> 'a * 'b
val with_ref_ : 'a -> ( 'a ref -> unit ) -> 'a

with_ref and with_ref_ runs a function with a reference with the specified initial value.

I/O
val with_oc : out_channel -> ( out_channel -> 'a ) -> 'a
val with_ic : in_channel -> ( in_channel -> 'a ) -> 'a
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_gen : ( ( 'a -> 'b ) -> 'a -> 'b ) -> ( 'a -> 'b ) * ( 'a, 'b ) Hashtbl.t

Generic memozation by hash with fixed point. You can call memoized self inside:

memoize_gen (fun self -> .... self v ....) f

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 self -> .... self v ....) f

val time : ( 'a -> 'b ) -> 'a -> 'b * float

Simple profiling. Returns seconds. If f a raises an exception e, then time f a raises e too.

val (+=) : int ref -> int -> unit
val (-=) : int ref -> int -> unit
val find_by_iter : ( ( 'a -> unit ) -> 'collection -> unit ) -> ( 'a -> bool ) -> 'collection -> 'a option

find the first element where predicate holds by iteration find_by_iter iterator predicate colleciton

val find_in_tree : ( 'a -> 'a list ) -> ( 'a -> 'res option ) -> 'a -> 'res option

find_in_tree get_subs p a visits a and finds the first sub node in which p holds. get_subs returns the sub nodes of a node.

val loop : ( 'a -> [< `Continue of 'a | `Break of 'b ] ) -> 'a -> 'b

Keep repeating a function until a final result is producted.

Comparison
val compare_on : ( 'a -> 'b ) -> 'a -> 'a -> int

compare_on f a b compares a and b not by themselves but by f a and f b.

val rev_compare_on : ( 'a -> 'b ) -> 'a -> 'a -> int

rev_compare_on f a b = - (compare_on f a b)

val add_if_not_mem : 'a -> 'a list ref -> [> `AlreadyIn | `NewlyAdded ]

add_if_not_mem adds a to asref if not (List.mem a !asref) then return `NewlyAdded. Otherwise it returns `AlreadIn and asref is not modified.

String <-> bytes conversions

val (!<$) : string -> bytes
val (!>$) : bytes -> string
module Monad = Monad
module Option = Option
include module type of struct include Option.Stdlib end
val from_Some : 'a option -> 'a

may raise Invalid_argument

val (//) : 'a Option.t -> 'a -> 'a

Same as default but the default value is always evaluated

module Weaktbl = Weaktbl
module Hashset = Hashset
module File = File
module Comopt = Comopt
module Overload = Overload
module Mtypes = Mtypes
module Stream = SpotStream
module Poly_result = Poly_result
module Result = Vresult
include module type of struct include Result.Stdlib end
val ok : 'a -> ( 'a, 'error ) result
val ng : 'error -> ( 'a, 'error ) result

No Good

val from_Ok : ( 'a, 'error ) Result.t -> 'a

Raises IsError when not Ok _

val result : ( 'a -> 'b ) -> ( 'c -> 'b ) -> ( 'a, 'c ) Result.t -> 'b

Haskell's either

val at_Error : ( 'err -> 'a ) -> ( 'a, 'err ) Result.t -> 'a

at_Error = result id

module Tuple = Tuple
include module type of struct include Tuple.Stdlib end
val map_fst : ( 'a -> 'b ) -> ('a * 'c) -> 'b * 'c
val map_snd : ( 'a -> 'b ) -> ('c * 'a) -> 'c * 'b
module Exn = Exn
include module type of struct include Exn.Stdlib end
Printf style errors
val failwithf : ( 'a, unit, string, 'b ) format4 -> 'a
val invalid_argf : ( 'a, unit, string, 'b ) format4 -> 'a
val raisef : ( string -> exn ) -> ( 'a, unit, string, 'b ) format4 -> 'a
Exception handling
exception Finally of exn * exn
val protect : ( 'a -> 'b ) -> 'a -> finally:( 'a -> unit ) -> 'b

It raises an exception Finally (org, final) when finally cannot recover the error.

val protect_with : ( 'a -> 'b ) -> 'a -> finally:( 'a -> 'c ) -> 'b * 'c

It raises an exception Finally (org, final) when finally cannot recover the error.

val catch : ( 'a -> 'b ) -> 'a -> ( 'b, [> `Exn of exn ] ) result
val try_ignore : ( 'a -> unit ) -> 'a -> unit
val try_or : ( 'a -> 'b ) -> ( 'a -> 'b ) -> 'a -> 'b
val try_bool : ( 'a -> unit ) -> 'a -> bool

true at success

val protect_ : ( unit -> 'b ) -> finally:( unit -> unit ) -> 'b
val protect_with_ : ( unit -> 'b ) -> finally:( unit -> 'c ) -> 'b * 'c
val catch_ : ( unit -> 'b ) -> ( 'b, [> `Exn of exn ] ) result
val try_ignore_ : ( unit -> unit ) -> unit
val try_or_ : ( unit -> 'b ) -> ( unit -> 'b ) -> 'b
val try_bool_ : ( unit -> unit ) -> bool
val tee : ( 'a -> 'b ) -> 'a -> handler:( exn -> unit ) -> 'b

tee f v ~handler. If f v raises an exception e, handler e is executed, then e is reraised.

type 'a return = {
return : 'jump. 'a -> 'jump;
}
val with_return : ( 'a return -> 'a ) -> 'a
module Temporal = Temporal
module Year = Temporal.Year
module Date = Temporal.Date
module Time = Temporal.Time
module Datetime = Temporal.Datetime
module IntRange = IntRange
module List : sig ... end
include module type of struct include Xlist.Stdlib end
val (--) : int -> int -> int list

same as from_to. [f--t = [f..t]]

val (+::=) : 'a list ref -> 'a -> unit

Same as accum

module Array : sig ... end
module Format : sig ... end
module Hashtbl : sig ... end
module String : sig ... end
include module type of struct include Xstring.Stdlib end
val chop_eols : string -> string
module Bytes : sig ... end
module Lazy : sig ... end
module Filename : sig ... end
include module type of struct include Xfilename.Stdlib end
val (^/) : string -> string -> string

Filename concatenation. If the second argument is absolute, the first is ignored and the second is just returened.

"hello" ^/ "world" = "hello/world" "hello" ^/ "/world" = "/world"

module Filepath = Filepath
module Unix : sig ... end
include module type of struct include Xunix.Stdlib end
val timed_message : string -> ( 'a -> 'b ) -> 'a -> 'b

timed_message mes f returns a function as same as f but with printing computation times to stderr.

module Printf : sig ... end
module Sys : sig ... end
module Set = Xset
module StringSet : sig ... end
module IntSet : sig ... end
module Int64 : sig ... end
module Printexc : sig ... end
module Obj : sig ... end
module URL = URL
module Gc : sig ... end
module UniqueID = UniqueID
module Once = Once
module Shell = Shell
module Lexing : sig ... end
module Command = Command