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 / for-comb.clj
Created March 7, 2013 16:01
puzzle solving
(ns cljpuzzle.core
(:use [clojure.math.combinatorics]))
(defn solve-logic-puzzle []
(let [people [:amaya :bailey :jamari :jason :landon]]
(first
(for [[fortune time cosmopolitan us-weekly vogue] (permutations people) ; magazines
[asiago blue-cheese mascarpone mozzarella muenster] (permutations people) ; cheeses
; We bind the reservations in two steps, so we have a name for the overall order
reservations (permutations people)
@martintrojer
martintrojer / clojure-match.clj
Created February 26, 2013 08:26 — forked from cgrand/clojure-match.clj
interpreters
(use '[clojure.core.match :only [match]])
(defn evaluate [env [sym x y]]
(match [sym]
['Number] x
['Add] (+ (evaluate env x) (evaluate env y))
['Multiply] (* (evaluate env x) (evaluate env y))
['Variable] (env x)))
(def environment {"a" 3, "b" 4, "c" 5})
@martintrojer
martintrojer / fme.clj
Last active December 14, 2015 05:40
filter-map-entries
(defn re?
([p]
(fn [s] (re-find (re-pattern p) (str s))))
([p s] ((re? p) s)))
(defn mapentry-matches? [pat [k v]]
(or (re? pat (-> k str string/lower-case))
(re? pat (-> v str string/lower-case))))
;; filter keys and values in json-like nested maps and vectors
@martintrojer
martintrojer / tpe.clj
Last active December 13, 2015 20:08
ThreadPoolExecutor
(def pool (java.util.concurrent.Executors/newFixedThreadPool 2))
(def l1 (java.util.concurrent.CountDownLatch. 1))
(def l2 (java.util.concurrent.CountDownLatch. 1))
(defn worker [] (.await l1) (println "l1") (.await l2) (println "done!"))
(dotimes [_ 4] (.submit pool worker))
(.countDown l1)
;; l1
;; l1
(use '[clojure.core.match :only [match]])
(defn evaluate [env [sym x y]]
(match [sym]
['Number] x
['Add] (+ (evaluate env x) (evaluate env y))
['Multiply] (* (evaluate env x) (evaluate env y))
['Variable] (env x)))
(def environment {"a" 3, "b" 4, "c" 5})
@martintrojer
martintrojer / newtonSqrt.scala
Created October 31, 2012 18:49
sieve of eratosthenes + newton (lazy)
def sqrtStream(x: Double): Stream[Double] = {
def improve(guess: Double) = (guess + x / guess) / 2
lazy val guesses: Stream[Double] = 1 #:: (guesses map improve)
guesses
}
sqrtStream(4).take(10).toList
@martintrojer
martintrojer / mnem encode.scala
Created October 29, 2012 22:20
Scala comb search
import scala.io.Source
object mnem {
val in = Source.fromURL("http://lamp.epfl.ch/files/content/sites/lamp/files/teaching/progfun/linuxwords.txt")
val words = in.getLines.toList filter (word => word forall (c => c.isLetter))
val mnem = Map(
'2' -> "ABC", '3' -> "DEF", 4 -> "GHI", '5' -> "JKL",
'6' -> "MNO", '7' -> "PQRS", '8' -> "TUV", 9 -> "WXYZ")
@martintrojer
martintrojer / goodies.clj
Created September 27, 2012 07:26
core.logic goodies
(defne not-membero [x l]
([_ []])
([_ [h . t]]
(!= x h)
(not-membero x t)))
(defne subseto [s1 s2]
([() _])
([[x . xs] _]
(membero x s2)
@martintrojer
martintrojer / cl-graph.clj
Last active October 11, 2015 02:57
core.logic graph blog
(ns cl-graph
(:refer-clojure :exclude [==])
(:use clojure.core.logic))
;; Directed Acyclic Graphs
(defrel edge a b)
;; a
;; |
@martintrojer
martintrojer / graph_cl.clj
Created September 26, 2012 11:58
core logic graph hacks
(ns graph-cl
(:refer-clojure :exclude [==])
(:use clojure.core.logic))
(defrel edge a b)
(fact edge :b :a)
(fact edge :c :a)
(fact edge :d :b)
(fact edge :d :c)