rev-map function
Given an indicator function (A->B) and a list of items of A, return an association list associating B to the elements that map to it; essentially the inverse map of the indicator.
This commit is contained in:
@@ -28,11 +28,28 @@ each member is STEP distance apart."
|
|||||||
:collect i)))
|
:collect i)))
|
||||||
|
|
||||||
(fn parse-integer* (inp) (-> (string) (or integer 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))
|
(parse-integer inp :junk-allowed t))
|
||||||
|
|
||||||
(fn take (n lst) (-> (fixnum list) list)
|
(fn take (n lst) (-> (fixnum list) list)
|
||||||
|
"Return the first N elements of LST."
|
||||||
(subseq lst 0 n))
|
(subseq lst 0 n))
|
||||||
|
|
||||||
(fn split (n lst) (-> (fixnum list) list)
|
(fn split (n lst) (-> (fixnum list) list)
|
||||||
|
"Return CONS where CAR is the first N elements of LST and CDR is the rest."
|
||||||
(cons (take n lst)
|
(cons (take n lst)
|
||||||
(subseq lst n)))
|
(subseq lst n)))
|
||||||
|
|
||||||
|
(fn rev-map (indicator lst &key (key-eq #'eq))
|
||||||
|
(-> (function list &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 lst
|
||||||
|
:for key = (funcall indicator element)
|
||||||
|
:if (assoc key assoc-list :test key-eq)
|
||||||
|
:do (setf (alist-val key assoc-list)
|
||||||
|
(cons element (alist-val key assoc-list)))
|
||||||
|
:else
|
||||||
|
:do (setq assoc-list (acons key element assoc-list))
|
||||||
|
:finally (return assoc-list)))
|
||||||
|
|||||||
@@ -29,7 +29,8 @@
|
|||||||
(:use :cl :lib.macros)
|
(:use :cl :lib.macros)
|
||||||
(:export
|
(:export
|
||||||
:parse-integer*
|
:parse-integer*
|
||||||
:range :take :split))
|
:range :take :split
|
||||||
|
:rev-map))
|
||||||
|
|
||||||
(defpackage main
|
(defpackage main
|
||||||
(:use :cl :lib.macros)
|
(:use :cl :lib.macros)
|
||||||
|
|||||||
Reference in New Issue
Block a user