Skip to content

Instantly share code, notes, and snippets.

View laurentpetit's full-sized avatar

Laurent Petit laurentpetit

View GitHub Profile
@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)]
@laurentpetit
laurentpetit / gist:1507832
Created December 21, 2011 21:39
FooBarQiz in Clojure
;; Note 1: in a real project, would have had 3 files: one for the code, one for the tests, one for the test data
;; Note 2: you can test online at http://tryclj.com/ : copy/paste the (do ...) encapsulating the 3 defs (lines 7 to 21).
;; Then hit Enter to evaluate. Then type (main), then hit Enter to evaluate.
(ns foobarqiz)
(do ;; trick to ease the use of copying/pasting into http://tryclj.com
(def data (sorted-map 7 "Qix", 5 "Bar", 3 "Foo"))
(defn serialize
@laurentpetit
laurentpetit / gist:1510229
Created December 22, 2011 12:58
FooBarQiz in Clojure with more decomposed steps
;; Note 1: in a real project, would have had 3 files: one for the code, one for the tests, one for the test data
;; Note 2: you can test online at http://tryclj.com/ : copy/paste the (do ...) encapsulating the 3 defs (lines 8 to 55).
;; Then hit Enter to evaluate. Then type (main), then hit Enter to evaluate.
(ns foobarqix
(:require [clojure.string :as str]))
(do ;; trick to ease the use of copying/pasting into http://tryclj.com
(def FBQ