This commit is contained in:
Yann Esposito 2015-10-26 17:20:19 +01:00
parent 612124d1e3
commit 2f89a70005
3 changed files with 141 additions and 2 deletions

View file

@ -21,6 +21,11 @@
<li><a href="#mongodb-the-destroyer">MongoDB the Destroyer!</a></li>
<li><a href="#months-of-gestation">9 months of gestation</a></li>
</ul></li>
<li><a href="#long-live-the-new-flesh">Long live the new flesh</a><ul>
<li><a href="#deep-access">Deep access</a></li>
<li><a href="#merges">Merges</a></li>
<li><a href="#syntax">Syntax</a></li>
</ul></li>
</ul></li>
</ul>
</nav>
@ -131,12 +136,63 @@
<p>Each choice was balanced. In the end some of those choices were changed.</p>
<p>For example, we dont use Storm at all now. The power of core.async was far from enough to deal with taking all resources of our machines.</p>
<p>Today you could see a result here:</p>
<iframe src="https://dev.pulse.vigiglo.be/#/vgteam/TV_Shows/dashboard" style="width:100%; border:solid 2px #DDD; padding: none; margin: 20px 0; height: 500px;">
<div class="wrap" style="height: 630px; width: 100%;">
<iframe src="https://dev.pulse.vigiglo.be/#/vgteam/TV_Shows/dashboard" style="width:200%; border:solid 2px #DDD; padding: none; margin: 20px 0; height: 1200px; -ms-zoom:0.5; -moz-transform: scale(0.5); -moz-transform-origin: 0 0; -o-transform: scale(0.5); -o-transform-origin: 0 0; -webkit-transform: scale(0.5); -webkit-transform-origin: 0 0">
</iframe>
</div>
<h2 id="long-live-the-new-flesh">Long live the new flesh</h2>
<figure>
<img src="img/videodrome.jpg" alt="Long Live the new Flesh" /><figcaption>Long Live the new Flesh</figcaption>
</figure>
<p>Difficulties with the new mindset. As everything new, there is a period of adaptation. Typically the most difficult part was to deal with reversed arrays.</p>
<p>In javascript one would write</p>
<pre class="sourceCode javascript"><code class="sourceCode javascript">foo[<span class="st">&quot;a&quot;</span>]=<span class="st">&quot;value-for-a&quot;</span>
foo[<span class="st">&quot;b&quot;</span>]=<span class="st">&quot;value-for-b&quot;</span>
foo[<span class="st">&quot;c&quot;</span>]=<span class="st">&quot;value-for-c&quot;</span>
<span class="fu">foreach</span> (i <span class="kw">in</span> foo) {v[foo[i]]=i;}</code></pre>
<ul>
<li>Java null pointer exception!</li>
<li>Unreadable stacktrace</li>
</ul>
<p>What were the immediate wins!</p>
<h3 id="deep-access">Deep access</h3>
<p>For the brave an true there is the lenses Haskell library. But for clojurist, the basic access function should be good enough.</p>
<p>Lets compare Javascript with Clojure:</p>
<pre class="sourceCode javascript"><code class="sourceCode javascript">foo={<span class="st">&quot;db&quot;</span>: [{<span class="st">&quot;name&quot;</span>:<span class="st">&quot;John Doe&quot;</span>,<span class="st">&quot;age&quot;</span>:<span class="dv">30</span>},{<span class="st">&quot;name&quot;</span>:<span class="st">&quot;Rich&quot;</span>,<span class="st">&quot;age&quot;</span>:<span class="dv">40</span>},{<span class="st">&quot;age&quot;</span>:<span class="dv">20</span>}]
<span class="co">// other stuff , ....</span>
}
<span class="kw">var</span> val = <span class="kw">function</span>() {
x = foo[db];
<span class="kw">if</span> (x) {
<span class="kw">let</span> y = x[<span class="dv">1</span>];
<span class="kw">if</span> (y) {
<span class="kw">return</span> <span class="ot">y</span>.<span class="fu">age</span>;
} <span class="kw">else</span> <span class="kw">return</span> nil;
} <span class="kw">else</span> <span class="kw">return</span> nil;
}();</code></pre>
<p>Yes, you have to manually check at each level if the value is null or not. Without this manual check, your code is going to crash at runtime!</p>
<p>Now lets compare the situation with clojure:</p>
<pre class="sourceCode clojure"><code class="sourceCode clojure">(<span class="kw">-&gt;</span> foo <span class="kw">:db</span> <span class="kw">second</span> <span class="kw">:age</span>)</code></pre>
<p>Yes, thats all. The default value in case of problem is <code>nil</code>.</p>
<h3 id="merges">Merges</h3>
<p><strong>Seriously!!!!!</strong></p>
<pre class="sourceCode clojure"><code class="sourceCode clojure">(<span class="kw">into</span> map1 map2)</code></pre>
<p>I dont even want to compare to javascript as it would be ridiculous. Mainly, you cant<a href="#fn2" class="footnoteRef" id="fnref2"><sup>2</sup></a>, or you need jQuery and its ugly.</p>
<h3 id="syntax">Syntax</h3>
<p>Learning Clojure syntax take about 3 minutes. It is clean, no <em>fucking</em> comma, semicolons, etc…</p>
<ul>
<li>Arrays: <code>[a b c]</code> in javascript <code>[a,b,c]</code> (why the commas?)</li>
<li>Hash Map (Associative arrays): <code>{:key1 value1 :key2 value2}</code> in javascript you need to define an Object and keys are generally strings: <code>{&quot;key1&quot;:value1, &quot;key2&quot;:value2}</code>. Multiline object declaration always have bad number of commas.</li>
<li>Set: <code>#{:a :b :c}</code> in javascript sets doesnt even exists you have to simulate them with Objects: <code>{&quot;a&quot;:true, &quot;b&quot;:true, &quot;c&quot;:true}</code></li>
<li>inline function declaration; compare <code>#(* % 2)</code> in clojure with <code>function(x){return x * 2;}</code> in javascript</li>
</ul>
<section class="footnotes">
<hr />
<ol>
<li id="fn1"><p>Just a great thank you to FPComplete and in particular Michael Snoyman!<a href="#fnref1"></a></p></li>
<li id="fn2"><p><a href="http://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically" class="uri">http://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically</a><a href="#fnref2"></a></p></li>
</ol>
</section>
<hr/>

View file

@ -199,4 +199,87 @@ resources of our machines.
Today you could see a result here:
<iframe src="https://dev.pulse.vigiglo.be/#/vgteam/TV_Shows/dashboard" style="width:100%; border:solid 2px #DDD; padding: none; margin: 20px 0; height: 500px;"></iframe>
<div class="wrap" style="height: 630px; width: 100%;">
<iframe src="https://dev.pulse.vigiglo.be/#/vgteam/TV_Shows/dashboard" style="width:200%; border:solid 2px #DDD; padding: none; margin: 20px 0; height: 1200px; -ms-zoom:0.5; -moz-transform: scale(0.5); -moz-transform-origin: 0 0; -o-transform: scale(0.5); -o-transform-origin: 0 0; -webkit-transform: scale(0.5); -webkit-transform-origin: 0 0"></iframe>
</div>
## Long live the new flesh
![Long Live the new Flesh](img/videodrome.jpg)
Difficulties with the new mindset.
As everything new, there is a period of adaptation.
Typically the most difficult part was to deal with reversed arrays.
In javascript one would write
~~~ {.javascript}
foo["a"]="value-for-a"
foo["b"]="value-for-b"
foo["c"]="value-for-c"
foreach (i in foo) {v[foo[i]]=i;}
~~~
- Java null pointer exception!
- Unreadable stacktrace
What were the immediate wins!
### Deep access
For the brave an true there is the lenses Haskell library.
But for clojurist, the basic access function should be good enough.
Let's compare Javascript with Clojure:
~~~ {.javascript}
foo={"db": [{"name":"John Doe","age":30},{"name":"Rich","age":40},{"age":20}]
// other stuff , ....
}
var val = function() {
x = foo[db];
if (x) {
let y = x[1];
if (y) {
return y.age;
} else return nil;
} else return nil;
}();
~~~
Yes, you have to manually check at each level if the value is null
or not. Without this manual check, your code is going to crash at runtime!
Now lets compare the situation with clojure:
~~~ {.clojure}
(-> foo :db second :age)
~~~
Yes, that's all. The default value in case of problem is `nil`.
### Merges
**Seriously!!!!!**
~~~ {.clojure}
(into map1 map2)
~~~
I don't even want to compare to javascript as it would be ridiculous.
Mainly, you can't[^2], or you need jQuery and its ugly.
[^2]: <http://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically>
### Syntax
Learning Clojure syntax take about 3 minutes.
It is clean, no _fucking_ comma, semicolons, etc...
- Arrays: `[a b c]` in javascript `[a,b,c]` (why the 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}`
- inline function declaration; compare `#(* % 2)` in clojure with `function(x){return x * 2;}` in javascript

BIN
articles/img/videodrome.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB