hackerbase/doc/utils.md

269 lines
6.5 KiB
Markdown

Utility Modules
===============
To ensure there are no external dependencies (including chicken eggs),
these modules re-implement any basic procedures which are required for
any algorithms used. And some advanced yet generic functionality as
well.
The modules are listed in alphabetical order.
### CSV
(import util-csv)
This module provides a very simple, incomplete and incorrect but fast
CSV loader.
(csv-parse filename
[#:separator #\;]
[#:string-delimiter #\"])
* ```separator``` - cell separator in CSV file
* ```string-delimiter``` - for introducing strings possibly with separators
Parses given CSV file and returns list of lists of strings
representing its contents.
(csv-split-header csv)
* ```csv``` - list of lists of strings
Splits given loaded CSV into two tables at the first empty row.
### Dictionary
(import util-dict)
This module implements a simple key/value dictionary using lists as
backend. All operations are O(n) with respect to time.
(make-ldict [equality?/pairs [pairs]])
* ```equality?/pairs``` - procedure or list of pairs
* ```pairs``` - list of pairs
Creates a new ldict with configurable equality procedure, optionally
populating it with initial data.
If only one argument is given, the procedure checks whether it is a
list of equality procedure and acts accordingly.
(ldict-empty? ld)
* ```ld``` - a ldict instance
Returns ```#t``` if given dictionary contains no keys.
(ldict-contains? ld k)
* ```ld``` - a ldict instance
* ```k``` - a key compatible with given ldict
Returns ```#t``` if given ```ld``` contains given key ```k```.
### IO
(import util-io)
Module implementing advanced I/O.
(read-lines/no-bom port)
* ```port``` - an input port
Reads lines using ```read-lines``` and if the first line contains
UTF-8 BOM, removes it.
(get-process-output-lines cmd)
* ```cmd``` - a string with the command
Returns a list of strings representing all the lines produced by
running the command given.
### List
(import util-list)
This module implements basic list functionality which is common in
most scheme implementations.
(filter pred? lst)
* ```pred?``` - procedure accepting any value and returning #t or #f
* ```lst``` - list to be filtered
Returns a list containing only elements matching given ```pred?```
predicate.
### Parser
(import util-parser)
This module contains common functions for both configuration and
member file parsers. All functions are UTF-8 aware.
(parser-preprocess-line line)
* ```line``` - a string with contents of one source line
If the input ```line``` contains the ```#``` character, the rest of
the line (including this character) is removed.
Any leading and trailing space is removed.
Returns a string representing the preprocessed line.
(parser-parse-line line)
* ```line``` - preprocessed line (string)
If the ```line``` is empty, returns ```#f```.
If the line contains only one token consisting of non-whitespace
characters before the first whitespace character (there is no
whitespace), returns a symbol created by interning the whole
```line```.
When the ```line``` contains whitespace character(s), it returns a
pair consisting of symbol created by interning the string of
non-whitespace characters before the first whitespace character and
the string with the rest of the line.
### Set (List)
(import util-set-list)
This module implements linear-time set with custom comparator.
(make-lset [comparator])
* ```comparator``` - comparison procedure, defaults to ```equal?```
Creates new lset with given comparator.
(lset-empty? ls)
* ```ls``` - lset instance
Returns ```#t``` if given lset contains no elements.
(lset-member? ls el)
* ```ls``` - lset instance
* ```el``` - element to check
Returns ```#t``` if given element ```el``` is contained in the lset
```ls``` using its equality comparator.
(lset-count ls)
* ```ls``` - lset instance
Returns the number of elements in given lset.
(lset-add ls [el ...])
* ```ls``` - lset instance
* ```el``` - element(s) to add
Adds given element(s) ```el``` to the lset instance ```ls``` returning
a new lset instance.
(lset-remove ls el)
* ```ls``` - lset instance
* ```el``` - element to remove
If given lset instance ```ls``` contains the element ```el```
provided, it is removed and the new lset is returned. If the element
is not contained in ```ls```, it is returned intact.
(list->lset lst [comparator])
* ```lst``` - list to convert
* ```comparator``` - equality comparison procedure, defaults to ```equal?```
Returns a new lset with the comparator provided containing all
elements in given list.
(lset->list ls)
* ```ls``` - lset instance
Returns the list of elements in given lset.
(lset-merge ls1 ls2)
* ```ls1``` - lset instance
* ```ls2``` - lset instance
Returns a new lset instance with all elements in both lset instances given.
(lset-intersect ls1 ls2)
* ```ls1``` - lset instance
* ```ls2``` - lset instance
Returns a new lset instance containing elements present both in
```ls1``` and ```ls2```.
(lset-subtract ls1 ls2)
Returns a new lset instance from ```ls1``` with all elements in
```ls2``` removed from it.
### String
(import util-string)
String manipulation functions which are used throughout other modules.
(string-repeat str rep)
* ```str``` - string to repeat
* ```rep``` - number of repeats
Returns a string created by repeating the string ```str``` exactly
```rep``` number of times.
(string-first+rest str)
* ```str``` - a string to split
Returns a pair of strings where the ```car``` of the pair is the first
token in the ```str``` given and ```cdr``` is a string with the
remainder with leading whitespace removed.
### Tag
(import util-tag)
A unifying module for compound data structures tagging.
(make-tag name)
* ```name``` - unquoted symbol to base the tag on
Creates a unique and collision free symbol to identify compound data
structures based on lists and pairs.
### Time
(import util-time)
Compatibility module for the differences between chicken scheme
version 5.2 and earlier and 5.3 and on.
(current-util-milliseconds)
Returns the number of milliseconds since process startup.
Under chicken scheme <5.3 it uses ```current-milliseconds``` and for
chicken scheme >=5.3 it uses ```current-process-milliseconds```.
Without this module compilation with ```current-milliseconds```
results in warnings on chicken scheme >=5.3 and compilation under
chicken scheme <5.3 results in error with
```current-process-milliseconds```.