Add flip and orlet.

This commit is contained in:
Kyle Isom 2015-09-03 22:37:42 -07:00
parent 5aa19630ad
commit 5bb8215444
6 changed files with 47 additions and 0 deletions

5
docs/flip.lisp Normal file
View File

@ -0,0 +1,5 @@
CL-USER> (format t "(- 7 3) -> ~A~%"
(funcall (flip #'- 3) 7))
(- 7 3) -> 4
NIL

View File

@ -117,6 +117,8 @@ some functions I found useful.)
@code(@include[path=examples.lisp start=1 end=5]()) @code(@include[path=examples.lisp start=1 end=5]())
@cl:doc(function effector) @cl:doc(function effector)
@cl:doc(function empty-or-nil-p) @cl:doc(function empty-or-nil-p)
@cl:doc(function flip)
@code(@include[path="flip.lisp"]())
@cl:doc(function macroexpand-n) @cl:doc(function macroexpand-n)
@cl:doc(function mksymb) @cl:doc(function mksymb)
@cl:doc(function mkkw) @cl:doc(function mkkw)
@ -224,6 +226,8 @@ For example, from @c(kmop/kmop.lisp):
@cl:doc(macro condlet*) @cl:doc(macro condlet*)
@cl:doc(macro iflet) @cl:doc(macro iflet)
@cl:doc(macro iflet*) @cl:doc(macro iflet*)
@cl:doc(macro orlet)
@code(@include[path="orlet.lisp"]())
@cl:doc(macro unlesslet) @cl:doc(macro unlesslet)
@cl:doc(macro unlesslet*) @cl:doc(macro unlesslet*)
@cl:doc(macro whenlet) @cl:doc(macro whenlet)
@ -360,6 +364,9 @@ described in "Miscellaneous utilities" under "Vector-related".)
@item(@c(flatten) is a function defined in @c(on.lisp), described in @item(@c(flatten) is a function defined in @c(on.lisp), described in
"On Lisp utilities".) "On Lisp utilities".)
@item(@c(flip) is a function defined in @c(kutils.lisp), described in
"Miscellaneous utilities" under "General".)
@item(@c(group) is a function defined in @c(kutils.lisp), described in @item(@c(group) is a function defined in @c(kutils.lisp), described in
"On Lisp utilities".) "On Lisp utilities".)
@ -410,6 +417,9 @@ utilities".)
@item(@c(new-vector) is a function defined in @c(kutils.lisp), @item(@c(new-vector) is a function defined in @c(kutils.lisp),
described in "Miscellaneous utilities" under "Vector-related".) described in "Miscellaneous utilities" under "Vector-related".)
@item(@c(orlet) is a macro defined in @c(macros.lisp), described in
"Macros" under "Let macros".)
@item(@c(partial) is a function defined in @c(kutils.lisp), described @item(@c(partial) is a function defined in @c(kutils.lisp), described
in "Miscellaneous utilities" under "Clojure-inspired functions".) in "Miscellaneous utilities" under "Clojure-inspired functions".)
@ -485,6 +495,7 @@ Alphabetical documentation for all exported symbols.
@cl:doc(function enable-hash-table-reader) @cl:doc(function enable-hash-table-reader)
@cl:doc(function extend-vector) @cl:doc(function extend-vector)
@cl:doc(function flatten) @cl:doc(function flatten)
@cl:doc(function flip)
@cl:doc(function group) @cl:doc(function group)
@cl:doc(function hash-table-to-alist) @cl:doc(function hash-table-to-alist)
@cl:doc(function hashkeys) @cl:doc(function hashkeys)
@ -499,6 +510,7 @@ Alphabetical documentation for all exported symbols.
@cl:doc(function mksymb) @cl:doc(function mksymb)
@cl:doc(function new-hash-table) @cl:doc(function new-hash-table)
@cl:doc(function new-vector) @cl:doc(function new-vector)
@cl:doc(macro orlet)
@cl:doc(function partial) @cl:doc(function partial)
@cl:doc(function partition) @cl:doc(function partition)
@cl:doc(macro sethash) @cl:doc(macro sethash)

12
docs/orlet.lisp Normal file
View File

@ -0,0 +1,12 @@
(macroexpand-1
'(orlet ((cls (class-symbol s))
(slots (only-key-slots cls))
(args (json-slots tbl slots)))
(apply #'make-instance cls args)))
(LET ((CLD (CLASS-SYMBOL S)))
(WHEN CLS
(LET ((SLOTS (ONLY-KEY-SLOTS CLS)))
(WHEN SLOTS
(LET ((ARGS (JSON-SLOTS TBL SLOTS)))
(WHEN ARGS
(PROGN (APPLY #'MAKE-INSTANCE CLS ARGS))))))))

View File

@ -46,6 +46,11 @@ additional args provided to the lambda."
(lambda (&rest args) (lambda (&rest args)
(apply fn (append initial-args args)))) (apply fn (append initial-args args))))
(defun flip (fn y)
"Given a function @c(fn) that takes arguments @c(x) and @c(y), return a lambda with @c(y) applied to the function."
(lambda (x)
(funcall fn x y)))
(defun macroexpand-n (n form) (defun macroexpand-n (n form)
"Expand the macro n times." "Expand the macro n times."
(let ((new-form form)) (let ((new-form form))

View File

@ -4,6 +4,17 @@
;;; Various utility macros. ;;; Various utility macros.
(defun orletfun (bindings finally)
(if (null bindings)
finally
`(let (,(first bindings))
(when ,(first (first bindings))
,(orletfun (rest bindings) finally)))))
(defmacro orlet (bindings &body body)
"For each set of bindings, evaluate them in sequence. If each binding evaluates to T, evaluate @c(body) in a @c(progn)."
(orletfun bindings `(progn ,@body)))
(defmacro whenlet (bindings &body body) (defmacro whenlet (bindings &body body)
"Evaluate the bindings in a let form; if they all evaluate to T, "Evaluate the bindings in a let form; if they all evaluate to T,
evaluate @c(body) in an implicit @c(progn)." evaluate @c(body) in an implicit @c(progn)."

View File

@ -19,6 +19,7 @@
#:drop #:drop
#:build-list #:build-list
#:partial #:partial
#:flip
#:macroexpand-n #:macroexpand-n
#:mksymb #:mksymb
#:mkkw #:mkkw
@ -44,6 +45,7 @@
#:alist-to-hash-table #:alist-to-hash-table
;; macros.lisp ;; macros.lisp
#:orlet
#:whenlet #:whenlet
#:whenlet* #:whenlet*
#:unlesslet #:unlesslet