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._ | |
class JSON extends JavaTokenParsers { | |
def obj: Parser[Map[String, Any]] = "{" ~> repsep(member, ",") <~ "}" ^^ (Map() ++ _) | |
def arr: Parser[List[Any]] = "[" ~> repsep(value, ",") <~ "]" | |
def member: Parser[(String, Any)] = stringLiteral ~ ":" ~ value ^^ | |
{ case name ~ ":" ~ value => (name, value) } |
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
object queens extends App { | |
def queens(n: Int): List[List[(Int, Int)]] = { | |
def placeQueens(k: Int): List[List[(Int, Int)]] = { | |
if (k == 0) | |
List(List()) | |
else for { | |
queens <- placeQueens(k - 1) | |
col <- 1 to n | |
queen = (k, col) |
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
object exprPattern extends App { | |
sealed trait Expr | |
case class Add(e1: Expr, e2: Expr) extends Expr | |
case class Sub(e1: Expr, e2: Expr) extends Expr | |
case class Num(n: Int) extends Expr | |
def value(e: Expr): Int = e match { | |
case Add(e1, e2) => value(e1) + value(e2) | |
case Sub(e1, e2) => value(e1) - value(e2) | |
case Num(n) => n |
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
object BST extends App{ | |
case class Tree[T](left: Option[Tree[T]], right: Option[Tree[T]], value: T) { | |
def toList(): List[T] = { | |
def tol: (Tree[T] => List[T]) = { | |
case Tree(Some(l), _, v) => tol(l) ::: List(v) | |
case Tree(None, Some(r), v) => v :: tol(r) | |
case Tree(None, None, v) => List(v) | |
} | |
tol(this) |
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
;; [commons-codec "1.7"] | |
(ns b64uuid | |
(:import [java.util UUID] | |
[java.nio ByteBuffer] | |
[org.apache.commons.codec.binary Base64])) | |
(defn uuid->b64 [^UUID uuid] | |
(let [ba (-> (ByteBuffer/wrap (make-array Byte/TYPE 16)) | |
(.putLong (.getMostSignificantBits uuid)) |
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
;; filter keys and values in json-like nested maps and vectors | |
(defn filter-map-entries [pred m] | |
(loop [acc {}, [[k v] & tail] (if (map? m) (seq m) (map seq m))] | |
(if k | |
(cond (pred [k k]) (recur (conj acc [k v]) tail) | |
(map? v) (let [submap (filter-map-entries pred v)] | |
(if (empty? submap) | |
(recur acc tail) | |
(recur (conj acc [k submap]) tail))) | |
(vector? v) (let [ms (->> v (map #(if (coll? %) |
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 size [o] | |
(-> o pr-str count)) | |
(defn col-size [c] | |
(let [s (if (map? c) (seq c) (map-indexed vector c))] | |
(->> s | |
(map (fn [[k v]] [k (size v)])) | |
(sort-by second) | |
reverse))) |
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
(ns pdftstr.core | |
(:import [com.snowtide.pdf OutputTarget PDFTextStream])) | |
(defn get-text [filename] | |
(with-open [pdfts (PDFTextStream. filename)] | |
(let [txt (StringBuilder. 1024)] | |
(.pipe pdfts (OutputTarget. txt)) | |
(str txt)))) | |
(get-text "kalle.pdf") |
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
;;http://snellm.github.io/cutlet/ | |
(ns cljutlet.core | |
(:use [clojure.data.zip.xml]) | |
(:require [clojure.xml :as xml] | |
[clojure.zip :as zip])) | |
(def data (zip/xml-zip (xml/parse "people.xml"))) | |
(for [p (xml-> data :person)] |
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
(use 'frinj.repl) | |
(override-operators!) | |
(fj :inch :to :cm) | |
(fj :centi) | |
(fj :yottagrams) | |
(fj :centimeter) | |
(fj :c) | |
(fj :mm) |