Skip to content

Instantly share code, notes, and snippets.

Business Models

Advertising

Models Examples
Display ads Yahoo!
Search ads Google

Clojure Destructuring Tutorial and Cheat Sheet

(Related blog post)

Simply put, destructuring in Clojure is a way extract values from a datastructure and bind them to symbols, without having to explicitly traverse the datstructure. It allows for elegant and concise Clojure code.

Vectors

@gberenfield
gberenfield / output.png
Created June 30, 2011 06:15
Pixelizing images with ChunkyPNG - codebrawl #2
output.png
@gberenfield
gberenfield / gist:2052257
Created March 16, 2012 20:00
Week 1 - problem #2
(ns codelesson.week1
(:require [clojure.math.combinatorics :as mc]))
(defn add-up [coeffs terms]
"ax + by + cz + ...
this adds up a Specific set of coefficients for terms(coins)"
(reduce + (map #(* %1 %2) coeffs terms)))
(defn coeffs [dollar coin]
"return a sequence of coeffs from 0 to the divisor of the dollar amt by the coin
@gberenfield
gberenfield / gist:2115756
Created March 19, 2012 15:11
Week 1 - problem #3
(ns codelesson.week1-3)
(defn debugme [cp cd rp rd]
(prn cp cd )
(prn rp)
(prn rd)
(println "-------------"))
(defn nomore [a b]
(and (empty? a) (empty? b)))
@gberenfield
gberenfield / week3-1.clj
Created March 30, 2012 17:40
week 3 - rover part 2
(ns codelesson.week3-1
(:require [clojure.string])
(:gen-class))
(defn merge-with-padding [x y]
" merge arrays x & y and padding the shorter of the two with nils "
(let [cx (count x) cy (count y)]
(if (not= cx cy)
(if (< cx cy)
(map #(vector %1 %2) (concat x (take (- cy cx) (repeat nil) )) y)
@gberenfield
gberenfield / week3-2.clj
Created April 8, 2012 01:50
Fantasy Banking week 3-2
(ns codelesson.week3-2)
(def fees { :checking {:interest 0.02 :overdraft 0.10}
:savings {:interest 0.04 :overdraft 0.075}
:money-market {:interest 0.06 :overdraft 0.05}})
(def overall-overdraft (atom 100000))
(def accounts (atom []))
(defn random-account-type []
(rand-nth [:checking :savings :money-market]))
@gberenfield
gberenfield / week4_1.clj
Created April 13, 2012 15:35
Fantasy Banking week 4 using Swing
(ns codelesson.week4-1
(:require clojure.string)
(:import (org.slf4j.impl.StaticLoggerBinder))
(:import (javax.swing JFrame JPanel JButton JLabel JTextField JTextArea JScrollPane))
(:import java.awt.event.ActionListener)
(:import (javax.swing.event DocumentListener))
(:import (java.awt GridBagLayout GridBagConstraints Dimension))
(:import (java.util.concurrent TimeUnit)))
(def fees { :checking {:interest 0.02 :overdraft 0.10}
@gberenfield
gberenfield / gist:4606244
Created January 23, 2013 14:24
codox troubles seen in 0.6.4 when running for lib-noir
λ lein deps ✭
Retrieving codox/codox/0.6.4/codox-0.6.4.pom from clojars
Retrieving codox/codox.leiningen/0.6.4/codox.leiningen-0.6.4.pom from clojars
Retrieving codox/codox/0.6.4/codox-0.6.4.jar from clojars
Retrieving codox/codox.leiningen/0.6.4/codox.leiningen-0.6.4.jar from clojars
[:~/clojure/git/lib-noir on master]
λ lein doc ✭
Exception in thread "main" java.lang.AssertionError: Assert failed: (vector? (:dependencies project []))
at leinjacker.deps$add_if_missing.invoke(deps.clj:43)
at leiningen.d
@gberenfield
gberenfield / gist:4607301
Created January 23, 2013 14:55
codox 0.6.4 after lein clean
[:~/clojure/git/lib-noir on master]
λ lein clean 1 ↵ ✭
[:~/clojure/git/lib-noir on master]
λ lein deps ✭
[:~/clojure/git/lib-noir on master]
λ lein doc ✭
Exception in thread "main" java.lang.AssertionError: Assert failed: (vector? (:dependencies project []))