Implement $ operator, second class version of the applicative operator
The `$` operator takes a sequence of FORMS and returns a unary function which applies the input through that sequence via the `->>` operator. For example, consider the predicate "not null". `null` is built into Common Lisp but "not null" requires writing a function (lambda (x) (not (null x))). Now, using this operator, you can write ($ not null) which returns the same lambda as above while being more concise.
This commit is contained in:
@@ -77,3 +77,9 @@ arguments `LAMBDA-LIST' with body `BODY'."
|
|||||||
(declaim (ftype ,type ,name))
|
(declaim (ftype ,type ,name))
|
||||||
(defun ,name ,lambda-list
|
(defun ,name ,lambda-list
|
||||||
,@body)))
|
,@body)))
|
||||||
|
|
||||||
|
(defmacro $ (&rest forms)
|
||||||
|
"Given a sequence of FORMS, return a unary function which applies each form
|
||||||
|
sequentially"
|
||||||
|
`(lambda (x)
|
||||||
|
(->> x ,@forms)))
|
||||||
|
|||||||
@@ -22,7 +22,8 @@
|
|||||||
(:export
|
(:export
|
||||||
:_ :--> :->>
|
:_ :--> :->>
|
||||||
:while
|
:while
|
||||||
:-> :fn))
|
:-> :fn
|
||||||
|
:$))
|
||||||
|
|
||||||
(defpackage lib.functions
|
(defpackage lib.functions
|
||||||
(:use :cl :lib.macros)
|
(:use :cl :lib.macros)
|
||||||
|
|||||||
Reference in New Issue
Block a user