Does Clojure have continuations/coroutines/etc

clojurefunctional programming

I started programming with Python, and I was really confused by concepts like coroutines and closures.

Now I think I know them on some superficial level, but I've never felt that "enlightement" moment, so I choose to learn Clojure. I bought the book by Stuart Halloway and it's good, but when I looked at the index there were no words like coroutine or continuation. I googled them, but there's nothing there either.

So, my question is:

Does Clojure have continuations or coroutines to do tasks like ping-ponging without a stack overflow?

Python example (though standard Python does not support a fully-featured version of this symmetric coroutine):

def ping():
  while 1:
   print "ping"
   function to switching to pong

def pong():
  while 1:
   function to switching to ping
   print "pong"

Best Answer

Clojure doesn't have call/cc, but you don't want undelimited continuations anyway.

We argue against call/cc as a core language feature, as the distinguished control operation to implement natively relegating all others to libraries. The primitive call/cc is a bad abstraction -- in various meanings of 'bad' shown below, -- and its capture of the continuation of the whole program is not practically useful. The only reward for the hard work to capture the whole continuation efficiently is more hard work to get around the capture of the whole continuation. Both the users and the implementors are better served with a set of well-chosen control primitives of various degrees of generality with well thought-out interactions...

...Offering call/cc as a core control feature in terms of which all other control facilities should be implemented turns out a bad idea. Performance, memory and resource leaks, ease of implementation, ease of use, ease of reasoning all argue against call/cc. If there is really one distinguished control feature to implement as a primitive, with others relegated into libraries, it is not call/cc.

David Nolen wrote a delimited continuations library for Clojure. Try it out!

delimc

A delimited continuations library for Clojure 1.4.0 (and 1.3.0). Portions based on cl-cont by Slava Akhmechet (http://defmacro.org)...

Related Topic