As a Java/Ruby guy who is not used to reading inside out, I'm curious
as to whether people who ARE accustomed to LISP find the -> macro
distracting since it flops things around. Are there circumstances
where you prefer it?
Am 27.02.2009 um 19:39 schrieb John D. Hume:
> As a Java/Ruby guy who is not used to reading inside out, I'm curious
> as to whether people who ARE accustomed to LISP find the -> macro
> distracting since it flops things around. Are there circumstances
> where you prefer it?
I have a Scheme background and I like -> a lot. I would prefer it
always over
(foo (bar (baz (frobnicate a-thing)) bla)).
Eh? To which function belongs the bla argument? I think
(-> a-thing frobnicate baz (bar bla) foo)
is much clearer.
Sincerely
Meikel
Consistent syntax is nice, but not very useful if it gets in the way
of readability. Fortunately, Clojure has a consistent method for
designing arbitrary new syntax when needed. -> is not a "special
case". It's a well-documented macro intended to make some kinds of
expressions more readable. The non-macro syntax is "simpler", yes, but
not always easier to read, which is the reason why -> exists.
--
Jarkko
(-> a-thing fun-1 (fun-2) (fun-3 b-thing))
is turned into
(fun-3 (fun-2 (fun-1 a-thing)) b-thing)
So, -> takes the first argument and places it as
first argument in the second argument. If the second
argument is not a list, -> creates automatically a list
the given thing as first argument.
So here are the cases.
(-> x foo) => (foo x)
(-> x (foo)) => (foo x)
(-> x (foo a b c)) => (foo x a b c)
This works recursively. So you can easily chain
functions together. One nice example is that of
nested maps as given by James before in this
thread.
(-> the-map :a :b :c)
is turned into
(:c (:b (:a the-map)))
Now consider the-map to be
{:a {:b {:c 5}}}
Then the above evaluates to 5.
A bit contorted, but I hope this helps.
Sincerely
Meikel
-Stuart Sierra
- Afficher le texte des messages précédents -
Am 01.03.2009 um 02:19 schrieb Belfabius:
> I quickly learned the -> macro can't be used in the same way for one
> simple reason; the value that is threaded through to the forms is
> always placed in the first argument position. How about adding a
> macro -- it could be called |> just as in F# -- that places the value
> in the last argument position? (It's dead simple to write -- I just
> copied and modified the -> macro.)
See here for a in-depth discussion of the pipe macro. :)
http://groups.google.com/group/clojure/browse_frm/thread/66ff0b89229be894/c3d4a6dae45d4852
Sincerely
Meikel