News for hackers. Stuff that matters.
Re:The Beauty of Clojure by icebraining on Wednesday August 26, 2015 @05:50AM (#10121987)
As a Clojure/Lisp beginner, I can't say I agree. I don't know any language that works like your example, except maybe shell script with its pipes. Without variables, that expression just seems too "magical". Nested function calls, on the other hand, are common everywhere.
Re:The Beauty of Clojure by grayrest on Wednesday August 26, 2015 @09:21AM (#10122666)
If it's particularly confusing you can always use swiss arrows[0] instead:

   (-<> (range 10)
        (map #(+ % 2) <>)
        (reduce + <>))

[0] https://github.com/rplevy/swiss-arrows

It's more common in functional languages and called a thrush or pipe operator.

F#:

    [|0..9|] |> Seq.map ((+) 2) |> Seq.sum
Javascript (Ramda):

    R.pipe(R.range(0,10), R.map(R.add(2)), R.sum)();

    //With slightly more magic
    let {pipe, range, map, add, sum} = R;
    pipe(range(0), map(add(2)), sum)(10);
Re:The Beauty of Clojure by icebraining on Wednesday August 26, 2015 @10:31AM (#10123113)
While they might make the flow clearer, I don't think the swiss arrows fix what I perceive as more confusing for a newcomer.

The problem is that -<> looks like a regular function call, but does things that a function call can't do. In a language without macros, which are most of the mainstream ones, <> would have to be a language feature, and then it's not clear why you'd need to use -<> to 'activate' it.

Now, I've read enough about Lisp and AST and such to understand what a macro is, but the concept doesn't map easily to mainstream languages.

Re:The Beauty of Clojure by danneu on Wednesday August 26, 2015 @03:48PM (#10125558)
Clojure's standard library already has a threaded placeholder:

    (as-> (range 10) _
      (map #(+ % 2) _)
      (reduce + _))
Re:The Beauty of Clojure by jdd on Friday August 28, 2015 @07:57PM (#10138414)
JavaScript (lodash-fp):

    _.flow(_.range(0,10), _.map(_.add(2)), _.sum)();

    //With slightly more magic
    let {flow, range, map, add, sum} = _;
    flow(range(0), map(add(2)), sum)(10);
Re:The Beauty of Clojure by nnq on Wednesday August 26, 2015 @09:13AM (#10122628)
...that's how chaining method calls end up working in most OO languages (take the Scala example in OP's article). And also in Javascript (I can't remember if there is something like `range` in js right now...):

    [1,2,3,4,5,6,7,8,9,10]
      .map(function (x) { return x + 2 })
      .reduce(function (a, b) { return a + b })
so the "." in OOP ends up working like "->" or "->>". Heck, you can make the dot in OOP code behave like anything, even like monadic bind (`>>=` or `do` notation in Haskell), if you do OOP-style-monads in JS: https://www.youtube.com/watch?v=b0EF0VTs9Dc

(off-topic: this is also my main problem with OOP... the "." can actually end up meaning anything practically, as program logic is concerned, it's like some kind of "infinite operator overloading" :) )

Re:The Beauty of Clojure by icebraining on Wednesday August 26, 2015 @10:19AM (#10123019)
But (conceptually) the "." in most OO languages is something that is part of the object it's being applied to, while the -> is something external that one applies to the subsequent expressions. At least, this is what I perceive by looking at the code, and in my opinion, the two concepts don't map intuitively.
Re:The Beauty of Clojure by AnimalMuppet on Wednesday August 26, 2015 @01:17PM (#10124508)
No (at least not in one specific OO language, namely C++).

"foo->" means exactly the same thing as "(*foo)." That is, you use "." when you have an object, and "->" when you have a pointer to an object.

I don't know of any other OO language that uses "->". If there is one, my comments here probably do not apply to it...

(Or were you talking about the "." as used in OO, compared with the "->" as used in Clojure? If so, disregard this comment...)

Re:The Beauty of Clojure by icebraining on Wednesday August 26, 2015 @01:35PM (#10124649)
(Or were you talking about the "." as used in OO, compared with the "->" as used in Clojure? If so, disregard this comment...)

I'm afraid I was :)