Skip to content

Instantly share code, notes, and snippets.

View lispm's full-sized avatar

Rainer Joswig lispm

  • Germany
View GitHub Profile
@lispm
lispm / gist:5816842
Created June 19, 2013 18:47
n at a time
(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))))
(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
@lispm
lispm / gist:7499480
Created November 16, 2013 12:11
Collect items with the same head.
(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)
@lispm
lispm / lottery
Created June 9, 2014 09:13
lottery
;; 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
@lispm
lispm / gist:e028d3f3c11c9f74d4e7
Created December 5, 2014 14:16
some clever sorting
(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)))
@lispm
lispm / gist:5990c341a20003de493b
Last active August 29, 2015 14:10
non-recursive quicksort
;;; 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)))
@lispm
lispm / gist:6066e1eeadf943910c47
Last active August 29, 2015 14:11
longest graph, version without a NODE structure
; https://github.com/logicchains/LPATHBench/blob/master/writeup.md
(eval-when (:load-toplevel :compile-toplevel :execute)
(defstruct route
(dest 0 :type fixnum)
(cost 0 :type fixnum)))
(defun parse-line (line &aux (pos 0) n)
(declare (ignorable n))
(loop repeat 3
@lispm
lispm / gist:e063918e5c354c138922
Last active August 29, 2015 14:11
longest path, without node and route structures
; https://github.com/logicchains/LPATHBench/blob/master/writeup.md
(defun parse-line (line &aux (pos 0) n)
(declare (ignorable n))
(loop repeat 3
collect (multiple-value-setq (n pos)
(parse-integer line :start pos :junk-allowed t))))
(defparameter *file* "agraph")
@lispm
lispm / gist:e9372894519f8e6feae1
Last active August 29, 2015 14:11
longest path, route and node structures, but no visited array
;;; optimizations copyright Rainer Joswig, 2014, [email protected]
;;; Original: https://github.com/logicchains/LPATHBench/blob/master/writeup.md
;;; Structure declarations
;; In Common Lisp the slot declarations might save space for some types. But
;; that might not make it faster, since access gets more complicated..
;; It also might take more time, when type checks are done at runtime.
;; Some implementations check slot updates for correct types under some
;; SAFETY optimization values.
@lispm
lispm / gist:4b200a7a5a7f5c3fd911
Last active August 29, 2015 14:12
day of week
; https://github.com/d4gg4d/it-factors/blob/master/day-of-the-week.lisp
(defvar *month-to-code*
'(nil 1 4 4 0 2 5 0 3 6 1 4 6))
(defun fetch-month-code (month)
(nth month *month-to-code*))
(defvar *code-to-day*
'("Saturday" "Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday"))