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
import scala.pickling._
import scala.reflect.runtime.universe._
import scala.util.parsing.json._
import scala.collection.mutable.{StringBuilder, Stack}
package object edn {
implicit val pickleFormat: EdnPickleFormat = new EdnPickleFormat
}
@martintrojer
martintrojer / Atom.scala
Last active December 18, 2015 16:49
Atom
import java.util.concurrent.atomic.AtomicReference
class Atom[T](private val state: AtomicReference[T]) {
def get = state.get()
def swap(f: T => T): T = {
val v = state.get()
val newv = f(v)
if (state.compareAndSet(v, newv)) newv else swap(f)
@martintrojer
martintrojer / EDNParser.scala
Last active December 18, 2015 14:48
EDN Parser
// EDN-ish parser
// https://github.com/edn-format/edn
import scala.util.parsing.combinator._
import java.util.UUID
import java.text.DateFormat
class EDN extends JavaTokenParsers {
val set: Parser[Set[Any]] = "#{" ~> rep(elem) <~ "}" ^^ (Set() ++ _)
val map: Parser[Map[Any, Any]] = "{" ~> rep(pair) <~ "}" ^^ (Map() ++ _)
@martintrojer
martintrojer / future.scala
Last active December 18, 2015 11:09
futures
// map creates a new future that is triggered when the originating future is done
scala> future { 42 } map { _ + 1 } map { println(_) }
43
res26: scala.concurrent.Future[Unit] = scala.concurrent.impl.Promise$DefaultPromise@5427eb13
// flatMap creates a new future that is triggered when the enclosed future is done
scala> future { 42 } flatMap { v => future { 1 + v } } map { println(_) }
res28: scala.concurrent.Future[Unit] = scala.concurrent.impl.Promise$DefaultPromise@9afcbc0
scala> 43
@martintrojer
martintrojer / pad-seq.clj
Last active December 18, 2015 08:39
pad
(defn pad-seq
"Returns a lazy seq with coll's items followed by pad"
[col pad]
(lazy-seq (concat col [pad] (pad-seq [] pad))))
@martintrojer
martintrojer / oc.scala
Created June 7, 2013 21:34
Options are collections
scala> val a = List(Map("a"->1), Map("b"->2), Map("c"->3,"b"->4))
a: List[scala.collection.immutable.Map[String,Int]] = List(Map(a -> 1), Map(b -> 2), Map(c -> 3, b -> 4))
scala> a find (_ contains "b") map (_("b"))
res0: Option[Int] = Some(2)
scala> a find (_ contains "c") map (_("b"))
res1: Option[Int] = Some(4)
scala> a find (_ contains "d") map (_("b"))
@martintrojer
martintrojer / t-in.clj
Last active December 18, 2015 03:39
scheme interpreter timings
;; making Scheme an internal DSL in clojure is quite easy -- and runs at the same speed as clojure.
;; a bit un-fair to compare to the other interpreter-based solutions, but hey
;; http://martintrojer.github.io/clojure/2011/11/29/scheme-as-an-embedded-dsl-in-clojure/
user=> (use 'mtscheme.core)
user=> (define (fact x) (if (= x 0) 1 (* x (fact (- x 1)))))
user=> (time (doseq [_ (range 100)] (fact 20)))
"Elapsed time: 0.677392 msecs"
@martintrojer
martintrojer / sieve-core-async.clj
Last active December 18, 2015 02:39
Sieve of Eratosthenes
(defn prime-filter [in out prime]
(go
(loop [i (<! in)]
(when-not (zero? (mod i prime))
(>! out i))
(recur (<! in)))))
(let [ch (chan)]
(go (loop [i 2] (>! ch i) (recur (inc i))))
@martintrojer
martintrojer / Parser.scala
Last active December 18, 2015 01:59
Scheme Scala
import scala.util.parsing.combinator._
object Parser extends JavaTokenParsers {
def value: Parser[ValueT] = stringLiteral ^^ (x => Name(x.tail.init)) |
floatingPointNumber ^^ (x => Num(BigDecimal(x)))
def expression: Parser[ExprT] = value ^^ (x => Value(x)) |
"""[^()\s]+""".r ^^ (x => Symbol(x)) |
combination
def combination: Parser[Comb] = "(" ~> rep(expression) <~ ")" ^^ (x => Comb(x))
def program: Parser[List[ExprT]] = rep(expression)
@martintrojer
martintrojer / session.clj
Last active December 18, 2015 01:29
The power of dynamic languages
$ lein repl
user=> (def res (slurp "http://www.bbc.co.uk/tv/programmes/genres/drama/scifiandfantasy/schedules/upcoming.json"))
user=> (require 'clojure.data.json 'clojure.walk)
user=> (def json (->> res clojure.data.json/read-str
clojure.walk/keywordize-keys))
user=> (->> json :broadcasts (filter #(>= (:duration %) 6300))
(map :programme) (map (juxt :title :pid)))