Skip to content

Instantly share code, notes, and snippets.

Christophe Grand cgrand

Block or report user

Report or block cgrand

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View Flattening a nested list in clojure
(defn flat?
"Returns true if seq contains no sequences"
(not-any? (fn [x] (isa? (type x) java.util.List)) seq))
(defn flatten
"Returns an unnested sequence from the non-sequence elements of seq
for example, it turns (1 (2) 3) into (1 2 3)"
(if (isa? (type seq) java.util.List)
View flatten-versions.clj
;;from clojure.contrib.seq-utils
;;'flatten' written by Rich Hickey,
(defn flatten [x]
(filter (complement sequential?)
(rest (tree-seq sequential? seq x))))
;aprox. translate from paul graham's on lisp to clojure
(defn flatten-ol [x]
(letfn [(rec [x acc]
View gist:126676
(set! *warn-on-reflection* true)
(ns slow
(:import [java.nio ByteBuffer Buffer]))
(defn #^ints bytes-to-ints [#^bytes bs]
(let [#^ints is (make-array Integer/TYPE (alength bs))]
(loop [i (int 0)]
(if (< i (alength bs))
View gist:188441
(defn walk-out [customer]
(ref-set customer :outside))
(defn wait [customer {q :queue}]
(alter q conj customer)
(ref-set customer :waiting))
(defn sleeping? [barber]
(= :sleeping (ensure barber)))
View sweeeeeeeeeet! :).clj
(defn euclidean
"berechnet den (modifizierten) euklidischen abstand zwischen zwei usern"
[active other]
(Math/sqrt (reduce + (for [movie (common-items active other)
feat (item-map movie)]
(diff active other movie feat)))))
View unbean.clj
(defn unbean [m]
"Tries to recreate a simple JavaBean object from map m"
(let [obj (.newInstance (:class m))]
(doseq [#^java.beans.PropertyDescriptor pd (-> String java.beans.Introspector/getBeanInfo .getPropertyDescriptors)
:let [wm (.getWriteMethod pd) k (-> pd .getName keyword)] :when (and wm (contains? m k))]
(.invoke wm obj (to-array [(m k)])))
View respond.clj
;; something like that should be closer to what you want
(defmacro defplugin [& body]
(let [cmd-list (into {} (for [[docs words cmdkey] body word words] [word {:cmd cmdkey :doc docs}]))]
~@(for [[docs words cmdkey & method-stuff] body]
`(defmethod respond ~cmdkey ~@method-stuff))
(let [m-name# (keyword (last (.split (str *ns*) "\\.")))]
(alter modules assoc m-name#
{:load #(dosync (alter commands assoc m-name# ~cmd-list))
View cs2.clj
(deftype Timeseries
[series] ; maybe dataset or backing-set or something like that
(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])
View rle.clj
(defn mundane-pack
"Mundane recursive way to pack a sequence"
[[f & r :as S]]
(if (seq S)
(let [[packed tail] (split-with #{f} S)]
(if (seq tail)
(cons packed (pack tail))
View gist:1200354
(defn neighbours [[x y]]
(for [dx [-1 0 1] dy (if (zero? dx) [-1 1] [-1 0 1])]
[(+ dx x) (+ dy y)]))
(defn step [cells]
(set (for [[loc n] (frequencies (mapcat neighbours cells))
:when (or (= n 3) (and (= n 2) (cells loc)))]
(def board #{[2 1] [2 2] [2 3]})
You can’t perform that action at this time.