Skip to content

Instantly share code, notes, and snippets.

View svdberg's full-sized avatar

Sander van den Berg svdberg

View GitHub Profile
@svdberg
svdberg / addressbook
Created January 4, 2011 17:11
Haskell implementation of a addressbook
import Text.CSV
type FirstName = String
type LastName = String
type MiddleName = String
type FullName = String
type PhoneNumber = String
data Contact = Contact
{ firstname :: FirstName
@svdberg
svdberg / Stacking-boxes.clj
Created March 23, 2011 11:35
Attempt to solve the stacking boxes problem
(ns user.core
(:use [clojure.contrib.duck-streams :only (read-lines)]))
(declare lcs) ; Declare the memoized lcs
(def *file-name* "/Users/svdberg/boxes.txt")
(defn longest [xs ys] (if (> (count xs) (count ys)) xs ys))
(defn undecorated-lcs [seqx seqy]
@svdberg
svdberg / blow-stack.clj
Created March 25, 2011 11:25
This blows the stack, how do I fix it?
(defn path
[[x & more :as full-list] orgmat p]
(let [maxsize (count orgmat)
new-row (- (count more) 1)
this-row (+ new-row 1)]
(cond
(empty? full-list) (conj p this-row)
(= x 0) (path more orgmat p)
(= x 1) (if (>= new-row (last p))
(path more orgmat p)
@svdberg
svdberg / clojure-recursion.clj
Created March 25, 2011 17:30
Why does this hang/don't end?
(defn test-test
[[x & more :as list] orgmat p]
(cond
(empty? list) p
(= x 1) (test-test (nth orgmat (count more)) orgmat (conj p (count more)))
(= x 0) (recur more orgmat p)
true (recur more orgmat p)))
orgmat is in form:
((0 0 0 0 0 0 0 0) (1 0 1 0 0 1 0 0) (1 0 0 0 0 1 0 0) (1 1 1 0 0 1 0 0) (1 1 1 1 0 1 0 1) (1 0 0 0 0 0 0 0) (1 1 1 1 1 1 0 1) (1 1 1 1 0 1 0 0))
@svdberg
svdberg / path.clj
Created March 25, 2011 19:11
Doesnt terminate.
(defn path
[[x & more :as full-list] orgmat p]
(let [maxsize (count orgmat)
new-row (- (count more))
this-row (count more)]
(cond
(empty? full-list) p
(= this-row 0) p
(= x 0) (recur more orgmat p)
(= x 1) (if (< new-row (last p))
@svdberg
svdberg / macro.clj
Created April 10, 2011 16:31
how do we handle static methods?
; (to-html (number-a-sequence (map track-to-str (top-tracks lastfm-user-name (lastfm-api-key))))))
(defmacro with-error-handling
[func return_on_ok return_on_fail & args]
`(let [result# (apply func ~args)]
(if (.isSuccessful result#)
~return_on_ok
~return_on_fail)))
;(with-error-handling (fn [a b] (Library/addArtist a b)) "OK" "FAIL" "Murk" (get-session))
@svdberg
svdberg / mnemonic.clj
Created June 6, 2011 07:19
Finding telephone number mnemonics
(ns mnemonics.core
(:require [clojure.string :as str :only (lower-case)])
(:use [clojure.contrib.duck-streams :only (read-lines)]))
(def *number-pairs*
{ 0 "e" 1 "jnq" 2 "rwx" 3 "dsy" 4 "ft" 5 "am" 6 "civ" 7 "bku" 8 "lop" 9 "ghz"})
(defn pair-list
[n cs]
(map #(hash-map % n) cs))
@svdberg
svdberg / prop.clj
Created March 1, 2012 08:43
Clojure property resolver
(ns propertyresolver.core
(:refer-clojure :exclude [char])
( :require [ clojure.java.io])
( :use [the.parsatron]))
(defn load-properties [file-name]
(with-open [^java.io.Reader reader (clojure.java.io/reader file-name)]
(let [props (java.util.Properties.)]
(.load props reader)
(into {} (for [[k v] props] [(keyword k) v])))))
@svdberg
svdberg / proptests.clj
Created March 1, 2012 16:10
Property resolver tests
(ns propertyresolver.test.core
(:use [propertyresolver.core])
(:use [clojure.test])
(:use midje.sweet))
;; "PropertyResolver resolve single reference"
(facts "replace should resolve a single reference"
(let [map-with-placeholder {:jndiname "${jndi.ref}" :jndi.ref "MyQueue"}]
(expand-placeholders map-with-placeholder map-with-placeholder) => {:jndi.ref "MyQueue" :jndiname "MyQueue"}))
@svdberg
svdberg / heapboom.clj
Created March 8, 2012 10:22
Blows the compiler heap, why?
(def pp
(λ [f xs]
(reduce ƒ(concat %1 (list (f %2))) '() xs)) )