mlisp/stdlib/stdlib.lsp

289 lines
7.3 KiB
Common Lisp

; not a bool
(defn not (x) (if x nil #t))
(defn is-pos? (n) (> n 0))
(defn is-neg? (n) (< n 0))
(defn neg (n) (- 0 n))
(defn dec (n) (- n 1))
(defn inc (n) (+ n 1))
(defn string-ltrim (str)
(string-rltrim str " \n\r\t" "ltrim"))
(defn string-rtrim (str)
(string-rltrim str " \n\r\t" "rtrim"))
(defn string-trim (str)
(string-rltrim str " \n\r\t" "trim"))
(defn string-rpad (str length pad_char)
(string-pad str length pad_char "rpad"))
(defn string-lpad (str length pad_char)
(string-pad str length pad_char "lpad"))
(defn string-upcase (str)
(string-case str "upper"))
(defn string-downcase (str)
(string-case str "lower"))
(defn string-cmp-ic (a b)
(string-cmp (string-upcase a) (string-upcase b)))
(defn string-join (lst sep)
(do
(def rslt "")
(def i 0)
(for e lst
(if (= i 0)
(do (def rslt (display e)) (def i 1))
(def rslt (+ rslt (display sep) (display e)))
))
rslt
))
(defn itok (ascii) (sprintf "%c" (list ascii)))
(defn ktoi (char) (int (sprintf "%d" (list char))))
; pause for interval of seconds
(defn sleep (time)
(system-cmd (+ "sleep " (string time))))
(defn third (l) (index l 2))
(defn fourth (l) (index l 3))
(defn fifth (l) (index l 4))
(defn sixth (l) (index l 5))
(defn seventh (l) (index l 6))
(defn eight (l) (index l 7))
(defn nineth (l) (index l 8))
(defn tenth (l) (index l 9))
(defn nth (i l) (index l (- i 1)))
(defn list? (e) (= (type e) "list"))
(defn empty-list? (e) (and (list? e) (= (len e) 0)))
(defn string? (e) (= (type e) "string"))
(defn int? (e) (= (type e) "int"))
(defn float? (e) (= (type e) "float"))
(defn nil? (e) (= (type e) "nil"))
(defn true? (e) (= (type e) "#t"))
(defn function? (e) (= (type e) "function"))
(defmacro unless (test v)
(list 'if (list 'not test) v))
(defmacro dotimes (v n body)
(list 'for v '(range 0 (eval n))
body
))
(defmacro until (cnd body)
(list 'while '(not (eval cnd))
body
))
; return 1 when list contains item otherwise nil
(defn member (lst itm)
(do
(if (list? lst)
(do
(def found_index -1)
(def i 0)
(def lst_len (len lst))
(while (and (< i lst_len) (= found_index -1))
(if (= itm (index lst i))
(set! found_index i)
(set! i (+ i 1))
))
(if (!= -1 found_index)
#t
nil)
)
nil)
))
(defn make-list-of (size val)
(do
(def lst '())
(def i 0)
(while (< i size)
(def lst (push lst val))
(def i (inc i)))
lst
))
(defn make-list (size)
(make-list-of size nil))
(defn uniq (lst)
(do
(def rslt '())
(def i 0)
(def lst_len (len lst))
(while (< i lst_len)
(def e (index lst i))
(if (not (member rslt e))
(def rslt (push rslt e)))
(def i (inc i)))
rslt
))
(defn flatten (lst)
(do
(def rslt '())
(for e lst
(if (= (type e) "list")
(do
(for ee (flatten e)
(def rslt (push rslt ee))))
(def rslt (push rslt e))
))
rslt
))
(defn take (lst n)
(if (> (len lst) n)
(map (lambda (i) (index lst i)) (range 0 n))
lst))
(defn concat-lists (seq1 seq2)
(do (def l seq1)
(dotimes i (len seq2)
(set! l (push l (index seq2 i))))
))
(defn quick-sort-by (l cmp)
(if (<= (len l) 1)
l
(do
(def pivot (first l))
(+
(quick-sort-by (filter (lambda (n) (cmp pivot n)) l) cmp)
(list pivot)
(quick-sort-by (tail (filter (lambda (n) (not (cmp pivot n))) l)) cmp)
))
))
(defn quick-sort (l)
(quick-sort-by l (lambda (a b) (> a b))))
(defn quick-sort-reverse (l)
(quick-sort-by l (lambda (a b) (> b a))))
(defn start-of-day (datetime) (str-to-date (+ (date-to-str datetime "%Y-%m-%d") " " "00:00:00") "%Y-%m-%d %H:%M:%S"))
(defn end-of-day (datetime) (str-to-date (+ (date-to-str datetime "%Y-%m-%d") " " "23:59:59") "%Y-%m-%d %H:%M:%S"))
(defn start-of-month (datetime) (str-to-date (+ (date-to-str datetime "%Y-%m") "-01 00:00:00") "%Y-%m-%d %H:%M:%S"))
(defn start-of-next-month (datetime) (date-add (start-of-month datetime) 1 "month"))
(defn end-of-next-month (datetime) (date-add (end-of-month datetime) 1 "month"))
(defn end-of-month (datetime) (date-add (date-add (start-of-month datetime) 1 "month") -1 "second"))
(defn start-of-prev-month (datetime) (date-add (start-of-month datetime) -1 "month"))
(defn end-of-prev-month (datetime) (date-add (end-of-month datetime) -1 "month"))
(defn start-of-year (datetime) (str-to-date (+ (date-to-str datetime "%Y") "-01-01 00:00:00") "%Y-%m-%d %H:%M:%S"))
(defn end-of-year (datetime) (str-to-date (+ (date-to-str datetime "%Y") "-12-31 23:59:59") "%Y-%m-%d %H:%M:%S"))
(defn decode-universal-time (time) (map (lambda (x) (int x)) (string-split (date-to-str time "%S %M %H %d %m %Y %w") "\s+")))
; from list of lists creates csv string
; (print (make-csv '(("r1c1" "r1c2") ("r2c1" "r2c2"))))
(defn make-csv (csv_list)
(do
(def rows_str "")
(def r 0)
(for row csv_list
(def cols_str "")
(def c 0)
(for col row
(if (= c 0)
(def cols_str (display col))
(def cols_str (+ cols_str "," (display col))))
(def c (inc c)))
(if (= r 0)
(def rows_str cols_str)
(def rows_str (+ rows_str "\n" cols_str)))
(def r (inc r)))
rows_str
))
; looks into lst's elements and when there is a list with first item with name, it returns the _second_ element of that list
(defn find-val-in-list (lst name)
(do
(def found_val nil)
(def i 0)
(def list_len (len lst))
(while (and (>= i 0) (< i list_len))
(def e (index lst i))
(set! i (inc i))
(if (= (type e) "list")
(if (= (type (first e)) "list")
(do (set! found_val (find-val-in-list (first e) name))
(if (!= found_val nil) (set! i -1))
)
; else
(do
(if (= name (first e))
(do
(set! found_val (second e)) (set! i -1)
)
; else
(if (= (type (second e)) "list")
(do (set! found_val (find-val-in-list (second e) name))
(if (!= found_val nil)
(set! i -1)
)
)
)
)
)
)
)
; else
(if (= name e)
(do (set! found_val (index lst (inc i))) (set! i -1))
)
)
found_val
))
(defn min (l)
(if (or (not l) (= (len l) 0))
nil
(do
(def minn (index l 0))
(def i 1)
(while (< i (len l))
(if (< (index l i) minn)
(set! minn (index l i)))
(set! i (inc i))
)
minn
)
))
(defn max (l)
(if (or (not l) (= (len l) 0))
nil
(do
(def maxx (index l 0))
(def i 1)
(while (< i (len l))
(if (> (index l i) maxx)
(set! maxx (index l i)))
(set! i (inc i))
)
maxx
)
))
; load and init doc system
(include "/usr/local/var/mlisp/doc.lsp")