Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View cgrand's full-sized avatar

Christophe Grand cgrand

View GitHub Profile
@cgrand
cgrand / rle.clj
Created March 9, 2011 09:00 — forked from fogus/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))
[packed]))
[nil]))
(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]})
@cgrand
cgrand / gist:1236838
Created September 23, 2011 06:05 — forked from laurentpetit/gist:1233437
coding-dojo-20110921
; 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})))
@cgrand
cgrand / gist:1877266
Created February 21, 2012 16:30 — forked from remleduff/gist:1876575
Simplified AST processing code from CinC
(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)}]}
@cgrand
cgrand / fib-seq-1.clj
Created March 6, 2012 17:35 — forked from scientific-coder/fib-seq-1.clj
Code snippets for Duchess-fr Battle Language 2012-02-29 test it on http://tryclj.com/ !
(def fib-seq
"lazy seq of Fibonacci numbers"
(lazy-cat [0 1] (map + (rest fib-seq) fib-seq)))
@cgrand
cgrand / fib-seq-1.clj
Created March 8, 2012 15:27 — forked from scientific-coder/fib-seq-1.clj
Code snippets for Duchess-fr Battle Language 2012-02-29 test it on http://tryclj.com/ !
(def fib-seq
"lazy seq of Fibonacci numbers"
(lazy-cat [0 1] (map + (rest fib-seq) fib-seq)))
(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 %))))]
@cgrand
cgrand / clojure-match.clj
Created September 5, 2012 14:55 — forked from ckirkendall/clojure-match.clj
Language Compare F#, Ocaml, Scala, Clojure, Ruby and Haskell - Simple AST example
(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})
@cgrand
cgrand / gist:4038631
Created November 8, 2012 12:52 — forked from laurentpetit/gist:4038525
Memento du déroulé Agile Grenoble 2012
(ns agile.life
(:use [clojure.pprint :only [pprint]]))
;; cell representation
(pprint [2 2])
;; game state representation
#{[2 2] [3 2]}
;; game initial state ("blinker")
@cgrand
cgrand / answer.clj
Last active December 11, 2015 06:09 — forked from laurentpetit/gist:4538379
Optimisons sans trahir
; 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)))))