Add partition.

This commit is contained in:
Kyle 2015-09-01 02:21:07 -07:00
parent 580cb5c21b
commit 782442afc8
3 changed files with 33 additions and 0 deletions

View File

@ -133,6 +133,7 @@ some functions I found useful.)
@cl:doc(function take) @cl:doc(function take)
@cl:doc(function drop) @cl:doc(function drop)
@cl:doc(function partial) @cl:doc(function partial)
@cl:doc(function partition)
) )
@end(def) @end(def)
@ -395,6 +396,9 @@ described in "Miscellaneous utilities" under "Vector-related".)
@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".)
@item(@c(partition) is a function defined in @c(kutils.lisp), described
in "Miscellaneous utilities" under "Clojure-inspired functions".)
@item(@c(read-file-as-string) is a macro defined in @c(macros.lisp), @item(@c(read-file-as-string) is a macro defined in @c(macros.lisp),
described in "Macros" under "File macros".) described in "Macros" under "File macros".)
@ -475,6 +479,7 @@ Alphabetical documentation for all exported symbols.
@cl:doc(function new-hash-table) @cl:doc(function new-hash-table)
@cl:doc(function new-vector) @cl:doc(function new-vector)
@cl:doc(function partial) @cl:doc(function partial)
@cl:doc(function partition)
@cl:doc(macro sethash) @cl:doc(macro sethash)
@cl:doc(function symb) @cl:doc(function symb)
@cl:doc(function take) @cl:doc(function take)

View File

@ -144,3 +144,30 @@ effectful code, such as logging."
(apply fn args)))) (apply fn args))))
(defun partition-list (test lst)
(let (match no-match)
(dolist (x lst)
(if (funcall test x)
(push x match)
(push x no-match)))
(vector match no-match)))
(defun partition-vector (test vec)
(let ((match (new-vector))
(no-match (new-vector)))
(dotimes (i (length vec))
(let ((x (aref vec i)))
(if (funcall test x)
(vector-push-extend x match)
(vector-push-extend x no-match))))
(list match no-match)))
(defun partition (pred seq)
"Split @c(seq) into a pair of sequences with @c(pred) : the first of
the pair are those elements satisfying @c(pred), and the second are
those that do not satisfy @c(pred)."
(cond
((listp seq) (partition-list pred seq))
((vectorp seq) (partition-vector pred seq))
(t (error "Values of type ~A cannot be partitioned."
(type-of seq)))))

View File

@ -30,6 +30,7 @@
#:cartprod2 #:cartprod2
#:empty-or-nil-p #:empty-or-nil-p
#:effector #:effector
#:partition
;; kutils-hash-tables.lisp ;; kutils-hash-tables.lisp
#:enable-hash-table-reader #:enable-hash-table-reader