Skip to content

Instantly share code, notes, and snippets.

View martintrojer's full-sized avatar
🕺
My hovercraft is full of eels

Martin Trojer martintrojer

🕺
My hovercraft is full of eels
View GitHub Profile
@martintrojer
martintrojer / Hello.java
Created May 17, 2012 05:38
clojure-akka-vision
package akka.tutorial.first.java;
import akka.actor.ActorRef;
import akka.actor.UntypedActor;
import akka.actor.UntypedActorFactory;
import akka.actor.ActorSystem;
import akka.actor.Props;
public class Hello {
@martintrojer
martintrojer / project.clj
Created April 26, 2012 08:43
Getting Clojure-ZMQ working
(defproject zguide "1.0.0-SNAPSHOT"
:source-path "../Clojure/"
:description "0MQ zguide in Clojure"
:dependencies [[org.clojure/clojure "1.4.0"]
[org.zmq/zmq "1.0.0"]
])
@martintrojer
martintrojer / tee.clj
Created April 25, 2012 10:40
Tee combinator
;; olabini: Am I blind. Clojure doesn't have a tee-style combinator that allow you to chain side effecting functions without threading values?
(defn tee [& fs] (comp last (apply juxt fs)))
@martintrojer
martintrojer / anagrams.clj
Created April 18, 2012 19:02
Knuth Anagrams
(defn get-anagram-map
"map of words keyed by letter-sorted 'words' (which all anagrams share)"
[dict]
(reduce (fn [acc w]
(let [sw (apply str (sort w))]
(if-let [d (get acc sw)]
(assoc acc sw (conj d w))
(assoc acc sw (list w))))) {} dict))
(defn knuth-anagrams
(defn my-part [n col]
(when-not (empty? col)
(lazy-seq (cons (take n col) (my-part n (drop n col))))))
(->>
(iterate inc 1)
(my-part 4)
(drop 55)
(take 10))
@martintrojer
martintrojer / pascal-trapezoid.clj
Created March 25, 2012 15:21
Pascal's Trapezoid
(defn pascals-trapezoid [v]
(iterate #(map + `(0 ~@%) `(~@% 0)) v))
(take 10 (pascals-trapezoid [1]))
@martintrojer
martintrojer / dfib.clj
Created March 25, 2012 15:20
Fibonacci
(defn fib [n]
(loop [back1 1, back2 0, n n]
(cond
(= n 0) 0
(= n 1) (+ back1 back2)
:else (recur back2 (+ back1 back2) (- n 1)))))
@martintrojer
martintrojer / non-estimate.clj
Created March 25, 2012 15:19
Prime number estimation
(defn prime? [x]
(let [divs (range 2 (inc (int (Math/sqrt x))))
rems (map #(rem x %) divs)]
(not (some zero? rems))))
(filter prime? (range 1000))
@martintrojer
martintrojer / nqueens-cl.clj
Created March 25, 2012 15:05
Enumerate all N-Queens solutions
(ns nqueens-cl
(:refer-clojure :exclude [==])
(:use [clojure.core.logic]))
(defmacro def-subo
"Generate subtraction algebra"
[name n]
(let [xn 'x, yn 'y, rn 'r
gen-association (fn [x y]
`[(== ~xn ~x) (== ~yn ~y) (== ~rn ~(- x y))])
user=> (use 'frinj.repl)
user=> (str (fj 10 :thousand :SEK :to :GBP)) ;; standard currency conversion
"937.1075201531269 [dimensionless]"
user=> (str (fj :Gold :per :Silver)) ;; Gold vs Silver price
"49.95612708018155 [dimensionless]"
user=> (str (fj :Milk))
"0.3659673552268969 dollar kg^-1 [price_per_mass]"