diff --git a/docs/examples.lisp b/docs/examples.lisp new file mode 100644 index 0000000..0f551f1 --- /dev/null +++ b/docs/examples.lisp @@ -0,0 +1,4 @@ +* (cartprod2 '(a b c) '(1 2 3 4)) +((A . 1) (A . 2) (A . 3) (A . 4) + (B . 1) (B . 2) (B . 3) (B . 4) + (C . 1) (C . 2) (C . 3) (C . 4)) diff --git a/docs/manual.scr b/docs/manual.scr index 50bccb8..2541ffa 100644 --- a/docs/manual.scr +++ b/docs/manual.scr @@ -105,6 +105,8 @@ some functions I found useful.) @cl:with-package[name="kutils"]( @cl:doc(macro assoc-val) @cl:doc(function build-list) +@cl:doc(function cartprod2) +@code(@include[path=examples.lisp start=1 end=5]()) @cl:doc(function macroexpand-n) @cl:doc(function mksymb) @cl:doc(function mkkw) @@ -147,17 +149,7 @@ some functions I found useful.) The package has an optional reader macro that attempts to make Clojure-like hash table literals in the reader. For example: -@begin(code) -CL-USER> (gethash :foo #{:foo :bar :baz :quux}#) -:BAR -T -CL-USER> (let ((m #{:foo :bar :baz :quux}#)) - (sethash :baz :woz m) - (hashkeys m)) -(:BAZ :FOO) -CL-USER> (hashtable-to-alist #{:a :b :c :d}#) -((:C . :D) (:A . :B)) -@end(code) +@code(@include[path=reader.lisp]()) @cl:with-package[name="kutils"]( @cl:doc(function enable-hash-table-reader)) diff --git a/docs/reader.lisp b/docs/reader.lisp new file mode 100644 index 0000000..7d5c822 --- /dev/null +++ b/docs/reader.lisp @@ -0,0 +1,9 @@ +CL-USER> (gethash :foo #{:foo :bar :baz :quux}#) +:BAR +T +CL-USER> (let ((m #{:foo :bar :baz :quux}#)) + (sethash :baz :woz m) + (hashkeys m)) +(:BAZ :FOO) +CL-USER> (hashtable-to-alist #{:a :b :c :d}#) +((:C . :D) (:A . :B)) diff --git a/kutils.lisp b/kutils.lisp index a06a287..8a411e6 100644 --- a/kutils.lisp +++ b/kutils.lisp @@ -4,12 +4,6 @@ ;;; "kutils" goes here. Hacks and glory await! -(defun build-docs () - "Generate the package documentation using Codex." - (unless (find-package :codex) - (ql:quickload :codex)) - (codex:document :kutils)) - ;;; This file contains various utilities I've written. (defun take (n lst) @@ -128,5 +122,14 @@ new vector is adjustable and has a fill pointer set." (defmacro assoc-val (item alist &rest key-args) "Return the value of @c(item) in @c(alist). @c(key-args) should contain any additional keyword arguments to @c(assoc)." - `(first (assoc ,item ,alist ,@key-args))) + `(rest (assoc ,item ,alist ,@key-args))) + +(defun cartprod2 (a b) + "Produce the cartesian product of the two lists." + (apply #'concatenate 'list + (mapcar (lambda (x) + (mapcar (lambda (y) + (cons x y)) + b)) + a))) diff --git a/package.lisp b/package.lisp index d08e6b9..c52c2c7 100644 --- a/package.lisp +++ b/package.lisp @@ -27,6 +27,7 @@ #:build-vector #:extend-vector #:assoc-val + #:cartprod2 ;; kutils-hash-tables.lisp #:enable-hash-table-reader