Last active
December 28, 2018 07:06
-
-
Save jasonrobot/123c098488c4e73889b3f5c1f76ac097 to your computer and use it in GitHub Desktop.
Common lisp, why no have common sequence functions!?! >:-|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun all (seq fn) | |
"Immediately return NIL if FN returns NIL for any item in SEQ." | |
(loop for i in seq | |
always (funcall fn i))) | |
(all '(1 2 3) (lambda (x) (< x 5))) | |
;; => T | |
(all '(1 2) (lambda (x) (= x 1))) | |
;; => NIL | |
(defun none (seq fn) | |
"Immediately return NIL if FN returns T for any item in SEQ." | |
(loop for i in seq | |
always (not (funcall fn i)))) | |
(none '(1 3) (lambda (x) (= 0 (mod x 2)))) | |
;; => T | |
(none '(1 3 6) (lambda (x) (= 0 (mod x 2)))) | |
;; => NIL | |
(defun any (seq fn) | |
"Immediately return T if FN returns T for any item in SEQ." | |
(not (loop for i in seq | |
never (funcall fn i)))) | |
(any '(1 2 3 "1") #'stringp) | |
;; => T | |
(any '("foo" "bar") #'numberp) | |
;; => NIL | |
;; I bet the preferred way to do this is just use #'loop, but this python style feels better for me right now.111 | |
(defun range-impl (start end step) | |
(loop for i from start by step to (- end 1) | |
collect i)) | |
(defun range (par1 &optional par2 par3) | |
(cond | |
(par3 (range-impl par1 par2 par3)) | |
(par2 (range-impl par1 par2 1)) | |
(t (range-impl 0 par1 1)))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey you dingus:
see EVERY SOME NOTEVERY and NOTANY
I guess the pythonic range impl is still kinda nice though.