diff --git a/samples/Clojure/for.clj b/samples/Clojure/for.clj new file mode 100644 index 00000000..725f7b2d --- /dev/null +++ b/samples/Clojure/for.clj @@ -0,0 +1,17 @@ +(defn prime? [n] + (not-any? zero? (map #(rem n %) (range 2 n)))) + +(range 3 33 2) +'(3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) + +;; :when continues through the collection even if some have the +;; condition evaluate to false, like filter +(for [x (range 3 33 2) :when (prime? x)] + x) +'(3 5 7 11 13 17 19 23 29 31) + +;; :while stops at the first collection element that evaluates to +;; false, like take-while +(for [x (range 3 33 2) :while (prime? x)] + x) +'(3 5 7) diff --git a/samples/Clojure/hiccup.hic b/samples/Clojure/hiccup.hic new file mode 100644 index 00000000..318f03da --- /dev/null +++ b/samples/Clojure/hiccup.hic @@ -0,0 +1,8 @@ +[:html + [:head + [:meta {:charset "utf-8"}] + [:link {:rel "stylesheet" :href "css/bootstrap.min.css"}] + [:script {:src "app.js"}]] + [:body + [:div.nav + [:p "Hello world!"]]]] diff --git a/samples/Clojure/into-array.cljc b/samples/Clojure/into-array.cljc new file mode 100644 index 00000000..a1c9fa29 --- /dev/null +++ b/samples/Clojure/into-array.cljc @@ -0,0 +1,13 @@ +(defn into-array + ([aseq] + (into-array nil aseq)) + ([type aseq] + (let [n (count aseq) + a (make-array n)] + (loop [aseq (seq aseq) + i 0] + (if (< i n) + (do + (aset a i (first aseq)) + (recur (next aseq) (inc i))) + a))))) diff --git a/samples/Clojure/protocol.cljs b/samples/Clojure/protocol.cljs new file mode 100644 index 00000000..5496ac0c --- /dev/null +++ b/samples/Clojure/protocol.cljs @@ -0,0 +1,15 @@ +(defprotocol ISound (sound [])) + +(deftype Cat [] + ISound + (sound [_] "Meow!")) + +(deftype Dog [] + ISound + (sound [_] "Woof!")) + +(extend-type default + ISound + (sound [_] "... silence ...")) + +(sound 1) ;; => "... silence ..." diff --git a/samples/Clojure/rand.cljscm b/samples/Clojure/rand.cljscm new file mode 100644 index 00000000..ca07579d --- /dev/null +++ b/samples/Clojure/rand.cljscm @@ -0,0 +1,5 @@ +(defn rand + "Returns a random floating point number between 0 (inclusive) and + n (default 1) (exclusive)." + ([] (scm* [n] (random-real))) + ([n] (* (rand) n))) \ No newline at end of file diff --git a/samples/Clojure/svg.cljx b/samples/Clojure/svg.cljx new file mode 100644 index 00000000..dd2206d4 --- /dev/null +++ b/samples/Clojure/svg.cljx @@ -0,0 +1,20 @@ +^:clj (ns c2.svg + (:use [c2.core :only [unify]] + [c2.maths :only [Pi Tau radians-per-degree + sin cos mean]])) + +^:cljs (ns c2.svg + (:use [c2.core :only [unify]] + [c2.maths :only [Pi Tau radians-per-degree + sin cos mean]]) + (:require [c2.dom :as dom])) + +;;Stub for float fn, which does not exist on cljs runtime +^:cljs (def float identity) + +(defn ->xy + "Convert coordinates (potentially map of `{:x :y}`) to 2-vector." + [coordinates] + (cond + (and (vector? coordinates) (= 2 (count coordinates))) coordinates + (map? coordinates) [(:x coordinates) (:y coordinates)])) diff --git a/samples/Clojure/unit-test.cl2 b/samples/Clojure/unit-test.cl2 new file mode 100644 index 00000000..bac21586 --- /dev/null +++ b/samples/Clojure/unit-test.cl2 @@ -0,0 +1,20 @@ +(deftest function-tests + (is (= 3 + (count [1 2 3]))) + (is (= false + (not true))) + (is (= true + (contains? {:foo 1 :bar 2} :foo))) + + (is (= {"foo" 1, "baz" 3} + (select-keys {:foo 1 :bar 2 :baz 3} [:foo :baz]))) + + (is (= [1 2 3] + (vals {:foo 1 :bar 2 :baz 3}))) + + (is (= ["foo" "bar" "baz"] + (keys {:foo 1 :bar 2 :baz 3}))) + + (is (= [2 4 6] + (filter (fn [x] (=== (rem x 2) 0)) [1 2 3 4 5 6])))) +