Add with-gensyms and in macros.
This commit is contained in:
parent
6aab1b6bd1
commit
5847a6b054
|
@ -87,6 +87,7 @@ some functions I found useful.)
|
||||||
@cl:doc(function group)
|
@cl:doc(function group)
|
||||||
@cl:doc(function flatten)
|
@cl:doc(function flatten)
|
||||||
@cl:doc(function compose)
|
@cl:doc(function compose)
|
||||||
|
@cl:doc(macro with-gensyms)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -263,10 +264,13 @@ Evaluate @c(body), and call @c(mkstr) on the result. Write the
|
||||||
resulting string to @c(path). Any remaining arguments are sent to
|
resulting string to @c(path). Any remaining arguments are sent to
|
||||||
@c(with-open-file).
|
@c(with-open-file).
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@end(def)
|
@end(def)
|
||||||
|
|
||||||
|
@term(General)
|
||||||
|
|
||||||
|
@cl:with-package[name="kutils"](
|
||||||
|
@cl:doc(macro in))
|
||||||
|
|
||||||
@end(deflist)
|
@end(deflist)
|
||||||
|
|
||||||
@end(section)
|
@end(section)
|
||||||
|
@ -373,6 +377,9 @@ utilities".)
|
||||||
@item(@c(iflet*) is a macro defined in @c(macros.lisp), described in
|
@item(@c(iflet*) is a macro defined in @c(macros.lisp), described in
|
||||||
"Macros" under "Let macros".)
|
"Macros" under "Let macros".)
|
||||||
|
|
||||||
|
@item(@c(in) is a macro defined in @c(macros.lisp), described in
|
||||||
|
"Macros" under "General".)
|
||||||
|
|
||||||
@item(@c(interpose) is a function defined in @c(kutils.lisp),
|
@item(@c(interpose) is a function defined in @c(kutils.lisp),
|
||||||
described in "Miscellaneous utilities" under "Clojure-inspired
|
described in "Miscellaneous utilities" under "Clojure-inspired
|
||||||
functions".)
|
functions".)
|
||||||
|
@ -433,6 +440,9 @@ Lisp utilities".)
|
||||||
@item(@c(whenlet*) is a macro defined in @c(macros.lisp), described in
|
@item(@c(whenlet*) is a macro defined in @c(macros.lisp), described in
|
||||||
"Macros" under "Let macros".)
|
"Macros" under "Let macros".)
|
||||||
|
|
||||||
|
@item(@c(with-gensyms) is a macro defined in @c(on.lisp), described in
|
||||||
|
"On Lisp utilities".)
|
||||||
|
|
||||||
@item(@c(with-new-hash-table) is a macro defined in
|
@item(@c(with-new-hash-table) is a macro defined in
|
||||||
@c(kutils-hash-tables.lisp), described in "Hash-table related
|
@c(kutils-hash-tables.lisp), described in "Hash-table related
|
||||||
utilities".)
|
utilities".)
|
||||||
|
@ -480,6 +490,7 @@ Alphabetical documentation for all exported symbols.
|
||||||
@cl:doc(function hashkeys)
|
@cl:doc(function hashkeys)
|
||||||
@cl:doc(macro iflet)
|
@cl:doc(macro iflet)
|
||||||
@cl:doc(macro iflet*)
|
@cl:doc(macro iflet*)
|
||||||
|
@cl:doc(macro in)
|
||||||
@cl:doc(function interpose)
|
@cl:doc(function interpose)
|
||||||
@cl:doc(function macroexpand-n)
|
@cl:doc(function macroexpand-n)
|
||||||
@cl:doc(macro mapv)
|
@cl:doc(macro mapv)
|
||||||
|
@ -497,6 +508,7 @@ Alphabetical documentation for all exported symbols.
|
||||||
@cl:doc(macro unlesslet*)
|
@cl:doc(macro unlesslet*)
|
||||||
@cl:doc(macro whenlet)
|
@cl:doc(macro whenlet)
|
||||||
@cl:doc(macro whenlet*)
|
@cl:doc(macro whenlet*)
|
||||||
|
@cl:doc(macro with-gensyms)
|
||||||
@cl:doc(macro with-new-hash-table)
|
@cl:doc(macro with-new-hash-table)
|
||||||
@cl:doc(macro with-read-from-file)
|
@cl:doc(macro with-read-from-file)
|
||||||
@cl:doc(macro with-write-to-file)
|
@cl:doc(macro with-write-to-file)
|
||||||
|
|
29
macros.lisp
29
macros.lisp
|
@ -113,3 +113,32 @@ value of @c(stream). Any remaining keyword arguments are passed to
|
||||||
(error "Specifying :direction is not allowed with WRITE-FILE."))
|
(error "Specifying :direction is not allowed with WRITE-FILE."))
|
||||||
`(with-open-file (,stream ,path :direction ,direction ,@args)
|
`(with-open-file (,stream ,path :direction ,direction ,@args)
|
||||||
,@body))
|
,@body))
|
||||||
|
|
||||||
|
(defun is (a b test key)
|
||||||
|
"Are a and b equal? If not NIL, #'key is applied to b, then #'test
|
||||||
|
is called on both."
|
||||||
|
(declare (inline))
|
||||||
|
(funcall test
|
||||||
|
(if key
|
||||||
|
(funcall key b)
|
||||||
|
b)
|
||||||
|
a))
|
||||||
|
|
||||||
|
(defmacro in (obj seq &key (test #'eql) key deep)
|
||||||
|
"Returns T if @c(obj), which may be a vector or list), is in
|
||||||
|
@c(seq); using @c(test) to determine whether the object matches. If
|
||||||
|
@c(key) is not NIL, it is applied to the key before @c(test). If
|
||||||
|
@c(deep) is true, @c(seq) will be flattened before checking the
|
||||||
|
list. Note that @c(deep) is only valid for lists, and will not behave
|
||||||
|
as expected with vectors."
|
||||||
|
(with-gensyms (obj% seq% x!)
|
||||||
|
(declare (dynamic-extent obj% seq% x!))
|
||||||
|
`(let ((,obj% ,obj)
|
||||||
|
(,seq% (if ,deep ,(flatten seq) ,seq)))
|
||||||
|
(cond
|
||||||
|
((vectorp ,seq%)
|
||||||
|
(loop for ,x! across ,seq%
|
||||||
|
thereis (is ,obj% ,x! ,test ,key)))
|
||||||
|
((listp ,seq%)
|
||||||
|
(loop for ,x! in ,seq%
|
||||||
|
thereis (is ,obj% ,x! ,test ,key)))))))
|
||||||
|
|
9
on.lisp
9
on.lisp
|
@ -53,3 +53,12 @@ composed together in a chain."
|
||||||
:from-end t
|
:from-end t
|
||||||
:initial-value (apply fn1 args))))
|
:initial-value (apply fn1 args))))
|
||||||
#'identity))
|
#'identity))
|
||||||
|
|
||||||
|
(defmacro with-gensyms (syms &body body)
|
||||||
|
"Binds a whole list of variables to gensyms."
|
||||||
|
`(let ,(mapcar #'(lambda (s)
|
||||||
|
`(,s (gensym)))
|
||||||
|
syms)
|
||||||
|
,@body))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
#:group
|
#:group
|
||||||
#:flatten
|
#:flatten
|
||||||
#:compose
|
#:compose
|
||||||
|
#:with-gensyms
|
||||||
|
|
||||||
;; lol.lisp : utilities from Let Over Lambda
|
;; lol.lisp : utilities from Let Over Lambda
|
||||||
#:defmacro!
|
#:defmacro!
|
||||||
|
@ -55,4 +56,5 @@
|
||||||
#:with-string-output-to-file
|
#:with-string-output-to-file
|
||||||
#:with-read-from-file
|
#:with-read-from-file
|
||||||
#:with-write-to-file
|
#:with-write-to-file
|
||||||
|
#:in
|
||||||
))
|
))
|
||||||
|
|
Loading…
Reference in New Issue