Skip to content

Instantly share code, notes, and snippets.

View lispm's full-sized avatar

Rainer Joswig lispm

  • Germany
View GitHub Profile
@lispm
lispm / square-spirals.lisp
Last active August 29, 2015 14:27
Reddit dailyprogrammer [2015-08-10] Challenge #227 [Easy] Square Spirals
; https://www.reddit.com/r/dailyprogrammer/comments/3ggli3/20150810_challenge_227_easy_square_spirals/
; [2015-08-10] Challenge #227 [Easy] Square Spirals
; Rainer Joswig, joswig@lisp.de, 2015
; portable Common Lisp code
; 'math' version based on Java version by 9speedy
;;; ================================================================
@lispm
lispm / lucky7s.lisp
Created August 29, 2015 23:47
[2015-08-28] Challenge #229 [Hard] Divisible by 7
; https://www.reddit.com/r/dailyprogrammer/comments/3irzsi/20150828_challenge_229_hard_divisible_by_7/
(defun lucky7s (n &aux
(nx0 (floor (+ n 2) 3))
(ny0 (floor (+ n 1) 3))
(nz0 (floor n 3))
(cache (make-hash-table :test 'equal)))
(labels ((nt (k m &aux (n 0) (t0 0) nsub tsub)
(cond ((zerop m)
(values (if (zerop k) 1 0) 0))
@lispm
lispm / split-with.lisp
Created September 12, 2015 00:10
simpler CL split-with implementation than in dash.el
(defun split-with (pred list)
"splits the LIST at the first NIL result of PRED"
(values (loop while (and (not (null list))
(funcall pred (first list)))
collect (pop list))
list))
(defun map-while (predicate function list)
(loop with e
while (not (null list))
@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))
(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)))