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 / joins.clj
Last active December 22, 2015 22:39
joins
(def admins #{{:name "owen" :id 1} {:name "paul" :id 2} {:name "bazzz" :id 3}})
(def orgs #{{:name "xively" :id 1} {:name "cosm" :id 2} {:name "patchbay" :id 3}})
(def groups #{{:name "admins" :id 1} {:name "owners" :id 2} {:name "playas" :id 3}})
(def joins #{{:admin-id 1 :org-id 1 :group-id :1}
{:admin-id 2 :org-id 1 :group-id :1}
{:admin-id 2 :org-id 2 :group-id :2}
{:admin-id 3 :org-id 3 :group-id :3}})
(defn join-tables [src-key dst-key dst-table val]
(let [;; "select distinct dst-key from joins where src-key = val"
@martintrojer
martintrojer / app.clj
Last active December 22, 2015 12:39
reloaded
(ns app
(:gen-class))
(defn -main [& args]
(require 'myapp.core)
(eval `(apply myapp.core/old-main ~args)))
(ns dbtest.datomic
(:require [datomic.api :as d]))
(def uri "datomic:mem://test")
(d/delete-database uri)
(d/create-database uri)
(def conn (d/connect uri))
(def urif "datomic:free://localhost:4334/test")
(d/delete-database urif)
@martintrojer
martintrojer / Makefile
Created August 27, 2013 06:54
download stuff on windows
all: iedl.exe
iedl.exe: iedl.cpp
g++ -g iedl.cpp -o iedl.exe -l wininet
clean:
rm -rf iedl.exe
@martintrojer
martintrojer / memo_ttl.clj
Created August 16, 2013 07:20
memoize-ttl
;; http://dev.clojure.org/jira/browse/CMEMOIZE-8
(defn memoize-ttl [f time-to-live]
(let [mem (atom {})]
(fn [& args]
(let [[_ [r t]] (find @mem args)]
(if (and t (<= (- (System/currentTimeMillis) t) time-to-live))
r
(let [ret (apply f args)]
(swap! mem assoc args [ret (System/currentTimeMillis)])
@martintrojer
martintrojer / cj-http-proxy.clj
Created July 19, 2013 07:29
clj-http through proxy
(clj-http.client/get "http://www.google.com" {:proxy-host "inetproxy3" :proxy-port 8080 :digest-auth ["<u>" "<p>"]})
@martintrojer
martintrojer / bad-go-walker.clj
Last active December 19, 2015 21:19
core.async non-tailrecursive
(defn walk [tree ch]
(letfn [(walker [t]
(go
(when t
(walker (:left t))
(>! ch (:value t))
(walker (:right t)))))]
(walker tree)
(close! ch)))
@martintrojer
martintrojer / blocking-thread.clj
Last active March 4, 2017 20:59
core.async blocking IO
(time
(def data-thread
(let [c (chan)
res (atom [])]
;; fetch em all
(doseq [i (range 10 100)]
(thread (>!! c (blocking-get (format "http://fssnip.net/%d" i)))))
;; gather results
(doseq [_ (range 10 100)]
(swap! res conj (<!! c)))
@martintrojer
martintrojer / einstein.scala
Last active December 18, 2015 23:19
E=mc^2
// If you took the matter in a teaspoon of water, and converted that to energy,
// how many gallons of gasoline would you have to burn to get an equal amount of energy?
scala> 'teaspoon * 'water * ('c ** 2) to 'gallons * 'gasoline
res0: frins.NumberT = 3164209.862836101 [dimensionless]
@martintrojer
martintrojer / ratio.scala
Last active December 18, 2015 21:09
Ratio
package frins
class Ratio(n: BigInt, d: BigInt) {
require(denom != 0)
private val g = gcd(n.abs, d.abs)
val numer: BigInt = n / g
val denom: BigInt = d / g
private def gcd(a: BigInt, b: BigInt): BigInt =