Skip to content

Instantly share code, notes, and snippets.

@jbr
jbr / 0-notes.md
Created February 17, 2011 00:39
Tail recursion special-form for sibilant

Some caveats about this method:

  • The "call" in the name of _tco_ is a lie. It's tail recursion, not tail call optimization. I'll update the name if I include this in sibilant.
  • It drops some boilerplate (the _tco_ function) at the top, which is something I don't feel too good about.
  • It only works from a tail position — if you don't return (recur ...), it does nothing.

It can be enabled either globally:

(enable-tco)

(defun infinite-loop () (recur)) ;=> infinite loop, no stack

;; ----------clojure beginner question------------
;; Let's say I have a vector of unique values or a
;; set and want to create a map with those as keys
;; and some transformation of them as values.
;; What's the idiomatic way to do this?
;; I'm using the following, but it seems there
;; should be a better way.
(defn build-map [initial-keys map-fn]
@jbr
jbr / switch.rb
Created February 12, 2011 11:15
ruby 1.9.2 case-like switch
# In response to:
# http://mlomnicki.com/ruby/tricks-and-quirks/2011/02/10/ruby-tricks2.html
# Ruby 1.9.2 has some neat stuff that lets us make a readable
# alternative case statement that calls each method in turn.
# 1.9.2 features used:
# * hashes are ordered in 1.9.2
# * cool JSON-style hash syntax
# * concise lambda syntax
@jbr
jbr / throw-errors.js
Created January 11, 2011 21:27
in response to recent nodejs post
// In response to this idiom
// (code copied from https://groups.google.com/d/topic/nodejs/wzSUdkPICWg/discussion):
mainWindow.menu("File", function(err, file) {
if (err) throw err;
file.openMenu(function(err, menu) {
if (err) throw err;
menu.item("Open", function(err, item) {
if (err) throw err;
item.click(function(err) {
@jbr
jbr / 1-doozer.lisp
Created November 28, 2010 10:17
Just a little builder.
; this depends on node-proxy. npm install node-proxy.
; https://github.com/brickysam26/node-proxy
(defvar builder (hash))
(defun builder.add (type &rest remainder)
(defvar tagname type
attributes ""
contents "")
@jbr
jbr / google-json-rss-feed-with-jquery.js
Created November 27, 2010 02:53
load an rss feed w/ jquery through google
$(function () { //assuming jquery and an <OL id="blog"></OL>
$.loadRSS = function (url, callbackName, num) {
$(document.body)
.append('<script src="' +
'https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=' +
url +
'&callback=' + callbackName + '&num='+num || 10+'">' +
'</script>')
}
(setq load-path (cons "~/.emacs.d" load-path))
(setq load-path (cons "~/.emacs.d/textmate.el" load-path))
(setq load-path (cons "~/.emacs.d/color-theme-6.6.0" load-path))
(setq load-path (cons "~/.emacs.d/magit" load-path))
(when window-system
(set-default-font
(concat "-apple-Anonymous_Pro-"
"medium-normal-normal-"
#!/bin/bash
gcc -o src/html_document.o -c\
-I/opt/local/include/libxml2\
-I/opt/local/include\
src/html_document.cc\
-I/usr/nodejs/v0.2.0/include/node/
gcc -o src/html_parser.o -c\
-I/opt/local/include/libxml2\
$ make
scons: Reading SConscript files ...
Checking for node.js ...(cached) yes
True
Checking for C++ library xml2... no
Did not find libxml2, exiting!
gmake: *** [node] Error 1
(defvar app (send (require 'express) create-server))
(app.get "/" (lambda (req res) (res.send "Hello World")))
(app.listen 8888)