# (retropikzel batteries) Batteries is meta library depending on and exporting: - (retropikzel assert) - (retropikzel string-util) - (retropikzel number-util) - (retropikzel iteration-util) - (retropikzel alist-util) - (retropikzel read-util) - (retropikzel write-util) - (retropikzel shell-util) - (retropikzel memoize) - (retropikzel vector2) - (retropikzel vector3) # (retropikzel assert) (**assert** *check* *val* *err-msg*) If *val* fails *check* then throw error with *err-msg* and *val*. Assert is not meant to be used for input checking, it is a tool for: - Checking for situations that should never happen - Communicating intentions through showing what should never happen Assert level can be adjusted by importing different libraries - (retropikzel assert) - Error when assert fails - (retropikzel assert warning) - Show warning when assert fails - (retropikzel assert ignore) - Ignore all assertion - Tries to eliminate all assert code and have no impact at all # (retropikzel string-util) Inspired by, and plainly stolen from other languages. :) [https://docs.python.org/3/library/string.html](https://docs.python.org/3/library/string.html) **\*ascii-letter\*** The concatenation of the \*ascii-lowercase\* and \*ascii-uppercase\* constants described below. **\*ascii-lowercase\*** The lowercase letters "abcdefghijklmnopqrstuvwxyz". This value is not locale-dependent and will not change. **\*ascii-uppercase\** The uppercase letters "ABCDEFGHIJKLMNOPQRSTUVWXYZ". This value is not locale-dependent and will not change. **\*digits\*** The string "0123456789". **\*hexdigits\*** The string "0123456789abcdefABCDEF". **\*octdigits\*** The string "01234567". **\*punctuation\*** String of ASCII characters which are considered punctuation characters in the C locale: !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~. (**any->string** *val*) Takes in any kind of *val* and returns it as string as outputted by display procedure. (**string-replace** *str* *replace* *replace-with*) (**string-replace** *str* (*replace* *replace-with*) ...) Replaces the *replace* string with *replace-with* string on on all occurances inside string *str*. Or replaces all given (*replace* *replace-with*) pairs. Examples: (string-replace "foo123bar" "123" "456") > "foo456bar" (string-replace "foo123bar-no" '("123" "456") '("no" "yes")) > "foo456bar-yes" (**string-split** *str* [*char*]) Split string *str* at character *char*. If *char* is not given **#\space** is used. (**string-format** *str* *vals*) *str* should be string containing keys of values surrounded by curly brackets. *vals* should be list of lists containing key and value. Examples: (string-format "Hello {name}, I count {n} parenthesis" '((name "Schemer") (n 7))) > "Hello Schemer, I count 7 parenthesis" (**string-capitalize** *str*) Capitalizes the first character of given string *str*. Examples: (string-capitalize "hello") > "Hello" (**string-center** *str* *len* . *char*) Center aligns the given string *str* to given *len*. If character *char* is given it is used, otherwise space is used. Examples: (string-center "hello" 15) > " hello " (**string-ends-with?** *str* *end-str*) Returns #t if given str ends with end-str. #f otherwise. Examples: (string-ends-with? "hello" \#o) > #t (string-ends-with? "hello" \#e) > #f (**string-expand-tabs** *str* *size*) Expand any **#\tab** in string *str* with spaces of count *size*. Examples: (string-expand-tabs "\thello" 2) > " hello" (string-expand-tabs "\thello\t" 4) > " hello " (**string-shorten** *str* *width* [*placeholder*]) # (retropikzel number-util) **\*m-pi\*** Constant 3.14159265358979323846. Taken from C header math.h from GNU lib C version 2.41. (**++** *n*) Increment given number *n* by one. (**--** *n*) Decrease given number *n* by one. # (retropikzel iteration-util) (**list-enumerate** *lst*) Returns a list of pairs with item index as car. (list-enumerate '(a b c)) > ((0 . a) (1 . b) (2 . c)) (**vector-enumerate** *lst*) Returns a vector of pairs with item index as car. (vector-enumerate #(a b c)) > #((0 . a) (1 . b) (2 . c)) (**list-indexes** *lst*) Returns a list of indexes, same length as give *lst*. (list-indexes '(a b c)) > (0 1 2) (**vector-indexes** *vect*) Returns a vector of indexes, same length as give *vect*. (vector-indexes #(a b c)) > #(0 1 2) # (retropikzel alist-util) (**vars->alist** *var* ...) Turn given vars into alist with keys same names as vars and vars as values. (let ((a 1) (b 2) (c 3) (d 4) (e 5)) (vars->alist a b c d e))) > ((a . 1) (b . 2) (c . 3) (d . 4) (e . 5)) (**alist-get** *key* *lst*) Gets the given cdr of pair with *key* as car out of *lst* or errors if the *key* is not found. (**alist-gets** *key* ... *lst*) Same as **alist-get** but gets list of multiple keys. (**alist-merge** *lst1* *lst2*) Merge given alists removing duplicates and preseving the last value of pair. (alist-merge '((a 1) (b 2) (c 3)) '((d 4) (e 5)))) > ((a 1) (b 2) (c 3) (d 4) (e 5)) (alist-merge '((a 1) (b 2) (c 3) (d x)) '((d 4) (e 5)))) > ((a 1) (b 2) (c 3) (d 4) (e 5)) (alist-merge '((a 1) (b 2) (c 3) (d x)) '((d y) (d 4) (e 5)))) > ((a 1) (b 2) (c 3) (d 4) (e 5)) (**alist-merge!** *lst1* *lst2*) Same as **alist-merge** but result is set! into *lst1* variable. # (retropikzel read-util) (**slurp** *path*) Read whole contents of file in *path* as text. (**read-selection** *prompt* *choice* ...) Prompt user to choose from number of choices. User is expected to enter a number from 1 to count of choices. (read-selection "Choose:" a b c) > Choose: > 1) a > 2) b > 3) c 2 > b # (retropikzel write-util) (**spit** *path* *text* . *append?*) Output the *text* into a file in given *path*. If *append?* is given and is **#t** then text is added to the end. Otherwise the file is deleted and rewritten. Note that when appending whole content of a file is read, and then then that is written into file and *text* after that. So this is propably not the best tool for example logs. (**display-line** *msg* [*port*]) Display given string *msg* and newline after that. If *port* is not given then (current-output-port) is used. (**write-line** *msg* [*port*]) Write given string *msg* and newline after that. If *port* is not given then (current-output-port) is used. # (retropikzel shell-util) (**$** *name/index* [*default*]) If *name/index* is symbol and environment variable with same name as symbol as a string is set returns it, otherwise returns empty string. If *name/index* is number and argument with that index is passed returns it, otherwise returns empty string. In either of previous cases the *default* is given, it is returned instead of empty string. **$#** Variable that holds the number of arguments passed from command line. (**$|** *exp1* *exp2*) Capture *exp1* output and use the output as input for *exp2*. (write ($| (write "Hello") (read))) > "Hello" (**$>** *exp* *path*) Display the (current-output-port) content of *exp* into file in *path*. If file does not exist it is created, if it exists it is overwritten. (**$<** *exp* *path*) Call exp with procedure (with-input-from-file *path* ...). # (retropikzel memoize) (**define-memoized** *name* *args* *body* ...) Define memoized procedure with name *name* and arguments *args*, *body* is the body of the procedure. (define x 1) (define-memoized add (y) (+ 1 x y)) (add 1) > 3 (two 2) > 4 (set! x 10) (add 1) > 3 (two 2) > 4 # (retropikzel vector2) (**make-vector2** *x* *y* [fill]) Returns a newly allocated vector2 of width of *x* and height of *y* elements. If *fill* argument is given then each element is initialized to *fill*. Otherwise the initial contents of each element is unspecified. Example: (make-vector2 2 2 #f) > #(#(#(#f #f) #(#f #f)) #(#(#f #f) #(#f #f))) (**vector2?** *obj*) Returns **#t** if *obj* is a vector2, otherwise returns **#f**. (**vector2-width** *vec2*) Returns the width(length of x) of *vec2*. (**vector2-height** *vec2*) Returns the height(length of y) of *vec2*. (**vector2-ref** *vec3* *x* *y*) It is an error if *x* and *y* are not valid indexes of vector2. The vector2-ref procedure returns the contents of element *x* *y* of vector2. (**vector2-set!** *vec2* *x* *y* *obj*) It is an error if *x* and *y* are not valid indexes of vector2. The vector2-set! procedure stores *obj* in element *x* *y* of the vector2. (**vector2->list** *vec2*) The vector2->list procedure returns a newly allocated list of the objects contained in the elements of *vec2*. (**list->vector2** *lst*) The list->vector2 procedure returns a newly created vector2 initialized to the elements of the list *lst*. (**vector2-copy** *vec2*) Returns a newly allocated copy of the elements of the given vector2 *vec2*. (**vector2-fill!** *vec2* *fill*) The vector2-fill! procedure stores *fill* in the elements of vector2 *vec2*. # (retropikzel vector3) (**make-vector3** *x* *y* *z* [fill]) Returns a newly allocated vector3 of width of *x*, height of *y* and depth of *z* elements. If *fill* argument is given then each element is initialized to *fill*. Otherwise the initial contents of each element is unspecified. Example: (make-vector3 2 2 2 #f) > #(#(#(#f #f) #(#f #f)) #(#(#f #f) #(#f #f))) (**vector3?** *obj*) Returns **#t** if *obj* is a vector3, otherwise returns **#f**. (**vector3-width** *vec3*) Returns the width(length of x) of *vec3*. (**vector3-height** *vec3*) Returns the height(length of y) of *vec3*. (**vector3-depth** *vec3*) Returns the depth(length of z) of *vec3*. (**vector3-ref** *vec3* *x* *y* *z*) It is an error if *x*, *y* and *z* are not valid indexes of vector3. The vector3-ref procedure returns the contents of element *x* *y* *z* of vector3. (**vector3-set!** *vec3* *x* *y* *z* *obj*) It is an error if *x*, *y* and *z* are not valid indexes of vector3. The vector3-set! procedure stores *obj* in element *x* *y* *z* of the vector3. (**vector3->list** *vec3*) The vector3->list procedure returns a newly allocated list of the objects contained in the elements of *vec3*. (**list->vector3** *lst*) The list->vector3 procedure returns a newly created vector3 initialized to the elements of the list *lst*. (**vector3-copy** *vec3*) Returns a newly allocated copy of the elements of the given vector3 *vec3*. (**vector3-fill!** *vec3* *fill*) The vector3-fill! procedure stores *fill* in the elements of vector3 *vec3*.