(chapter-start 'nydp-core "essential functions for getting anything done") (def joinstr (txt . things) ; flatten 'things into a single list (ie unnest lists) ; convert each item to a string ; return a single string which is the concatenation of each ; stringified item, with given 'txt inserted in between ; each item (let joinables (flatten things) (apply + (to-string (car joinables)) (flatten (zip (map (fn (_) txt) (cdr joinables)) (map to-string (cdr joinables))))))) (def iso (x y) (or (eq? x y) (and (pair? x) (pair? y) (iso (car x) (car y)) (iso (cdr x) (cdr y))))) (def x1 (thing) thing) (def sym? (arg) (isa 'symbol arg)) (def string? (arg) (isa 'string arg)) (mac just (arg) arg) (def quotify (arg) `(quote ,arg)) (def len (things) ; return the length of 'things where 'things may be nil, a string, list or hash ; length of nil is zero, length of hash is number of keys, length of string ; is number of characters, length of list is number of direct items - no recursive counting (chapter list-manipulation) (chapter string-manipulation) (chapter hash-manipulation) (if (no things) 0 (pair? things) (list-length things) (string? things) (string-length things) (hash? things) (list-length:hash-keys things) nil)) (assign dynamics (hash)) (mac dynamic (name) ; creates a dynamic variable. (hash-set dynamics name t) (let with-mac-name (sym "w/~name") (w/uniq prev `(do (mac ,with-mac-name (new-value . body) (w/uniq result `(let ,',prev (hash-get (thread-locals) ',',name) (hash-set (thread-locals) ',',name ,new-value) (let ,result (do ,@body) (hash-set (thread-locals) ',',name ,',prev) ,result)))) (def ,name () (hash-get (thread-locals) ',name)))))) (mac mapx (things x expr) ; a macro wrapper for 'map ; 'things is a list, 'x is the name of a variable, and 'expr ; is evaluated and collected for each 'x in 'things (chapter list-manipulation) `(map (fn (,x) ,expr) ,things)) (def atom? (thing) ; 't if 'thing is not a list or a hash (chapter nydp-core) (and thing (!pair? thing) (!hash? thing))) (def empty? (things) ; t if it's nil or an empty list, string, or hash ; nil otherwise (chapter list-manipulation) (chapter string-manipulation) (chapter hash-manipulation) (let l (len things) (and l (eq? l 0)))) (def present? (thing) ; t if it's a symbol or number, or a non-empty string, list or hash ; nil otherwise (chapter list-manipulation) (chapter string-manipulation) (chapter hash-manipulation) (!empty? thing))