Skip to content

Instantly share code, notes, and snippets.

View ulises's full-sized avatar

Ulises Cervino Beresi ulises

View GitHub Profile
(let [{:keys [a b] :or {a :a b :b}} +] [a b]) ; [:a :b]
user=> ((let [a :a] a) {:a :foo})
:foo
user=> (defn foo [& children] children)
#'user/foo
user=> (defn bar [f & children] (println children) (f children))
#'user/bar
user=> (bar foo 'first 'second 'third)
(first second third)
((first second third))
user=>
(defn mock [& {:keys [value delay log? wrap throw-after-count]}]
{:pre [(not (and value wrap))]}
(let [state (ref {:call-count 0
:call-args '()})]
(with-meta (fn [& args]
(do
(if log?
(log/info "Calling mock " (inc (:call-count @state))
" with " args))
(dosync
(defn line-parser []
(let [re #" \[(\S+)\] (\S+) "
formatter (tformat/formatter "dd/MMM/YYYY:HH:mm:ss.SSS")
coercers [#(int (coerce/to-epoch (tformat/parse formatter %)))
identity]
fields [:time :frontend-name]]
(fn [line]
(apply assoc nil (interleave
fields
(map #(%1 %2)
faust.core> ((parser "resources/parsing-rules") "a")
Parse error at line 1, column 1:
a
^
Expected:
#"[0-9]+" (followed by end-of-string)
faust.core> ((parser "resources/parsing-rules") "1")
[:NUMBER "1"]
faust.core>
(deftest parsing-chars
(testing "can parse valid chars for identifiers"
(is (= [:SCHAR "@"] (parser "@")))
(is (= [:SCHAR "#"] (parser "#")))))
(deftest ignore-leading-spaces
(testing "leading space doesn't break the things"
(is (= [:SCHAR "#"] (parser " #")))))
faust.core> (parse "a")
[:SEXP [:ATOM [:LETTER "a"]]]
faust.core> (parse "\"a\"")
[:SEXP [:STRING "\"a\""]]
faust.core> (parse "1")
[:SEXP [:NUMBER [:DIGIT "1"]]]
faust.core> (parse "!")
[:SEXP [:ATOM [:SYMBOL "!"]]]
faust.core>
faust.core> ((comp type second) (parse "atoms!"))
clojure.lang.Symbol
faust.core> ((comp type second) (parse "42"))
java.lang.Long
faust.core> ((comp type second) (parse "\"ohai\""))
java.lang.String
faust.core>
SEXP = SPACE* (ATOM | STRING | NUMBER)
STRING = #"\"(\\\"|[^\"])*\""
ATOM = (LETTER | SYMBOL)+ (LETTER | DIGIT | SYMBOL)*
NUMBER = (DIGIT+) DIGIT*
<LETTER> = (#"[a-zA-z]")*
<SYMBOL> = (#"[!$%&|*+-/:<=?>@^_~#]+")*
<DIGIT> = #"[0-9]+"
<SPACE> = <#"[ ]*">