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
================ | |
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" |
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
;;; 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 |
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
;;; 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. |
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
; 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 ( #{}, #""... ) |
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
; 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)) |
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 ^{: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] |
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 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 |
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 ^{: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." |
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 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) |
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 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)] |
OlderNewer