Skip to content

Instantly share code, notes, and snippets.

@joekarma
joekarma / gist:3785568
Created September 26, 2012 02:00
Printing to Emacs from Hunchentoot
(defpackage :printing-from-hunchentoot
(:use :cl))
(in-package :printing-from-hunchentoot)
(defparameter *my-acceptor* (make-instance 'hunchentoot:acceptor :port 8080))
(defparameter *out* *standard-output*)
(hunchentoot:start *my-acceptor*)
@joekarma
joekarma / gist:3754531
Created September 20, 2012 07:57
RESTAS Example
(eval-when (:compile-toplevel :load-toplevel)
(ql:quickload :restas))
(restas:define-module :my-site
(:use :cl))
(in-package :my-site)
(restas:define-route hello ("/hello")
"Hello!")
@joekarma
joekarma / gist:3688592
Created September 10, 2012 03:01
Read `n' characters from stream.
(defun read-n-characters (n &optional (stream *standard-input*))
(let ((seq (make-string n)))
(read-sequence seq stream)
seq))
@joekarma
joekarma / gist:3550384
Created August 31, 2012 08:44
Using CSS Selectors To Scrape Data With Common Lisp
(ql:quickload '(:drakma :closure-html :cxml :stp-query))
(defun get-before-and-after-pictures (page-number)
"Fetches relative urls for before and after pictures from a thread on Mark's Daily Apple."
(mapcar (alexandria:rcurry #'$:attr "src")
($:query "blockquote.postcontent > img"
(chtml:parse
(drakma:http-request (format nil "http://www.marksdailyapple.com/forum/thread6138-~D.html" page-number))
(cxml-stp:make-builder)))))
@joekarma
joekarma / gist:3549907
Created August 31, 2012 07:33
CSS Selectors (Not Working)
(ql:quickload '(:drakma :closure-html :cxml :css-selectors))
(let ((page (chtml:parse
(drakma:http-request "http://www.google.com")
(cxml-dom:make-dom-builder))))
(css:query "div input" page))
;;
;; Fails with the following error message:
;;
@joekarma
joekarma / gist:3333401
Created August 12, 2012 17:53
fugly antipattern
function handleMessage(message) {
try { /* master handler */
try { /* sub handler */
try { /* sub sub handler */
if (understandsMessage(message, "sub-sub-handler")) {
handleMessage(message, "sub-sub-handler");
} else {
throw "can't understand";
}
} catch (e) { throw e; }
@joekarma
joekarma / gist:3133596
Created July 18, 2012 01:56
I couldn't find a convenient way to have the HTML monkeylib-html emits output directly to a string, so fashioned a patch of sorts for sbclrc. This was probably a mistake. Oh well!
;;; Monkey patch for the monkey lib
(defun monkeylib-html::emit-html-to-string (html)
(with-output-to-string (s)
(let ((monkeylib-text-output:*text-output* s))
(monkeylib-html:emit-html html))))
(export 'monkeylib-html::emit-html-to-string
(find-package :monkeylib-html))
@joekarma
joekarma / fizzbuzz.lisp
Created April 22, 2012 06:05 — forked from codeforkjeff/fizzbuzz.lisp
fizzbuzz in Common Lisp
;; Discovered via http://www.adampetersen.se/articles/fizzbuzz.htm
;; “Write a program that prints the numbers from 1 to 100. But for
;; multiples of three print “Fizz” instead of the number and for the
;; multiples of five print “Buzz”. For numbers which are multiples of
;; both three and five print “FizzBuzz”.”
(defun multiple-p (n multiple)
(zerop (mod n multiple)))
(defun fizzbuzz ()
@joekarma
joekarma / gist:2417772
Created April 19, 2012 01:39
Return a list of functions defined in a given package.
(defun functions-in-package (&optional (package-name (package-name *package*)))
"Return a list of functions available in a given package."
(let ((symbols nil))
(do-symbols (symbol package-name)
(when (fboundp symbol)
(push symbol symbols)))
symbols))
(defun functions-defined-in-package (&optional (package-name (package-name *package*)))
"Return a list of functions that are defined in a given package."
@joekarma
joekarma / .ccl-init.lisp
Created April 11, 2012 07:40
A minor init file modification to make quickproject more convenient for git users.
(ql:quickload '(:quickproject
:external-program))
(push
(lambda (pathname &rest args)
(setf (current-directory) (fad:pathname-as-directory pathname))
(rename-file "README.txt" "README.markdown")
(external-program:run "git"
(list "init" "."))
(external-program:run "git"