example
This commit is contained in:
parent
2f89a70005
commit
8d80c53297
2 changed files with 58 additions and 0 deletions
|
@ -151,6 +151,28 @@ foo[<span class="st">"b"</span>]=<span class="st">"value-for-b&qu
|
||||||
foo[<span class="st">"c"</span>]=<span class="st">"value-for-c"</span>
|
foo[<span class="st">"c"</span>]=<span class="st">"value-for-c"</span>
|
||||||
|
|
||||||
<span class="fu">foreach</span> (i <span class="kw">in</span> foo) {v[foo[i]]=i;}</code></pre>
|
<span class="fu">foreach</span> (i <span class="kw">in</span> foo) {v[foo[i]]=i;}</code></pre>
|
||||||
|
<p>Or doing things like:</p>
|
||||||
|
<pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> foo = [[<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>],[<span class="dv">4</span>,<span class="dv">5</span>,<span class="dv">6</span>]];
|
||||||
|
tmp=<span class="dv">0</span>;
|
||||||
|
<span class="fu">foreach</span> (i <span class="kw">in</span> foo) {
|
||||||
|
<span class="fu">foreach</span> (j <span class="kw">in</span> foo[i]) {
|
||||||
|
tmp += foo[i][j] + <span class="dv">2</span>;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
<span class="kw">return</span> tmp;</code></pre>
|
||||||
|
<p>Now that I am used to reduce and filters this is like a second nature. And the new solution is far better.</p>
|
||||||
|
<p>For example the preceeding example write:</p>
|
||||||
|
<pre class="sourceCode clojure"><code class="sourceCode clojure">(<span class="kw">def</span><span class="fu"> foo </span>[[<span class="dv">1</span> <span class="dv">2</span> <span class="dv">3</span>] [<span class="dv">4</span> <span class="dv">5</span> <span class="dv">6</span>]])
|
||||||
|
(<span class="kw">defn</span><span class="fu"> plus2 </span>[x y] (<span class="kw">+</span> x <span class="dv">2</span>))
|
||||||
|
(<span class="kw">defn</span><span class="fu"> sum </span>[l] (<span class="kw">reduce</span> <span class="kw">+</span> <span class="dv">0</span> l))
|
||||||
|
(sum (<span class="kw">map</span> (<span class="kw">fn</span> [l] (<span class="kw">reduce</span> plus2 <span class="dv">0</span> l)) foo))
|
||||||
|
|
||||||
|
<span class="co">;; or</span>
|
||||||
|
|
||||||
|
(<span class="kw">->></span> foo
|
||||||
|
(<span class="kw">map</span> #(<span class="kw">reduce</span> plus2 <span class="dv">0</span> %))
|
||||||
|
(sum))</code></pre>
|
||||||
|
<p>The code is more modulable, easier to read and to modify.</p>
|
||||||
<ul>
|
<ul>
|
||||||
<li>Java null pointer exception!</li>
|
<li>Java null pointer exception!</li>
|
||||||
<li>Unreadable stacktrace</li>
|
<li>Unreadable stacktrace</li>
|
||||||
|
@ -188,6 +210,7 @@ foo[<span class="st">"c"</span>]=<span class="st">"value-for-c&qu
|
||||||
<li>Set: <code>#{:a :b :c}</code> in javascript sets doesn’t even exists you have to simulate them with Objects: <code>{"a":true, "b":true, "c":true}</code></li>
|
<li>Set: <code>#{:a :b :c}</code> in javascript sets doesn’t even exists you have to simulate them with Objects: <code>{"a":true, "b":true, "c":true}</code></li>
|
||||||
<li>inline function declaration; compare <code>#(* % 2)</code> in clojure with <code>function(x){return x * 2;}</code> in javascript</li>
|
<li>inline function declaration; compare <code>#(* % 2)</code> in clojure with <code>function(x){return x * 2;}</code> in javascript</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
<p>TODO: faudrait m’aider à en rajouter quelques tonnes. Avec de meilleurs exemples.</p>
|
||||||
<section class="footnotes">
|
<section class="footnotes">
|
||||||
<hr />
|
<hr />
|
||||||
<ol>
|
<ol>
|
||||||
|
|
|
@ -222,6 +222,39 @@ foo["c"]="value-for-c"
|
||||||
foreach (i in foo) {v[foo[i]]=i;}
|
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!
|
- Java null pointer exception!
|
||||||
- Unreadable stacktrace
|
- 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.
|
- 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}`
|
- 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
|
- 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.
|
||||||
|
|
Loading…
Reference in a new issue