[lib] Add -2map and siblings

master
Jacopo De Simoi 3 months ago
parent 3b0e0ecc44
commit a4768f7521
  1. 48
      lib/lib-advent.org

@ -38,22 +38,42 @@
(-map-indexed (lambda (x el) (list el (list x y))) l))
li)))))
#+end_src
* 2D geometry auxiliary functions
Here a vector is just a list with two components; the "dot"
function is a bit of an overkill tbh
#+begin_src emacs-lisp :results none
(defun advent/dot (a b)
"Return the dot product of the two vectors"
(-reduce #'+ (-map (lambda (x) (* (car x) (cdr x))) (-zip-pair a b))))
(defun advent/neighbour (p dir)
"Returns the neighbour to P in the direction DIR"
(list (+ (car p) (car dir))
(+ (cadr p) (cadr dir))))
(defun advent/taxicab-distance (p q)
"Returns the taxicab distance from P to Q"
(+ (abs (- (car p) (car q)))
(abs (- (cadr p) (cadr q)))))
#+end_src
(defun advent/dot (a b)
"Return the dot product of the two vectors"
(-reduce #'+ (-map (lambda (x) (* (car x) (cdr x))) (-zip-pair a b))))
(defun advent/neighbour (p dir)
"Returns the neighbour to P in the direction DIR"
(list (+ (car p) (car dir))
(+ (cadr p) (cadr dir))))
(defun advent/taxicab-distance (p q)
"Returns the taxicab distance from P to Q"
(+ (abs (- (car p) (car q)))
(abs (- (cadr p) (cadr q)))))
#+end_src
These are two-dimensional equivalent of -map and -map-indexed
#+begin_src emacs-lisp
(defun -2map (fun li)
(--map (-map fun it) li))
(defmacro --2map (form list)
`(-2map (lambda (it) ,form) ,list))
(defun -2map-indexed (fun li)
(-map-indexed (lambda (j row)
(-map-indexed (lambda (i it)
(funcall fun i j it))
row))
li))
(defmacro --2map-indexed (form li)
`(-2map-indexed (lambda (it-index-1 it-index-2 it) ,form) ,li))
#+end_src

Loading…
Cancel
Save