Skip to content

Instantly share code, notes, and snippets.

View spacebat's full-sized avatar

Andy Kirkpatrick spacebat

  • Adelaide, South Australia
View GitHub Profile
@spacebat
spacebat / sq.lisp
Last active November 10, 2016 23:46
(defmacro sq (variable value &optional (documentation nil documentation-p))
"REPL-friendly alternative-to/merger-of setq and defparameter."
(assert (and variable (symbolp variable)))
(alexandria:once-only (value)
`(cond
((boundp ',variable)
,@(when documentation-p
`((setf (documentation ',variable 'variable) ,documentation)))
(setf ,variable ,value))
(t
(defpackage :99-bottles
(:use :cl)
(:export #:print-song))
(in-package :99-bottles)
(defun wrap (number)
(if (minusp number)
(+ number 100)
number))
@spacebat
spacebat / uncurry.lisp
Last active September 27, 2016 07:41
Curry with uncurry in common lisp
(defvar *uncurry* nil "When bound and non-nil, curried functions can be uncurried")
(makunbound '*uncurry*)
(defun curry (function &rest args)
(lambda (&rest more-args)
(if (and (boundp '*uncurry*) *uncurry*)
(if more-args
(error "No more args expected")
function)
(apply function (append args more-args)))))
@spacebat
spacebat / gist:4f0c6b0fd30b534671eed3946954fb99
Created September 13, 2016 00:56
Clojure's definline in CL
(defmacro definline (name args &body body)
`(progn
(defmacro ,name ,args ,@body)
(setf ,name (lambda ,args (,name ,@args)))))
CL-USER> (definline myor (a b) `(or ,a ,b))
#<anonymous interpreted function 40500011FC>
CL-USER> (myor t (error "boom!"))
T
CL-USER> (apply myor '(nil t))
@spacebat
spacebat / function-wrappers.lisp
Created August 27, 2016 23:32
Poor man's defadvice
;; Simple and rough substitute for defadvice :around on SBCL. Should
;; be easy to port by substituting something appropriate for
;; sb-introspect:function-lambda-list
;; Its easy to get confused with wrapped functions - if you redefine a
;; function while it is wrapped, it will still seem to be wrapped
;; (entry in the hash table for the name) but won't be.
(defvar *wrapped-functions* (make-hash-table))
;; Naive and inefficient en/decoding of IPv6 addresses according to RFC-1924
;; Parsting/emitting hex-encoded IPv6 addresses in the usual format not implemented
(defparameter *digits* "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~")
(defun ip-encode (address)
(loop with value = address
with chars
as (quotient remainder) = (multiple-value-list (floor value 85))
do (setf value quotient)
@spacebat
spacebat / lisp-exploration.html
Created July 12, 2016 14:17
HTML export of lisp-exploration.org
<!DOCTYPE html>
<html>
<head>
<title>lisp-exploration</title>
<!-- 2016-07-12 Tue 14:15 -->
<meta charset="utf-8">
<meta name="generator" content="Org-mode">
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center; }
<!DOCTYPE html>
<html>
<head>
<title>exploration</title>
<!-- 2016-07-11 Mon 09:04 -->
<meta charset="utf-8">
<meta name="generator" content="Org-mode">
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center; }
@spacebat
spacebat / plump-error.lisp
Created June 16, 2016 05:33
Demonstrate an issue using PLUMP in multiple threads
;; This demonstrates a segfault (maybe counting the nodes doesn't
;; cause it, just makes the occasional fault more likely due to
;; spending time with the tree in each thread)
(ql:quickload :plump)
(ql:quickload :bordeaux-threads)
(defun thrash (&key
(num-threads 100)
(lockedp nil)
@spacebat
spacebat / gist:13ef3c926d40e4d7524591d54f1e2971
Created June 13, 2016 10:50
Workaround boutiquepaws popup
// ==UserScript==
// @name Remove signup form
// @namespace http://ubermonkey.net/
// @version 0.1
// @description Unbreak boutiquepaws
// @author spacebat
// @include /^https:\/\/www\.boutiquepaws\.com\.au\//
// @grant GM_log
// ==/UserScript==