Re: [racket-dev] An Improvement to for/set
On Sun, Feb 19, 2012 at 22:20, Matthias Felleisen wrote: > for/set sounds and looks more uniform with the rest of the loops, no? I disagree, I think for/union is a natural analogue for for/product and for/sum. Both for/union and for/set should be in the set library. > union you can throw in as a function or for/fold. I found that I use for/union often enough that it's worth writing a for/union macro instead of rewriting the same for/fold boilerplate code every time. However, I'm not sure that I understand what you're suggesting here. -- Dan King College of Computer and Information Science Northeastern University _ Racket Developers list: http://lists.racket-lang.org/dev
Re: [racket-dev] An Improvement to for/set
for/set sounds and looks more uniform with the rest of the loops, no? union you can throw in as a function or for/fold. On Feb 17, 2012, at 1:30 PM, J. Ian Johnson wrote: > 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" > To: "Racket Dev List" > 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 > _ > Racket Developers list: > http://lists.racket-lang.org/dev _ Racket Developers list: http://lists.racket-lang.org/dev
Re: [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" To: "Racket Dev List" 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 _ Racket Developers list: http://lists.racket-lang.org/dev
[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