This file contains 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 flat? | |
"Returns true if seq contains no sequences" | |
[seq] | |
(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)" | |
[seq] | |
(if (isa? (type seq) java.util.List) |
This file contains 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
;;from clojure.contrib.seq-utils | |
;;'flatten' written by Rich Hickey, | |
;;see http://groups.google.com/group/clojure/msg/385098fabfcaad9b | |
(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] |
This file contains 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
(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)) | |
(do |
This file contains 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 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))) |
This file contains 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 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))))) |
This file contains 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 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)]))) | |
obj)) |
This file contains 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
;; 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}]))] | |
`(do | |
~@(for [[docs words cmdkey & method-stuff] body] | |
`(defmethod respond ~cmdkey ~@method-stuff)) | |
(dosync | |
(let [m-name# (keyword (last (.split (str *ns*) "\\.")))] | |
(alter modules assoc m-name# | |
{:load #(dosync (alter commands assoc m-name# ~cmd-list)) |
This file contains 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 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 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)) | |
[packed])) | |
[nil])) |
This file contains 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 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)))] | |
loc))) | |
(def board #{[2 1] [2 2] [2 3]}) |
OlderNewer