Skip to content

Instantly share code, notes, and snippets.

Avatar
👋
Hey! Hope you're having an awesome day :-)

Peter Taoussanis ptaoussanis

👋
Hey! Hope you're having an awesome day :-)
View GitHub Profile
View reagent-component.cljs
(defn component
"Experimental! A Reagent 'component' is a (fn [& [props children this]]) that:
* May have special metadata for React lifecycle methods.
* Returns either Hiccup data, or a nested (usu. post-setup) component[1].
This util makes writing Reagent components a little more convenient:
(component
:render (fn [node_ & cmpt-args]) -> Hiccup data, or nested component[1].
;; Additional methods optional:
:did-mount (fn [node])
@ptaoussanis
ptaoussanis / loc-tree.clj
Created May 4, 2014
Alternative `loc-tree` strategy
View loc-tree.clj
(def ^:private loc-tree
(let [loc-tree*
(memoize
(fn [loc & [unpadded?]]
(let [loc-parts (str/split (-> loc locale-key name) #"[-_]")
loc-tree (mapv #(keyword (str/join "-" %))
(take-while identity (iterate butlast loc-parts)))
loc-tree-padded (into (vec (repeat (- 3 (count loc-tree)) nil))
loc-tree)]
(if unpadded? loc-tree loc-tree-padded))))]
@ptaoussanis
ptaoussanis / CLJS-866.clj
Last active Aug 29, 2015
Faulty ns macro desugaring
View CLJS-866.clj
(comment
;; Bug report for CLJS-721 (support :include-macros true modifier in :require),
;; Ref. http://dev.clojure.org/jira/browse/CLJS-721,
;; http://goo.gl/MQ3fWd (GitHub commit de6ee41b3)
;; desugar-ns-specs from clojurescript/src/clj/cljs/analyzer.clj
;; (`cljs.analyzer` ns)
(desugar-ns-specs '[(:require [foo.bar :as bar :include-macros true])])
;; =>
View mutabots.clj
(ns mutabots
"Reimplementation of transducers, in terms of processing functions instead
of reducing functions.
tl;dr: reducing-fn based transducers are a special case, influenced by reducers,
of processing-fn based transducers.
In Clojure 1.7.0-alpha2, transducers are expressed in terms of the existing
concept of reducing functions.
To sum it up, a transducer has currently the signature :
@ptaoussanis
ptaoussanis / clj-1.8.0-alpha2-tuple-perf.clj
Last active Aug 29, 2015
Clojure 1.8.0-alpha2 tuple performance
View clj-1.8.0-alpha2-tuple-perf.clj
;;
;; LATEST UPDATE: 25 July 2015
;;
;; ****************************************************************
;; ** NB false alarm! My original benchmarks showing large perf **
;; ** improvements with tuples turned out to be noise, **
;; ** unfortunately. Current (+more reliable) numbers seem[1] to **
;; ** show no consistent significant advantage using currently **
;; ** available tuple implementations against real-world code. **
;; ** **
View oget.cljs
(defn oget
"Like `aget` for JS objects, Ref. https://goo.gl/eze8hY. Unlike `aget`,
returns nil for missing keys instead of throwing."
([o k] (when o (gobj/get o k nil)))
([o k1 k2] (when-let [o (oget o k1)] (gobj/get o k2 nil))) ; Optimized common case
([o k1 k2 & ks] (when-let [o (oget o k1 k2)] (apply oget o ks)))) ; Can also lean on optimized 2-case
@ptaoussanis
ptaoussanis / smart_memoize.clj
Created Jun 14, 2012
More powerful memoize for Clojure
View smart_memoize.clj
(def ^:private gc-sm-cache!
"Maintains maximum cache size by intelligently pruning less valuable items."
(let [gc-running? (atom false)]
(fn [cache ttl max-items now]
(when-not @gc-running?
(reset! gc-running? true)
(let [snapshot @cache
@ptaoussanis
ptaoussanis / resource_modified.clj
Created Jun 15, 2012
Fn to check for modified resource files
View resource_modified.clj
(def some-file-resources-modified?
"Returns true iff any of the files backing given resources have changed
since this function was last called."
(let [times (atom {})]
(fn modified?
([resource-name & more] (some modified? (cons resource-name more)))
([resource-name]
(when-let [^File file (try (->> resource-name io/resource io/file)
(catch Exception _ nil))]
(let [last-modified (.lastModified file)]
@ptaoussanis
ptaoussanis / gist:3045473
Created Jul 4, 2012
Change standard Timbre appenders to use println instead of atomic (buffering) printer
View gist:3045473
(let [new-fn (ƒ [{:keys [more] :as args}]
(apply println (prefixed-message args) more))]
(set-config! [:appenders :standard-out :fn] new-fn)
(set-config! [:appenders :standard-out-or-err :fn] new-fn))
@ptaoussanis
ptaoussanis / declare-remote.clj
Created Dec 31, 2012
Make forward declarations in any namespace to circumvent circular dependency limitations.
View declare-remote.clj
(defmacro declare-remote
"Declares the given ns-qualified names, preserving symbol metadata. Useful for
circular dependencies."
[& names]
`(do ~@(map (fn [n]
(let [ns (namespace n)
v (name n)
m (meta n)]
`(do (in-ns '~(symbol ns))
(declare ~(with-meta (symbol v) m))))) names)
You can’t perform that action at this time.