Skip to content

Instantly share code, notes, and snippets.

View laurentpetit's full-sized avatar

Laurent Petit laurentpetit

View GitHub Profile
================
clojure/core.clj -
================
version originale (avec map):
1:4 paredit.parser=> (dotimes [_ 10] (time (do (-> "/home/lpetit/projects/clojure/src/clj/clojure/core.clj" slurp sexp) nil)))
"Elapsed time: 5391.817183 msecs"
"Elapsed time: 4978.254708 msecs"
"Elapsed time: 4365.472075 msecs"
"Elapsed time: 4478.291098 msecs"
"Elapsed time: 4496.73974 msecs"
@laurentpetit
laurentpetit / gist:759364
Created December 30, 2010 02:03
Dining philosophers solution
;;; Dining philosophers. Solution using Clojure STM.
;;; This is sort of an implementation of the "Use monitor" solution:
;;; http://en.wikipedia.org/wiki/Dining_philosophers_problem#Monitor_solution
;;; What are our identities?
;;; The problem talks about forks, and philosophers.
;;; Conceptually, forks have a "taken-by" property which can have one
;;; of three values: :left-philosopher, :righ-philosopher, :nobody.
;;; Conceptually, philosophers have a "state" property which can be
;;; :eating or :thinking.
;;; Note that with an approach using STM for getting both forks at once
@laurentpetit
laurentpetit / gist:828413
Created February 15, 2011 22:37
Functional Levenshtein distance
;;; The following version works with any seq-able (not only Strings), but hardwires
;;; function = for equality testing of seq values (rather good default IMHO), but also
;;; hardwires the cost of 1 for either element insertion, deletion, or swap.
;;;
;;; It is functional, it does not use arrays, nor a MxN matrix, just the required data
;;; for computing a given "row" of the "virtual matrix" (e.g. the previous row)
;;;
;;; I'm quite sure it can still be improved for better readability and performance
;;; without loosing any of the above mentioned characteristics.
@laurentpetit
laurentpetit / gist:875245
Created March 17, 2011 22:20
paredit.clj's core.clj modified content for smart indent "a la vim/emacs"
; todo
; done 1. emit text deltas, not plain text replacement (or IDEs will not like it)
; done 2. have a story for invalid parsetrees : just do nothing : currently = paredit deactivated if error from start-of-file to area of paredit's work
; 3. use restartable version of the parser
; 4. make paredit optional in ccw
; 5. prepare a new release of ccw
; 6. write with clojure.zip functions the close-* stuff
; 7. write the string related stuff
; ... ?
; . add support for more clojure-related source code ( #{}, #""... )
; Daniel Shiffman's initial example in his PVector + Processing Tutorial
; re-written in clojure
(ns example1
(:use [rosado.processing]
[rosado.processing.applet]))
(set! *warn-on-reflection* true)
(def x (atom 100))
@laurentpetit
laurentpetit / gist:990975
Created May 25, 2011 13:31
Apply let bindings 'til reaching the body, unless an intermediate value ...
=> (def ^{:private true :macro true} assert-args #'clojure.core/assert-args)
#'user/assert-args
=> (defmacro
let-unless
"Apply the bindings and then the body like clojure.core/let would do, unless error-fn,
applied to any intermediate result, returns logical true,
in which case no more binding will be evaluated, and the return value will be the result
of applying handler-fn to the result of error-fn"
{:arglists "([error-fn handler-fn bindings & body])"}
[error-fn handler-fn bindings & body]
(ns net.cgrand.parsley.fold
(:require [net.cgrand.parsley.util :as u]))
+(defn make-leaf [s]
+ (memoize
+ (fn [view]
+ ((:unit view) s))))
+
+(defn make-node [tag children]
+ (memoize
(ns ^{:doc "Conway's Game of Life."}
game-of-life)
;; Core game of life's algorithm functions
(defn neighbours
"Given a cell natural identifier, returns the natural identifiers
of the neighbours of the cell.
In this implementation, the natural identifier of the cell is its [x, y]
coordinates."
@laurentpetit
laurentpetit / gist:1233437
Created September 21, 2011 21:56 — forked from denlab/gist:1231894
coding-dojo-20110921
(defn primes []
((fn primes [candidate seen]
(lazy-seq
(letfn [(prime? [candidate] (when-not (some #(zero? (rem candidate %)) seen) candidate))]
(when-let [candidate (some prime? (iterate inc candidate))]
(cons candidate (primes (inc candidate) (cons candidate seen)))))))
2 ()))
(fact
(primes 1) => (2)
(ns challenge.weights
(:require [clojure.contrib.combinatorics :as c]))
(defn weigh? [i l r]
(let [l-weight (apply + i l)]
(some #(when (= l-weight (apply + %)) [l %]) (set (c/subsets r)))))
(defn diff [l1 l2]
(reduce (fn [l i]
(let [[b [f & r]] (split-with (complement #{i}) l)]