Hacker News new | past | comments | ask | show | jobs | submit login

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);



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.


Clojure's standard library already has a threaded placeholder:

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


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);




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: