[racket-dev] An Improvement to for/set
I would prefer a simpler for/union:
(define-syntax (for/union stx)
(syntax-case stx ()
[(_ clauses . body)
#'(for/fold/derived stx ((accum-set (set))) clauses
(set-union accum-set (let () . body))]))
-Ian
----- Original Message -----
From: "Daniel King" <danking at ccs.neu.edu>
To: "Racket Dev List" <dev at racket-lang.org>
Sent: Friday, February 17, 2012 1:07:25 PM GMT -05:00 US/Canada Eastern
Subject: [racket-dev] An Improvement to for/set
I've noticed myself desiring a for/set that would allow me to
optionally add zero, one, or more elements to the accumulated set
during each iteration. Is this something that other people would be
interested in having in the set library? If so, I can send a pull
request to the github repo.
My implementation should be backwards compatible with the old one. I
also created set-add* because I found myself wanting to add arbitrary
numbers of elements to sets as well.
;; set-add* : [Set X] X ... -> [Set X]
(define (set-add* s . vs)
(for/fold ((s s)) ((v vs))
(set-add s v)))
(define-syntax (for/set stx)
(syntax-case stx ()
[(_ clauses . body)
#'(for/fold/derived stx ((accum-set (set))) clauses
(call-with-values (lambda () . body)
(curry set-add* accum-set)))]))
Examples:
> (for/set ((x '(1 2 3 4 5)))
(sqr x))
(set 1 4 9 16 25)
> (for/set ((x '(1 2 3 4 5)))
(if (odd? x) x (values)))
(set 1 3 5)
--
Dan King
College of Computer and Information Science
Northeastern University
_________________________
Racket Developers list:
http://lists.racket-lang.org/dev