160 lines
3.6 KiB
Common Lisp
160 lines
3.6 KiB
Common Lisp
; not a bool
|
|
(defun not (x) (if x 0 1))
|
|
|
|
; negate a number
|
|
(defun neg (n) (- 0 n))
|
|
|
|
; is a number positive?
|
|
(defun is-pos? (n) (> n 0))
|
|
|
|
; is a number negative?
|
|
(defun is-neg? (n) (< n 0))
|
|
|
|
; decrement a number
|
|
(defun dec (n) (- n 1))
|
|
|
|
; increment a number
|
|
(defun inc (n) (+ n 1))
|
|
|
|
|
|
(defun string-ltrim (str)
|
|
(string-rltrim str " \n\r\t" "ltrim"))
|
|
(defun string-rtrim (str)
|
|
(string-rltrim str " \n\r\t" "rtrim"))
|
|
(defun string-trim (str)
|
|
(string-rltrim str " \n\r\t" "trim"))
|
|
|
|
; pad string on the end
|
|
(defun string-rpad (str length pad_char)
|
|
(string-pad str length pad_char "rpad"))
|
|
|
|
; pad string on the begining
|
|
(defun string-lpad (str length pad_char)
|
|
(string-pad str length pad_char "lpad"))
|
|
|
|
|
|
; pause for interval
|
|
(defun sleep (time)
|
|
(system-cmd (+ "sleep " (string time))))
|
|
|
|
(defun get-env (var)
|
|
(second (system-cmd (+ "echo ${" var "} | tr -d \"\n\""))))
|
|
|
|
|
|
; return second element of list
|
|
(defun second (l) (index l 1))
|
|
|
|
; return third element of list
|
|
(defun third (l) (index l 2))
|
|
|
|
; return fourth element of list
|
|
(defun fourth (l) (index l 3))
|
|
|
|
; return fifth element of list
|
|
(defun fifth (l) (index l 4))
|
|
|
|
; return nth element of list, indexing from 0
|
|
(defun nth (i l) (index l i))
|
|
|
|
; return 1 when list contains item otherwise 0
|
|
(defun member (lst itm)
|
|
(do
|
|
; TODO check if is empty list
|
|
(define found_index -1)
|
|
(define i 0)
|
|
(define lst_len (len lst))
|
|
|
|
(while (and (< i lst_len) (= found_index -1))
|
|
(if (= itm (index lst i))
|
|
(define found_index i)
|
|
(define i (+ i 1))
|
|
))
|
|
|
|
; TODO when nil will be implemented itm / nil
|
|
(if (!= -1 found_index)
|
|
1
|
|
0)
|
|
))
|
|
|
|
(defun make-list-of (size val)
|
|
(do
|
|
(define lst '())
|
|
(define i 0)
|
|
(while (< i size)
|
|
(define lst (push lst val))
|
|
(define i (inc i)))
|
|
lst
|
|
))
|
|
|
|
(defun make-list (size)
|
|
(make-list-of size nil))
|
|
|
|
(defun uniq (lst)
|
|
(do
|
|
(define rslt '())
|
|
(define i 0)
|
|
(define lst_len (len lst))
|
|
(while (< i lst_len)
|
|
(define e (index lst i))
|
|
(if (not (member rslt e))
|
|
(define rslt (push rslt e)))
|
|
|
|
(define i (inc i)))
|
|
|
|
rslt
|
|
))
|
|
|
|
(defun flatten(lst)
|
|
(do
|
|
(define rslt '())
|
|
(for e lst
|
|
(if (= (type e) "list")
|
|
(do
|
|
(for ee (flatten e)
|
|
(define rslt (push rslt ee))))
|
|
(define rslt (push rslt e))
|
|
))
|
|
|
|
rslt
|
|
))
|
|
|
|
(defun quick-sort-by (l cmp)
|
|
(if (<= (len l) 1)
|
|
l
|
|
(do
|
|
(define 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)
|
|
))
|
|
))
|
|
|
|
(defun quick-sort (l)
|
|
(quick-sort-by l (lambda (a b) (> a b))))
|
|
|
|
|
|
|
|
|
|
; from list of lists creates csv string
|
|
; (print (make-csv '(("r1c1" "r1c2") ("r2c1" "r2c2"))))
|
|
(defun make-csv (csv_list)
|
|
(do
|
|
(define rows_str "")
|
|
(define r 0)
|
|
(for row csv_list
|
|
(define cols_str "")
|
|
(define c 0)
|
|
(for col row
|
|
(if (= c 0)
|
|
(define cols_str col)
|
|
(define cols_str (+ cols_str "," (display col))))
|
|
(define c (inc c)))
|
|
(if (= r 0)
|
|
(define rows_str cols_str)
|
|
(define rows_str (+ rows_str "\n" cols_str)))
|
|
(define r (inc r)))
|
|
|
|
rows_str
|
|
))
|