From 91c422bb663c55f936a0ae00004f2e4708081217 Mon Sep 17 00:00:00 2001 From: Vincent St-Amour Date: Wed, 22 Jul 2015 11:21:39 -0500 Subject: [PATCH] Merge unstable/future with racket/future. --- .../scribblings/reference/futures.scrbl | 10 ++++++++ racket/collects/racket/future.rkt | 25 +++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/pkgs/racket-doc/scribblings/reference/futures.scrbl b/pkgs/racket-doc/scribblings/reference/futures.scrbl index b334c6c9e7..aed4951c54 100644 --- a/pkgs/racket-doc/scribblings/reference/futures.scrbl +++ b/pkgs/racket-doc/scribblings/reference/futures.scrbl @@ -126,6 +126,16 @@ execute through a call to @racket[touch], however. } +@deftogether[[ +@defform[(for/async (for-clause ...) body ...+)] +@defform[(for*/async (for-clause ...) body ...+)]]]{ + +Like @racket[for] and @racket[for*], but each iteration of the +@racket[body] is executed in a separate @racket[future], and +the futures may be @racket[touch]ed in any order. +} + + @; ---------------------------------------- @section{Future Semaphores} diff --git a/racket/collects/racket/future.rkt b/racket/collects/racket/future.rkt index cd9e657aac..1b08c8ae97 100644 --- a/racket/collects/racket/future.rkt +++ b/racket/collects/racket/future.rkt @@ -1,5 +1,6 @@ #lang racket/base -(require '#%futures) +(require '#%futures + (for-syntax racket/base)) (provide future? future @@ -13,4 +14,24 @@ fsemaphore-wait fsemaphore-try-wait? would-be-future - futures-enabled?) + futures-enabled? + for/async + for*/async) + +;; Note: order of touches not guaranteed. + +(define-syntaxes (for/async for*/async) + (let () + (define ((transformer for/fold/derived-id) stx) + (syntax-case stx () + [(_ (clause ...) . body) + (quasisyntax/loc stx + (let ([futures + (#,for/fold/derived-id #,stx ([fs null]) (clause ...) + (cons (future (lambda () . body)) fs))]) + ;; touches futures in original order + (let loop ([fs futures]) + (cond [(pair? fs) (begin (loop (cdr fs)) (touch (car fs)))] + [else (void)]))))])) + (values (transformer #'for/fold/derived) + (transformer #'for*/fold/derived))))