Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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"
share|improve this question

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!


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

share|improve this answer
@gnat: Nice edits; thanks! – Frank Shearar Aug 18 '12 at 9:59

While Clojure does not have first-class continuations or coroutines built-in as a core feature, it is possible to implement your own.

For example, core.async is a Clojure library that implements the CSP (Concurrent Sequential Processes) model. It uses a go macro to transform the code within to a state machine. While not exactly coroutines per se, it can be used to implement the same patterns.

There is also pulley.cps, a macro compiler I've authored that transforms (via cps/cps-fn macros) Clojure code written in direct style into continuation-passing style. To the best of my knowledge, it is the most complete continuation-themed Clojure program. It supports dynamic binding, exceptions, calling back and forth between native and transformed code (though the continuation is not maintained across contexts). At the moment, only abortive continuations (i.e., traditional call-cc) are supported, but I do have plans to implement delimited continuations in the future.

While pulley.cps does not directly provide coroutines per se, with call-cc it is relatively straight-forward to implement your own. In fact, one of the examples is a simple implementation of cooperative multitasking. This is further built upon in the CSP example. There is also a Ping-Pong example, but it's more an example of tail-call optimization than coroutines.

Of course, these sorts of transformations are most effective when applied to the whole program. Unfortunately, this isn't possible with macros alone, which are localized. Still, even localized transformations can be very effective.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.