[racket-dev] for/... improvement. #:filter
I like the proposal. My earlier suggestion for supporting #:when and
#:unless in the body is the same as what you are asking for, I
believe.
On Wed, Sep 11, 2013 at 12:41 AM, Roman Klochkov <kalimehtar at mail.ru> wrote:
> I propose to add yet one keyword to for/foldX/derived/break (and so to all
> for/... syntaxes).
>
> Reason:
>
> If one need to filter the results of body before collecting them in
> list/vector/sum/..., there are two options:
>
> 1. (filter filter-func (for/list ...)) : doubles memory consumption and
> slower
> 2. (for/list (exprs ...) body ...) => (for/list (exprs ... #:when #t (res
> (in-value (begin body ...))) #:when (filter-func res)) res). Works, but very
> clumsy.
>
> I propose syntax
> (for/list (exprs ...) body ...) => (for/list (exprs ... #:filter
> filter-func) body ...) with the same behavior as in 2.
>
> Syntax:
>
> #:filter filter-func = (thunk* #t)
>
> Expects function with the same number of arguments as the result of for/...
> body should return values. Applied to results of body, and collected only
> if filter-func returns #t.
>
> Implementation:
>
> Minor changes in for.rkt in (define-syntax (for/foldX/derived stx) ...)
> and in documentation. If you busy, I may do it myself and make pull request.
>
> To think about:
>
> How many values should return filter-func in (for/lists ...). Only one or as
> many as values of body. I don't know what is more natural.
>
> --
> Roman Klochkov
>
> _________________________
> Racket Developers list:
> http://lists.racket-lang.org/dev
>