Module Toolkit


module Toolkit: sig .. end
Some small functions and shortcuts which I find convenient.
Author(s): Gamall Wednesday Ida


Miscellaneous

val (%) : ('a -> 'b) -> ('c -> 'a) -> 'c -> 'b
Functions composition operator
val toggle : bool Pervasives.ref -> unit
Toggle an option on and off

About option and references types...

exception Empty_option
val opt_get : 'a option -> 'a
Get the contents of an option, fail if None
val opt_getd : 'a -> 'a option -> 'a
Get the contents of an option, use a default value if None
module Global: sig .. end
Modified Global module from Extlib library, Copyright (C) 2003 Nicolas Cannasse
type 'a global = 'a Global.t 

Functions about lists, strings and files

val aggreg_args : string list -> string
Concatenate a list of arguments, putting spaces as separators
val is_blank_char : char -> bool
Is this a blank ?
val trim : string -> string
Trim white spaces

Note: The four next functions are taken from the Str library. The reason why they are not in String is beyond me...

I have modified the first two so that they always return the string instead of raising exceptions, which is what I need most of the time.

val first_chars : string -> int -> string
first_chars s n returns the first n characters of s, and s itself if n is too big
val last_chars : string -> int -> string
last_chars s n returns the last n characters of s, and s itself if n is too big
val string_before : string -> int -> string
string_before s n returns the substring of all characters of s that precede position n (excluding the character at position n).
val string_after : string -> int -> string
string_after s n returns the substring of all characters of s that follow position n (including the character at position n).
val snippet : string -> int -> string
Get an extract of a string, up to n+3 chars, the extra three being the "..." if the string is trunctated
val subb : string -> int -> int -> string
Similar in purpose to String.sub, extracts between two positions, inclusively
val pluralise : string -> int -> string
Add an 's' at the end of the string if n > 2
val map_tr : ('a -> 'b) -> 'a list -> 'b list
Tail recursive version of map
val no_newline : string -> string
Get rid of trailing \n
val lines_of_file : ?f:(string -> string) -> string -> string list
Input a file into a list, tail-recursively, transforming each line through f (not entirely by me)
val run_on_file : (string -> 'a) -> string -> unit
Run f for each line in the file

Some shortcuts


Printing

val pl : string -> unit
Print line to stdout
val pe : string -> unit
Print line to stderr
val ps : string -> unit
Print string to stdout
val pf : ('a, Pervasives.out_channel, unit) Pervasives.format -> 'a
= Printf.printf: formated printing to stdout
val spf : ('a, unit, string) Pervasives.format -> 'a
= Printf.sprintf: formated printing to string
val epf : ('a, Pervasives.out_channel, unit) Pervasives.format -> 'a
= Printf.eprintf: formated printing to stderr
val fpf : Pervasives.out_channel ->
('a, Pervasives.out_channel, unit) Pervasives.format -> 'a
= Printf.fprintf: formated printing to output channel
val va : ('a, unit, string) Pervasives.format -> 'a
= spf: formated printing to stdout

Lists, strings and such

val tl : 'a list -> 'a list
Get the tail of list
val hd : 'a list -> 'a
Get the head of a list
val sof : float -> string
= string_of_float
val fos : string -> float
= float_of_string
val soi : int -> string
= string_of_int
val ios : string -> int
= int_of_string
val iof : float -> int
= int_of_float
val foi : int -> float
= float_of_int
val soff : float -> string
Get a string representation of a float, but without the dot if it's an integer
val iosd : string -> string -> int
ios with debug instructions
val iosz : string -> int
ios with zero in case of failure
val strlen : string -> int
Get length of a string
val listlen : 'a list -> int
Get length of a list
val arrlen : 'a array -> int
Get length of an array
val break_string : string -> unit
DEBUG: display codes for each char in a string

standard channels

val stdinc : Pervasives.in_channel
stdin as input channel
val stdoutc : Pervasives.out_channel
stdout as output channel
val stdinf : Unix.file_descr
stdin as a file descriptor
val stdoutf : Unix.file_descr
stdout as a file descriptor

Unix tricks and sockets

val hue : ('a -> 'b) -> 'a -> 'b
= Unix.handle_unix_error
val open_out_append : string -> Pervasives.out_channel
Open for writing at end of file
val getport : string -> int
Get a valid port number (that is, between 0 and 65535) from a string, or fail
val pidd : string -> unit
DEBUG: quickly display what processus I'm in
val cpipe : ?nonblock:bool -> unit -> Pervasives.in_channel * Pervasives.out_channel
Setup a pipe with channels instead of file_descr. Optionally specify if reading is non-blocking. Blocking by default.
val pdfork : ?nb:bool ->
(Pervasives.in_channel -> Pervasives.out_channel -> unit) ->
Pervasives.in_channel * Pervasives.out_channel
Piped double fork, leaving no zombie process behind.
Returns couple of channels (channel reading from the son, channel writing to the son)
nb : is reading non-blocking ?
f : function the son will be executing. It takes two arguments: the channels to reading from the son and writing to the son, respectively.
val peek_line : Pervasives.in_channel -> string option
Do an input_line for non-blocking channels. None is returned if nothing is there.
val string_of_sockaddr : Unix.sockaddr -> string
Display a human-legible internet address in IP:port format
val repeat_pattern : string -> int -> string
Build a string by repeating the same pattern n times. Useful for drawing terminal lines etc
val separator_double_line : string
standard separator for terminal output: double
val separator_line : string
standard separator for terminal output: single
val separator_solid_line : string
standard separator for terminal output: underscore

Command-line

val av : string array
Array of command-line arguments
val ac : int
Number of command-line arguments
val al : string list
List of command-line arguments
val bad_args : string -> unit
Incorrect command-line arguments routine: prints all arguments, insults user, and fails.