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]}) |
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))))) |