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
; juste pour l'art | |
(defn primes [] | |
(keep :prime | |
(iterate (fn [{:keys [known candidate]}] | |
(if (some #(zero? (rem candidate %)) known) | |
{:known known :candidate (inc candidate)} | |
{:known (conj known candidate) :candidate (inc candidate) :prime candidate})) | |
{:known [] :candidate 2}))) |
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
(use '[clojure.walk :only [walk]]) | |
(def ^:dynamic ^:private *frame*) | |
(defn- new-frame [] (atom {})) | |
(defn- collect-frame [ast] | |
(case (:op ast) | |
:constant | |
{:constants [{:value (:form ast)}]} |
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
(def fib-seq | |
"lazy seq of Fibonacci numbers" | |
(lazy-cat [0 1] (map + (rest fib-seq) fib-seq))) | |
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
(def fib-seq | |
"lazy seq of Fibonacci numbers" | |
(lazy-cat [0 1] (map + (rest fib-seq) fib-seq))) | |
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 lein-env | |
"Create a new classlojure based leiningen environment by fetching all jars | |
and classes directories found inside the ccw.leiningen-core plugin." | |
[] | |
(let [leiningen-core (bundle-dir "ccw.leiningen-core") | |
jar (fn [f] (and (.isFile f) (.endsWith (.getName f) ".jar"))) | |
classes-dir (fn [d] (let [manifest (io/file d "META-INF" "MANIFEST.MF")] | |
(and (.exists manifest) | |
(.isFile manifest)))) | |
libs (->> leiningen-core file-seq (filter #(or (jar %) (classes-dir %))))] |
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
(use '[clojure.core.match :only [match]]) | |
(defn evaluate [env [sym x y]] | |
(match [sym] | |
['Number] x | |
['Add] (+ (evaluate env x) (evaluate env y)) | |
['Multiply] (* (evaluate env x) (evaluate env y)) | |
['Variable] (env x))) | |
(def environment {"a" 3, "b" 4, "c" 5}) |
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
(ns agile.life | |
(:use [clojure.pprint :only [pprint]])) | |
;; cell representation | |
(pprint [2 2]) | |
;; game state representation | |
#{[2 2] [3 2]} | |
;; game initial state ("blinker") |
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
; original | |
(defn best-plan [bids] | |
(first (vals (reduce (fn [plans {:keys [DEPART VOL DUREE PRIX]}] | |
(assoc plans DEPART | |
(max-key #(:gain % 0) (first (vals plans)) | |
(let [[[_ {:keys [gain path] :or {gain 0}}]] | |
(subseq plans >= (+ DEPART DUREE))] | |
{:gain (+ gain PRIX) :path (cons VOL path)})))) | |
(sorted-map) (sort-by :DEPART > bids))))) |
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 cheap-merge-with | |
"Merges two maps-as-functions" [f a b] | |
(memoize (fn this | |
([k] (this k nil)) | |
([k default] | |
(let [av (a k this) | |
bv (b k this)] | |
(if (identical? this av) | |
(if (identical? this bv) | |
default |
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 tarjan-stepper | |
[graph] | |
(fn this | |
([[components seen :as state] node] | |
(if (contains? seen node) | |
state | |
(let [[components seen] (this components seen node)] | |
[(conj components (get seen node)) seen]))) | |
([components seen node] | |
(reduce (fn [[components seen] subnode] |