7.9 KiB
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 filestring-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 pairspairs
- 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 instancek
- a key compatible with given ldict
Returns #t
if given ld
contains given key k
.
(ldict-ref ld k [default])
ld
- a ldict instancek
- a compatible keydefault
- optional fallback value
Retrieves the value associated with given key in given dictionary. If the dictionary does not contain it and no default is given, an exception is raised. Otherwise the default value is returned in case of missing key.
(ldict-remove ld k)
ld
- a ldict instancek
- a compatible key
Returns a new dictionary with the record under given key removed. If
the dictionary does not contain the key k
, an error is raised.
(ldict-set ld k v)
ld
- a ldict instancek
- a compatible keyv
- a value to insert
Sets existing key k
to the new value v
or inserts it if it
is not already present in the dictionary ld
.
(ldict-keys ld)
ld
- a ldict instance
Returns the list of keys stored in given dictionary.
(ldict-map proc ld)
proc
- procedure accepting 1, 2 or 3 argumentsld
- a ldict instance
Returns a new dictionary of the same type with all values processed by given procedure.
If it accepts one argument, only the value is passed to it. If it accepts two values, the key and the value is passed to it. And lastly if it accepts three arguments, the key, value and numeric index (starting from 0) are passed to it.
In all cases the value the procedure returns is taken as the new value for given key.
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 #flst
- 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 toequal?
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 instanceel
- 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 instanceel
- 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 instanceel
- 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 convertcomparator
- equality comparison procedure, defaults toequal?
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 instancels2
- lset instance
Returns a new lset instance with all elements in both lset instances given.
(lset-intersect ls1 ls2)
ls1
- lset instancels2
- 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 repeatrep
- 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
.