diff --git a/articles/Two_years_with_clojure.html b/articles/Two_years_with_clojure.html index 77c0062..d9a93d5 100644 --- a/articles/Two_years_with_clojure.html +++ b/articles/Two_years_with_clojure.html @@ -21,6 +21,11 @@
  • MongoDB the Destroyer!
  • 9 months of gestation
  • +
  • Long live the new flesh
  • @@ -131,12 +136,63 @@

    Each choice was balanced. In the end some of those choices were changed.

    For example, we don’t use Storm at all now. The power of core.async was far from enough to deal with taking all resources of our machines.

    Today you could see a result here:

    - + +

    Long live the new flesh

    +
    +Long Live the new Flesh
    Long Live the new Flesh
    +
    +

    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

    +
    foo["a"]="value-for-a"
    +foo["b"]="value-for-b"
    +foo["c"]="value-for-c"
    +
    +foreach (i in foo) {v[foo[i]]=i;}
    + +

    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:

    +
    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:

    +
    (-> foo :db second :age)
    +

    Yes, that’s all. The default value in case of problem is nil.

    +

    Merges

    +

    Seriously!!!!!

    +
    (into map1 map2)
    +

    I don’t even want to compare to javascript as it would be ridiculous. Mainly, you can’t2, or you need jQuery and its ugly.

    +

    Syntax

    +

    Learning Clojure syntax take about 3 minutes. It is clean, no fucking comma, semicolons, etc…

    +

    1. Just a great thank you to FPComplete and in particular Michael Snoyman!

    2. +
    3. http://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically


    diff --git a/articles/Two_years_with_clojure.md b/articles/Two_years_with_clojure.md index 3b6e57b..1d4a354 100644 --- a/articles/Two_years_with_clojure.md +++ b/articles/Two_years_with_clojure.md @@ -199,4 +199,87 @@ resources of our machines. Today you could see a result here: - +
    + +
    + + +## 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]: + +### 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 diff --git a/articles/img/videodrome.jpg b/articles/img/videodrome.jpg new file mode 100644 index 0000000..368407a Binary files /dev/null and b/articles/img/videodrome.jpg differ