63 lines
2.3 KiB
Common Lisp
63 lines
2.3 KiB
Common Lisp
;;; lib.functions.lisp - 2025-02-09
|
|
|
|
;; Copyright (C) 2025 Aryadev Chavali
|
|
|
|
;; This program is distributed in the hope that it will be useful, but WITHOUT
|
|
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
;; FOR A PARTICULAR PURPOSE. See the GNU General Public License Version 2 for
|
|
;; details.
|
|
|
|
;; You may distribute and modify this code under the terms of the GNU General
|
|
;; Public License Version 2, which you should have received a copy of along with
|
|
;; this program. If not, please go to <https://www.gnu.org/licenses/>.
|
|
|
|
;;; Commentary:
|
|
|
|
;; Helpful functions for usage throughout the project.
|
|
|
|
;;; Code:
|
|
|
|
(in-package :cantedraw.lib.functions)
|
|
|
|
(fn range (&key (start 0) (end 0) (step 1))
|
|
(-> (&key (:start fixnum) (:end fixnum) (:step fixnum))
|
|
list)
|
|
"Return list of integers in interval [START, END). If STEP is given, then
|
|
each member is STEP distance apart i.e. {START + n * STEP | n from 0}.
|
|
|
|
If END is not given, return interval [0, START)."
|
|
(if (< end start)
|
|
(error (format nil "~a < ~a" end start))
|
|
(loop :for i :from start :to (1- end) :by step
|
|
:collect i)))
|
|
|
|
(fn split (n lst) (-> (fixnum sequence) (values sequence sequence))
|
|
"Return CONS where CAR is the first N elements of LST and CDR is the rest."
|
|
(values (subseq lst 0 n)
|
|
(subseq lst n)))
|
|
|
|
(fn rev-map (indicator lst &key (key-eq #'eq))
|
|
(-> (function sequence &key (:key-eq function)) list)
|
|
"Given LST and INDICATOR: LST -> A, return an association list A -> 2^LST
|
|
where key x in A has associations {y in LST : INDICATOR(y) = x}."
|
|
(loop :with assoc-list := nil
|
|
:for element :in (coerce lst 'list)
|
|
:for key := (funcall indicator element)
|
|
:if (assoc key assoc-list :test key-eq)
|
|
:do (->> (alist-val key assoc-list)
|
|
(cons element)
|
|
(setf (alist-val key assoc-list)))
|
|
:else
|
|
:do (setq assoc-list (cons (list key element) assoc-list))
|
|
:finally (return assoc-list)))
|
|
|
|
(fn parse-integer* (inp) (-> (string) (or integer list))
|
|
"Given string INP, attempt to parse an integer. Return NIL otherwise."
|
|
(parse-integer inp :junk-allowed t))
|
|
|
|
(fn remove-at-indices (indices lst) (-> (list sequence) list)
|
|
(loop :for i :from 0 :to (1- (length lst))
|
|
:for item :in (coerce lst 'list)
|
|
:if (not (member i indices))
|
|
:collect item))
|