From 8d80c532974711f1a36cbf9a50a92344a2f24dd5 Mon Sep 17 00:00:00 2001 From: Yann Esposito Date: Mon, 26 Oct 2015 17:36:47 +0100 Subject: [PATCH] example --- articles/Two_years_with_clojure.html | 23 ++++++++++++++++++ articles/Two_years_with_clojure.md | 35 ++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/articles/Two_years_with_clojure.html b/articles/Two_years_with_clojure.html index d9a93d5..b0895ba 100644 --- a/articles/Two_years_with_clojure.html +++ b/articles/Two_years_with_clojure.html @@ -151,6 +151,28 @@ foo["b"]="value-for-b&qu foo["c"]="value-for-c" foreach (i in foo) {v[foo[i]]=i;} +

Or doing things like:

+
var foo = [[1,2,3],[4,5,6]];
+tmp=0;
+foreach (i in foo) {
+  foreach (j in foo[i])  {
+   tmp += foo[i][j] + 2;
+  }
+}
+return tmp;
+

Now that I am used to reduce and filters this is like a second nature. And the new solution is far better.

+

For example the preceeding example write:

+
(def foo [[1 2 3] [4 5 6]])
+(defn plus2 [x y] (+ x 2))
+(defn sum [l] (reduce + 0 l))
+(sum (map (fn [l] (reduce plus2 0 l)) foo))
+
+;; or
+
+(->> foo
+     (map #(reduce plus2 0 %))
+     (sum))
+

The code is more modulable, easier to read and to modify.

+

TODO: faudrait m’aider à en rajouter quelques tonnes. Avec de meilleurs exemples.


    diff --git a/articles/Two_years_with_clojure.md b/articles/Two_years_with_clojure.md index 1d4a354..99a3d2b 100644 --- a/articles/Two_years_with_clojure.md +++ b/articles/Two_years_with_clojure.md @@ -222,6 +222,39 @@ foo["c"]="value-for-c" foreach (i in foo) {v[foo[i]]=i;} ~~~ +Or doing things like: + +~~~ {.javascript} +var foo = [[1,2,3],[4,5,6]]; +tmp=0; +foreach (i in foo) { + foreach (j in foo[i]) { + tmp += foo[i][j] + 2; + } +} +return tmp; +~~~ + +Now that I am used to reduce and filters this is like a second nature. +And the new solution is far better. + +For example the preceeding example write: + +~~~ {.clojure} +(def foo [[1 2 3] [4 5 6]]) +(defn plus2 [x y] (+ x 2)) +(defn sum [l] (reduce + 0 l)) +(sum (map (fn [l] (reduce plus2 0 l)) foo)) + +;; or + +(->> foo + (map #(reduce plus2 0 %)) + (sum)) +~~~ + +The code is more modulable, easier to read and to modify. + - Java null pointer exception! - Unreadable stacktrace @@ -283,3 +316,5 @@ It is clean, no _fucking_ comma, semicolons, etc... - Hash Map (Associative arrays): `{:key1 value1 :key2 value2}` in javascript you need to define an Object and keys are generally strings: `{"key1":value1, "key2":value2}`. Multiline object declaration always have bad number of commas. - Set: `#{:a :b :c}` in javascript sets doesn't even exists you have to simulate them with Objects: `{"a":true, "b":true, "c":true}` - inline function declaration; compare `#(* % 2)` in clojure with `function(x){return x * 2;}` in javascript + +TODO: faudrait m'aider à en rajouter quelques tonnes. Avec de meilleurs exemples.