Does Clojure have continuations/coroutines/etc?

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"

1

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)…

0

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

  • https://clojurebridge.github.io/community-docs/docs/clojure/trampoline/
  • https://jakemccrary.com/blog/2010/12/06/trampolining-through-mutual-recursion/
  • http://makble.com/the-magic-of-clojure-trampoline-function
  • http://pramode.net/clojure/2010/05/08/clojure-trampoline/

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật