This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() ++ _) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn pad-seq | |
"Returns a lazy seq with coll's items followed by pad" | |
[col pad] | |
(lazy-seq (concat col [pad] (pad-seq [] pad)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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))) |