Skip to content

Instantly share code, notes, and snippets.

View jackrusher's full-sized avatar

Jack Rusher jackrusher

View GitHub Profile
TO GET THE DATA
===============
$ ftp ftp.ncdc.noaa.gov
[... login as user 'ftp', password is your email address ...]
ftp> cd pub/data/gsod
250 CWD command successful
ftp> get ish-history.txt
[... transfer info ...]
ftp> cd 2013
(def stl-test (decode stl-codec (to-buf-seq (nio/mmap "data/duck.stl"))))
(defn setup []
(frame-rate 5)
(smooth))
(defn draw []
(frame-rate 5)
(background 20)
(no-stroke)

This drives me crazy, and I see something like it in almost every literary document corpus:

<blockquote>
  <i>
    <q data-eblatype="startmarker" data-eblasegid="38073">[</q>
      Act I, Scene 3
    <q data-eblatype="endmarker" data-eblasegid="38073">]</q>
 
(def date-parsers [nil (java.text.SimpleDateFormat. "yyyy") (java.text.SimpleDateFormat. "yyyy-M") (java.text.SimpleDateFormat. "yyyy-M-dd")])
(def grains [:decade :year :month :day]) ;; what's decade doing?
(map #(let [[_ year month day] (re-find #"(\d{2,4})[-_]([\dx]{2})[-_]([\dx]{2})" %)
date (first (split-with (partial not= "xx") [(if (= (count year) 2) (str "19" year) year) month day]))]
[(.parse (parsers (count date)) (clojure.string/join "-" date)) (grains (count date))])
["thefilehasaname__1939-xx-01.mp3"
"thefilehasaname__39_01_01.mp3"
"thefilehasaname__39-01-01.mp3"
@jackrusher
jackrusher / dbpedia.clj
Created June 1, 2014 07:14
Some leftover code from a livecoding data gathering/analysis in Clojure workshop I taught in Berlin a few days ago. (N.B. This code is meant to help one understand the concepts involved -- if one were to put something like this in production, it'd be much better to use one of the many excellent open source libraries available for this purpose.)
;; semantic web data is often formatted as RDF/XML, but I think that
;; format is horrible, so I generally prefer to use something like
;; n-triples (N3):
;; https://dvcs.w3.org/hg/rdf/raw-file/default/rdf-turtle/n-triples.html
;; for the sake of this gist we'll limit ourselves to the flavor of N3
;; produced by dbpedia
(def fetch-n3
"Fetch N3 data for a given dbpedia resource. Expects a URL like
@jackrusher
jackrusher / tact.clj
Last active August 29, 2015 14:02
A quick example of talking to StudioNAND's tactile sensor with clojure.
(ns livecode.tact
(:require [serial-port :as serial])
(use quil.core))
;; what ports are there?
;; (serial/list-ports)
;; the serial port of the tact device
(def port (serial/open "/dev/tty.usbmodem1421"))
(define (update-in ht ks v)
(cond [(empty? (cdr ks)) (hash-set ht (car ks) v)]
[else (hash-set ht (car ks) (update-in (hash-ref ht (car ks)) (cdr ks) v))]))
(define test-hash (hash 'fruit (hash 'apple 'red 'banana 'green)
'veg (hash 'aubergine 'purple)))
(update-in test-hash '(fruit banana) 'yellow)
;; => #hash((fruit . #hash((apple . red) (banana . yellow))) (veg . #hash((aubergine . purple))))
@jackrusher
jackrusher / fold-and-spindle.clj
Last active August 29, 2015 14:06
A few words on fold for a friend who had just read http://www.cs.nott.ac.uk/~gmh/fold.pdf
;; simplest fold definition
(defn fold [f init l]
(if (empty? l) init (fold f (f init (first l)) (rest l))))
;; by this time you are almost certainly comfortable with signatures like:
;; Int -> Int
(fold + 0 [1 2 3 4 5])
;; => 15
@jackrusher
jackrusher / asciify.rkt
Created October 9, 2014 20:57
A quick piece of code to create an ascii version of a bitmap from the web, here demo'd with the face of my friend Paul Ford.
(require 2htdp/image)
(define ascii-by-density '(#\# #\A #\@ #\% #\$ #\+ #\= #\* #\: #\, #\. #\space))
(define (choose-char cl w h x y)
(let* ([pixel (list-ref cl (+ x (* y w)))]
[peak (max (color-red pixel) (color-green pixel) (color-blue pixel))])
(if (zero? peak)
(last ascii-by-density)
(let ([c (round (- (* (length ascii-by-density) (/ peak 255)) 1))])
@jackrusher
jackrusher / gist:34bc7a6f8c4ca41b7766
Last active August 29, 2015 14:22
Automatically preview changes to an SVG in emacs on every save.
(defun refresh-svg-preview ()
(interactive)
(let ((oldbuf (current-buffer)))
(with-current-buffer (get-buffer-create "*svg-preview*")
(fundamental-mode)
(erase-buffer)
(insert-buffer-substring oldbuf)
(image-mode))))
(global-set-key '[(ctrl c) (ctrl %)]