Skip to content

Instantly share code, notes, and snippets.

View lispm's full-sized avatar

Rainer Joswig lispm

  • Germany
View GitHub Profile
@lispm
lispm / random-text.lisp
Created August 24, 2012 14:26
Colored and rotated text in LispWorks
(defun random-text (string &key (n 300) (color-filter nil))
(flet ((one-of (list)
(elt list (random (length list))))
(filter-colors (colors string)
(loop for color in colors
when (search string (symbol-name color) :test #'equalp)
collect color)))
(let* ((s (make-instance 'capi:output-pane))
(colors (color:get-all-color-names)))
(capi:contain s :width 2000 :height 1400)
@lispm
lispm / gist:3689837
Created September 10, 2012 09:16
drawing
; http://stackoverflow.com/questions/12345647/rewrite-this-script-by-designing-an-intepreter-in-racket
(defmacro verti (n &body body)
`(progn
(loop repeat ,n
do ,@body
(terpri))))
(defun hori (n s)
(loop repeat n do (princ s)))
@lispm
lispm / gist:4008619
Created November 3, 2012 20:29
average of a tree
(defun average (list)
(/ (tree-reduce #'+ list)
(tree-reduce #'+ list :key (constantly 1))))
(defun tree-reduce (fn tree &key (key #'identity))
(reduce fn tree
:key (lambda (item)
(if (consp item)
(tree-reduce fn item :key key)
(funcall key item)))))
@lispm
lispm / gist:4074589
Created November 14, 2012 20:32
FACTORS in Common Lisp using ITERATE
(defun factors (n)
(iterate
(with limit = (isqrt n))
(for factor from 1 below limit)
(for (values q r) = (floor n factor))
(when (zerop r)
(collect factor into lows)
(collect q into highs))
(finally (setf highs (reverse highs))
(when (= n (* limit limit))
@lispm
lispm / gist:4081687
Created November 15, 2012 22:09
defining a logical pathname LIB: for the LispWorks library
(setf (logical-pathname-translations "LIB")
`(("**;*" ,(make-pathname
:name :wild
:directory (append (pathname-directory
(sys:lisp-library-directory))
(list :wild-inferiors))
:defaults (sys:lisp-library-directory)))))
; now you can use the logical pathname LIB to refer to LispWorks files:
; (compile-file-if-needed "lib:examples;editor;commands;space-show-arglist.lisp" :load t)
(defun find-query (query descriptions)
(find query descriptions
:test (lambda (q b)
(interpret-query q b))
:key #'second))
(defun lookup (v bindings)
(let ((result (assoc v bindings)))
(if result
(second result)
@lispm
lispm / gist:4694973
Last active December 12, 2015 01:58
; 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)
@lispm
lispm / gist:4755118
Created February 11, 2013 15:28
Piping of forms in Common Lisp
(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)))
@lispm
lispm / gist:4973399
Created February 17, 2013 20:53
concatenate -> concat
(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
(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<)))