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"

3 Answers 3


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 (http://defmacro.org)...


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.


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

Old question, so I am not even sure whether this feature was available at the time, but for anyone wanting to implement any sort of "ping-pong" functionality, check out trampoline!

I just discovered it as an answer to my question on efficient Continuation-Passing Style in Clojure, here: https://stackoverflow.com/questions/50952443/continuation-passing-style-does-not-seem-to-make-a-difference-in-clojure/50955276#50955276 and I think it's just the job. I'd heard about it a while ago, but never investigated fully. More fool me. Unlike many of the other proposed solutions, it just works.

====== PS. Loads of tutorial information online,]here are a few I found useful

  • 1
    maybe linkify trampoline to point to the documentation?
    – esoterik
    Jun 20, 2018 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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