Skip to content

Instantly share code, notes, and snippets.

View cgrand's full-sized avatar

Christophe Grand cgrand

View GitHub Profile
;; 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
(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 \. \.}+
(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* ")"]
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
@cgrand
cgrand / cs2.clj
Created June 4, 2010 09:42 — forked from ejackson/cs2.clj
(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
(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})
(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
(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))))))))
(deftype Foo []
Object
(equals [this o]
(instance? Foo this)))
(.equals (Foo.) nil)
(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]