Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d75ef3
commit f8050ef
Showing
4 changed files
with
105 additions
and
23 deletions.
There are no files selected for viewing
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 file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
|
||
|
||
class result(object): | ||
def __init__(self, value, start, end): | ||
self.value, self.start, self.end = value, start, end | ||
|
||
def __str__(self): | ||
return "<result: '%s', [%d:%d]>" % (self.value, self.start, self.end) | ||
|
||
def __repr__(self): | ||
return str(self) | ||
|
||
def word(w): | ||
def _parser(s, i): | ||
if s[i:].startswith(w): | ||
yield result(w, i, i + len(w)) | ||
return _parser | ||
|
||
def any(*args): | ||
def _parser(s, i): | ||
for p in args: | ||
for r in p(s, i): | ||
yield r | ||
return _parser | ||
|
||
def seq(*args): | ||
def _parser(s, i): | ||
pass | ||
return _parser | ||
|
||
m = any(word("piet"), word("piet blaat")) | ||
|
||
|
||
s = "piet blaat aap" | ||
|
||
print list(m(s, 0)) | ||
|
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 file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
(defproject sio "1.0.0-SNAPSHOT" | ||
:description "FIXME: write description" | ||
:description "(web)Socket IO in clojure" | ||
:dependencies [[org.clojure/clojure "1.3.0"] | ||
[org.jboss.netty/netty "netty-4.0.0.Alpha1-SNAPSHOT.jar"]]) | ||
|
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 file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,32 @@ | ||
(ns sio.test) | ||
|
||
(defn hello [] | ||
(println "Hello World!")) | ||
(set! *warn-on-reflection* true) | ||
|
||
(defprotocol Blaat | ||
(aap [this b])) | ||
(deftype State [x v]) | ||
|
||
(deftype X [ax bx] | ||
Blaat | ||
(aap [this b] | ||
(+ ax bx b))) | ||
(deftype Derivative [dx dv]) | ||
|
||
(deftype Y [ax bx] | ||
Blaat | ||
(aap [this b] | ||
(- ax bx b))) | ||
(def FORCE 10) | ||
(def MASS 1) | ||
|
||
(def xx | ||
(let [ax 20 | ||
bx 20] | ||
(reify Blaat | ||
(aap [this b] | ||
(* ax bx b))))) | ||
(println (/ FORCE MASS)) | ||
|
||
(println (aap (X. 10 20) 30)) | ||
(println (aap (Y. 10 20) 30)) | ||
(defn euler [^State s dt] | ||
(let [x (+ (.x s) (* (.v s) dt)) | ||
v (+ (.v s) (* (/ FORCE MASS) dt))] | ||
(State. x v))) | ||
|
||
(println (aap xx 30)) | ||
(defn pstat [t ^State s] | ||
(println t "pos" (.x s) "vel" (.v s))) | ||
|
||
(println (butlast (concat [1 2] [3 4]))) | ||
(defn sim [end step init] | ||
(loop [t 0 | ||
s init] | ||
(when (<= t end) | ||
(let [s2 (euler s step)] | ||
(pstat t s) | ||
(recur (+ t step) s2))))) | ||
|
||
(sim 10 1 (State. 0 0)) | ||
|
||
|
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 file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
(ns sio.test2) | ||
|
||
(set! *warn-on-reflection* true) | ||
|
||
(deftype State [x v]) | ||
|
||
(deftype Derivative [dx dv]) | ||
|
||
(defn spring [k b] | ||
(fn [^State state t] | ||
(- (* (- k) (.x state)) (* b (.v state))))) | ||
|
||
(defn evaluate [^State initial t dt ^Derivative derivative acceleration] | ||
(let [state (State. (+ (.x initial) (* (.dx derivative) dt)) | ||
(+ (.v initial) (* (.dv derivative) dt)))] | ||
(Derivative. (.v state) | ||
(acceleration state (+ t dt))))) | ||
|
||
(defn integrate [^State state t dt acceleration] | ||
(let [^Derivative a (evaluate state t 0 (Derivative. 0 0) acceleration) | ||
^Derivative b (evaluate state t (* dt 1/2) a acceleration) | ||
^Derivative c (evaluate state t (* dt 1/2) b acceleration) | ||
^Derivative d (evaluate state t dt c acceleration) | ||
dxdt (* 1/6 (+ (.dx a) (* 2 (+ (.dx b) (.dx c))) (.dx d))) | ||
dvdt (* 1/6 (+ (.dv a) (* 2 (+ (.dv b) (.dv c))) (.dv d)))] | ||
(State. (+ (.x state) (* dxdt dt)) | ||
(+ (.v state) (* dvdt dt))))) | ||
|
||
|
||
|
||
(def acceleration | ||
(spring 10 1)) | ||
|
||
|
||
(defn sim [steps init dt] | ||
(loop [i steps | ||
t 0 | ||
state init] | ||
(when (> i 0) | ||
(println i (float t) (float (.x state)) (float (.v state))) | ||
(recur (dec i) (+ t dt) (integrate state t dt acceleration))))) | ||
|
||
(sim 100 (State. 100 0) 1/10) |