(assign script-name “core-020-utils.nydp”)

(dox-add-doc ‘if

'mac
'("with arguments a,         return a"
  "with arguments a b,       return b if a is true, otherwise nil"
  "with arguments a b c,     return b if a is true, otherwise return c"
  "with arguments a b c d,   return b if a is true, otherwise return d if c is true, otherwise nil"
  "with arguments a b c d e, return b if a is true, otherwise return d if c is true, otherwise e"
  "and so on for subsequent arguments")
'args
'(cond args
       (cond (cdr args)
             (cond (cddr args)
                   `(cond ,(car args) ,(cadr args) (if ,@(cddr args)))
                   `(cond ,(car args) ,(cadr args)))
             (car args))
       nil)
(dox/attrs (flow-control)))

(dox-add-doc ‘map

'def
'("transforms the list 'things by applying 'f to each item"
  "returns the resulting list")
'(f things)
'(if (pair? things)
     (cons (f (car things)) (map f (cdr things)))
     things
     (f things))
(dox/attrs (list-manipulation)))

(dox-add-doc ‘rev

'def
'("@things@ - the list to be reversed"
  "@last-cdr@ - (normally nil) - an item (atom, list, nil, anything) "
  "to be consed to the end of the reversed list.")
'(things)
'(def rev (things last-cdr) (loop (pair? things) ((fn nil (assign last-cdr (cons (car things) last-cdr)) (assign things (cdr things))))) last-cdr)
(dox/attrs (list-manipulation)))

(dox-add-doc ‘hash-cons

'def
'("push 'v onto the value for 'k in 'h")
'(h k v)
'(hash-set h k (cons v (hash-get h k)))
(dox/attrs (hash-manipulation)))

;; equivalent to (join-str “~prefix~joint~(car things)” joint (cdr things)) - except ;; ‘string-pieces hasn’t been defined yet, and if it were, it would be defined in terms of ;; ‘join-str, so it would be circular. ;; see ’joinstr for a more powerful and easier-to-use implementation of the same idea (def join-str (prefix joint things)

(chapter string-manipulation)
(if things
    (join-str (+ (to-string prefix)
                 joint
                 (to-string (car things)))
              joint
              (cdr things))
    prefix))

;; returns the ‘thing if the ’thing is present? ; otherwise nil ;; useful for compressing forms like ;; ;; (let thing (get-the-thing) ;; (if (present? thing) ;; (do-thing-stuff thing))) ;; ;; down to ;; ;; (aif (nb thing) (do-thing-stuff it)) ;; ;; ;; or, alternatively, compressing ;; ;; (let thing (get-the-thing) ;; (if (present? thing) ;; thing ;; (get-the-other-thing))) ;; ;; down to ;; ;; (or (nb (get-the-thing)) (get-the-other-thing)) ;; (def nb (thing) (if (present? thing) thing nil))