This file contains hidden or 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
;; follow up to http://clj-me.cgrand.net/2010/05/08/destructuring-records-prototypes-and-named-arguments/ | |
user=> (defrecord Foo [a b]) | |
user.Foo | |
user=> (defprotocol Seq (my-seq [this]) (my-seq? [this])) | |
Seq | |
user=> (extend-protocol Seq | |
clojure.lang.ISeq | |
(my-seq [this] (.seq this)) | |
(my-seq? [this] true) | |
Object |
This file contains hidden or 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 net.cgrand.parsley.test | |
(:require [net.cgrand.parsley.glr :as core] :reload) | |
(:use net.cgrand.parsley :reload) | |
(:use clojure.test)) | |
(def sexp | |
(parser {:space :whitespace? | |
:main :expr*} | |
:expr- #{:atom :list :vector :set :map} | |
:atom (token {\a \z \A \Z \- \- \0 \9 \. \.}+ |
This file contains hidden or 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
(def sexp | |
(parser {:space [#{:whitespace :comment :discard}*] | |
:main :expr*} | |
:expr- #{:atom :list :vector :set :map :string :regex | |
:meta :deprecated-meta} | |
:atom (token {\a \z \A \Z \- \- \0 \9 \. \.}+ | |
(?! {\a \z \A \Z \- \- \0 \9 \. \.})) | |
:string (token \" #{(not-one-of \\ \") [\\ any-char]}* \") | |
:regex (token \# \" #{(not-one-of \\ \") [\\ any-char]}* \") | |
:list ["(" :expr* ")"] |
This file contains hidden or 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
user=> (defn f [s] (.substring s 0 1)) | |
#'user/f | |
user=> (time (dotimes [_ 1e5] (f "abc"))) | |
"Elapsed time: 621.784682 msecs" | |
nil | |
user=> (time (dotimes [_ 1e5] (f "abc"))) | |
"Elapsed time: 491.772837 msecs" | |
nil | |
user=> (defn f [^String s] (.substring s 0 1)) | |
#'user/f |
This file contains hidden or 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
(deftype Timeseries | |
[series] ; maybe dataset or backing-set or something like that | |
Object | |
(equals [this other]) ; should a TS be equal to any kind of coll holding same value or to other TS only? (the easiest answer is "any kind of coll") | |
(hashCode [this]) ; depends on ^^ | |
(toString [this]) | |
;; http://github.com/richhickey/clojure/blob/master/src/jvm/clojure/lang/IPersistentCollection.java |
This file contains hidden or 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 response | |
"Returns a skeletal Ring response with the given body, status of 200, and no | |
headers." | |
[body] | |
{:status 200 | |
:headers {"Content-Type" "text/html; charset=UTF-8"} | |
:body body}) |
This file contains hidden or 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
(defmacro defhintedfn [name args & body] | |
(let [iface (gensym "HintedFn") | |
hname (vary-meta name assoc :tag iface) | |
vname (vary-meta name assoc | |
:inline (fn [& args] `(.invoke ~hname ~@args)))] | |
`(do | |
(definterface ~iface | |
(~(with-meta 'invoke {:tag (:tag name Object)}) ~args)) | |
(def ~vname nil) ; workaround for a soon-to-be-fixed bug | |
(def ~vname |
This file contains hidden or 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
(definterface IntToIntFn | |
(#^int call [^int n])) | |
(def fibr | |
(let [one (int 1) | |
two (int 2)] | |
(reify | |
IntToIntFn | |
(call [this n] | |
(if (>= one n) one (+ (.call this (dec n)) (.call this (- n two)))))))) |
This file contains hidden or 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
(deftype Foo [] | |
Object | |
(equals [this o] | |
(instance? Foo this))) | |
(.equals (Foo.) nil) |
This file contains hidden or 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
(comment | |
(cond | |
[[x & xs] (seq s)] foo ; like if-let | |
:let [b 42] | |
test b | |
:else 54) | |
) | |
;; cond could be "augmented" after the preamble in core.clj | |
(defmacro cond [& clauses] |