This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; non-recursive quicksort | |
;;; http://bertrandmeyer.com/2014/12/07/lampsort/ | |
(defun partition (array low high) | |
(let ((pivot-value (aref array high)) | |
(insert-at low)) | |
(loop for i from low upto high do | |
(when (< (aref array i) pivot-value) | |
(rotatef (aref array i) (aref array insert-at)) | |
(incf insert-at))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun split-alphanumeric-string (string) | |
(let ((pos0 0) | |
(pos1 0) ) | |
(labels ((end-pos-of (fn) | |
(loop while (and (< pos1 (length string)) | |
(funcall fn (aref string pos1))) | |
do (incf pos1)) | |
pos1)) | |
(loop while (< pos0 (length string)) | |
when (not (digit-char-p (aref string pos0))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; here is a version with a better shuffle function. Note that the shuffle | |
;; function is written in a functional Lisp style. You need to read it from | |
;; inside to outside. | |
;; it uses a vector where the elements get a random double float attached. | |
;; The vector gets sorted by the random double floats. | |
;; the vector-iota function is another utility function | |
;; the 'domain' level LOTTERY function then is just a composition of the utility functions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun hashtable-to-list (table &aux result) | |
(maphash (lambda (key value) | |
(push (cons key value) result)) | |
table) | |
result) | |
(defun collect-into-table (list &key key-fn value-fn &aux (table (make-hash-table))) | |
(mapc (lambda (item &aux (key (funcall key-fn item))) | |
(setf (gethash key table) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro new-flet (bindings &body body) | |
(loop with new-arg = (make-symbol "ARGS") | |
for (name f) in bindings | |
for new-name = (make-symbol (symbol-name name)) | |
collect (list new-name f) into let-bindings | |
collect `(,name (&rest ,new-arg) | |
(declare (dynamic-extent ,new-arg)) | |
(apply ,new-name ,new-arg)) | |
into flet-bindings | |
finally (return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun n-at-a-time (n fn list) | |
(loop while list | |
unless (nthcdr n list) | |
do (setf n (length list)) | |
collect (apply fn (subseq list 0 n)) | |
do (setf list (nthcdr n list)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun example (&rest maps) | |
(format nil "~:[<none>~;~:*~{~A~^, ~}~]" | |
(sort (remove-duplicates | |
(loop for map in maps nconc | |
(loop for key being the hash-key of map collect key)) | |
:test 'equal) | |
'string<))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun concat (type &rest items) | |
(let* ((len (loop for e in items | |
if (typep e 'sequence) | |
sum (length e) | |
else sum 1)) | |
(seq (make-sequence type len))) | |
(loop with pos = 0 | |
for e in items | |
if (typep e 'sequence) | |
do (progn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro -> (form &rest forms) | |
(loop with result = form | |
with next-form = nil | |
while forms | |
do (setf next-form (pop forms)) | |
(if (consp next-form) | |
(setf result (destructuring-bind (function . args) next-form | |
`(,function ,result ,@args))) | |
(setf result `(,next-form ,result))) | |
finally (return result))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; http://tapestryjava.blogspot.se/2013/02/crafting-code-in-clojure.html | |
; | |
; Extract all the keys from both maps | |
; Remove any duplicates | |
; Convert the keys to strings | |
; Sort the strings into ascending order | |
; Build and return one big string, by concatenating all the key strings, using ", " as a separator | |
; Return "<none>" if both maps are empty | |
(defun hash-table-keys (hash-table) |