Skip to content

Instantly share code, notes, and snippets.

View swannodette's full-sized avatar

David Nolen swannodette

View GitHub Profile
@swannodette
swannodette / cljs-binding-problem
Created December 20, 2011 21:40
Clojurescript problem with binding
; file one.cljs
(ns one)
(declare ^:dynamic *something*)
(defn with-something [func]
(binding [*something* 42] (func)))
(defn print-something []
(.log js/console *something*))
@swannodette
swannodette / image.clj
Created December 12, 2011 21:11 — forked from alamar/image.clj
a slow cycle
(import '[java.awt.image BufferedImage])
(defn create-image [w h]
(let [image (BufferedImage. w h BufferedImage/TYPE_INT_RGB)]
(loop [x (dec w)]
(when (>= x 0)
(loop [y (dec h)]
(when (>= y 0)
(.setRGB image (int x) (int y) (int 0))
(recur (dec y))))
@swannodette
swannodette / match.agda
Created October 25, 2011 03:58 — forked from bmmoore/match.agda
Horrible pattern matching hack
{-# OPTIONS --universe-polymorphism #-}
module match where
open import Data.List hiding (or)
open import Category.Monad
open import Category.Functor
open import Data.Maybe
open import Data.Bool
open import Data.Nat
open import Relation.Binary.PropositionalEquality
open import Relation.Nullary
@swannodette
swannodette / force-realized.clj
Created September 23, 2011 02:23 — forked from ibdknox/force-realized.clj
Slow cljs loops
(ns cljs-test.client)
(defn now []
(. (js/Date.) (getTime)))
(let [nums (doall (range 0 1000000))
start (now)]
(doseq [n nums]
)
(. js/console (log (- (now) start))))
@swannodette
swannodette / Coder.scala
Created September 7, 2011 16:54 — forked from retronym/Coder.scala
Martin Odersky's Phone Coder example
package demo
class Coder(words: List[String]) {
private val mnemonics = Map(
'2' -> "ABC", '3' -> "DEF", '4' -> "GHI", '5' -> "JKL",
'6' -> "MNO", '7' -> "PQRS", '8' -> "TUV", '9' -> "WXYZ")
/** Invert the mnemonics map to give a map from chars 'A' ... 'Z' to '2' ... '9' */
private val charCode: Map[Char, Char] =
@swannodette
swannodette / gist:1179446
Created August 29, 2011 21:23 — forked from scode/gist:1179436
lazy generator
(defn- inner-generate [n]
(if (= n 10)
nil
(cons n (lazy-seq (inner-generate (inc n))))))
(defn generate []
(lazy-seq (inner-generate 0)))
@swannodette
swannodette / gist:1164237
Created August 23, 2011 03:02 — forked from pitluga/gist:1164228
simple validation
(defn validate [params]
(letfn [(required [errs k]
(if (nil? (k params))
(assoc errs k "is required") errs))]
(reduce required {} [:model :pk])))
;; def validate(params_hash)
;; errors = {}
;; errors[:model] = "is required" if params_hash[:model].nil?
;; errors[:pk] = "is required" if params_hash[:pk].nil?
(match x
(1 |
2 |
3) :a0
1 :a1)
(match [x y]
([1 2] |
[2 3] |
[3 4]) :a0
(match [x y]
([1 2] |
[2 3] |
[3 4]) :a0
[10 20] :a1)
;; or
(let [z [x y]]
(match [z]
(use '[clojure.java.io :only [reader]])
(use '[clojure.string :only [split]])
(def read-keys [:id :status :pos-contig :pos-position :pos-strand :neg-contig :neg-position :neg-strand])
(defn split-line [x]
(split x #"\t"))
(defn create-read [x]
(with-meta