aboutsummaryrefslogtreecommitdiff
path: root/src/collections.lisp
blob: 84a54a2fc30d48b6c8bb32f3e325307266282902 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(in-package :senzill.collections)

(defmacro push-back (item place)
  "Item is appended to the (right) end of the place (list)"
  `(setf ,place (append ,place (list ,item))))

(defmacro pop-back (place)
  "Removes destructively last element and returns it"
  `(let ((val (car (last ,place))))
     (setq ,place (nbutlast ,place))
     val))

(defun slice (place &key (begin 0) (end (length place)))
  "Returns list of elements from begin to end indecies, inclusive"
  (butlast (nthcdr begin place) (- (length place) end 1)))

(defun integer-to-list (num &key (min-length 1))
  "Returns a list where each element is a digit"
  (loop for i = num then (floor i 10)
        for m = (list (mod i 10)) then (push (mod i 10) m)
        until (and (= i 0) (> (length m) min-length))
        finally (pop m) (return m)))