[racket-dev] [Feature Request] for macro #:when clauses should bind their results if asked.

From: Carl Eastlund (cce at ccs.neu.edu)
Date: Tue Sep 6 14:40:19 EDT 2011

You can also write:

(for*/list
  ([(k v) (in-hash ht)]
   [res (in-value (f k v))]
   #:when res)
  res)

The in-value sequence "lifts" values to length-one sequences for just
such kinds of bindings.  (The above is untested, so may need some
fixing.)

Carl Eastlund

On Tue, Sep 6, 2011 at 2:35 PM, J. Ian Johnson <ianj at ccs.neu.edu> wrote:
> When writing a natural extension to racket/base for hashes, hash-filter-map, I found that what would be the most natural way to write it would look like so:
>
> (define (hash-filter-map f ht)
>  (for/list ([(k v) (in-hash ht)]
>             #:when/bind [res (f k v)])
>     res))
>
> Instead I had to write
>
> (define (hash-filter-map f ht)
>  (for/fold ([result '()])
>            ([(k v) (in-hash ht)]
>    (cond [(f k v) => (lambda (temp) (cons temp result))]
>          [else result])))
>
> Which is less pretty.
> I'd add this myself, but the for macros are scary. Anyone familiar with this section of code willing to add this?
>
> -Ian



Posted on the dev mailing list.