Permalink
Browse files

Add some anaphoric functions.

  • Loading branch information...
skeeto committed Oct 27, 2012
1 parent b5433a5 commit 96c569bacc329188b444509d07801bdff46b7fa2
Showing with 37 additions and 6 deletions.
  1. +1 −0 init.el
  2. +36 −6 utility.el
View
@@ -24,6 +24,7 @@
(require 'memoize)
(require 'imgur)
(require 'my-funcs) ; custom functions
+(require 'utility)
;; Seed the PRNG
(random t)
View
@@ -6,12 +6,7 @@
(require 'cl)
-(defun partition (predicate seq)
- "Divide SEQ into two lists, selected by PREDICATE."
- (loop for element in (coerce seq 'list)
- when (funcall predicate element) collect element into a
- else collect element into b
- finally (return (list a b))))
+;; Higher-order functions
(defun curry (function args)
"Partially apply FUNCTION to ARGS."
@@ -40,4 +35,39 @@ right-associatively."
(lambda (x)
(reduce #'funcall functions :initial-value x :from-end t)))
+;; Utility
+
+(defun partition (predicate seq)
+ "Divide SEQ into two lists, selected by PREDICATE."
+ (loop for element in (coerce seq 'list)
+ when (funcall predicate element) collect element into a
+ else collect element into b
+ finally (return (list a b))))
+
+;; Anaphoric macros
+
+(defmacro amap (expression seq)
+ "Anahoric map: binds IT in expression to SEQ elements."
+ `(mapcar (lambda (it) ,expression) ,seq))
+
+(defmacro aif (condition then &rest else)
+ "Anaphoric if: binds IT to the condition."
+ `(let ((it ,condition))
+ (if it ,then ,@else)))
+
+(defmacro awhile (condition &rest body)
+ "Anaphoric while: binds IT to CONDITION in BODY."
+ (declare (indent defun))
+ `(let (it)
+ (while (setf it ,condition)
+ ,@body)))
+
+(defmacro alambda (params &rest body)
+ "Anaphoric lambda: binds SELF to the anonymous function itself."
+ (declare (indent defun))
+ `(labels ((self ,params ,@body))
+ #'self))
+
+(provide 'utility)
+
;;; utility.el ends here

0 comments on commit 96c569b

Please sign in to comment.