Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Amazing. What's the ->> operator called?


A monad ;-)

No, but seriously, you can think of (->> foo bar baz quux)

As a way of saying:

    Within the context of computing one thing and passing 
    that result to the next thing with a common carrier 
    object of "foo", do bar, then baz, then quux within
    that context, each operating on foo.
In VB you could do:

    with someFile
        readIt()
        processIt()
        writeIt()
See the similarity?

By the way, monads are a way to abstract this even further so that not only can you model the context of "do something to a thing then do the next thing then do the next", but almost every other kind of context you could think of.

EDIT: as has been pointed out, this isn't really a monad, but it's an example of something monad-like that helps me grasp the larger concept better. Also, fixed the syntax.


It's not a monad, it's an ordinary macro. Also, you got the syntax wrong. What you wrote expands to (bar baz quux foo).

To GP: I've heard it called "double arrow" or "post-threading operator".


(->>) is not really a monad, though. It's a concatenation of endomorphisms, which sort of has the smell of a monad, but is simpler.


Not necessarily endomorphisms, though, is it?


True, and then it's a category.


Well, hmm, I don't think that's quite right either. There is a category where the objects are clojure types and the arrows are clojure functions, but I don't see how the threading operator embodies it. There could totally be some perspective I'm missing, though.


No, I think I'm just playing more fast and loose than I should. I don't think it fits into any particular semantic mould because it's really a syntactic thing—it is a list processing function that's applied to source code represented as lists. The basic usage pretty much traces out a path in the Clj category, so perhaps if you built Paths(Clj) then (->>) is a forgetful functor from Paths(Clj) -> Clj. In which case it's almost a monad, since if you play with Paths and (->>) you can turn them into a forgetful/free adjunction pair and make a monad.


Hmm, I think that's right. Heh.




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

Search: