[racket-dev] for/fold feature request

From: oev (oev-racket at sibmail.com)
Date: Thu May 30 07:15:45 EDT 2013

Hi, Ian!

> The for[/*] macros are fairly low in the language tower for Racket, so
making these kinds of changes robust in the original implementation is
cumbersome and error-prone.

I supposed that adding anything as optional would not affect existing stuff.

> I've written a shim to use on top of for/fold
> and for*/fold to alleviate some pain in multiple value accumulation,
post-processing and irrelevant intermediate accumulators. It uses
syntax/parse, which is higher in the language tower. Check it out at
>
> https://github.com/ianj/nifty-macros/tree/master/for-accumulate

It's good enough to publish it as a package.

> If you find any bugs, just shoot me an email and I'll make sure to fix
the problems.
> -Ian

IMO, `for/acc' or `for/accum' is better than full name.

Thanks for answer!





> ----- Original Message -----
> From: "oev" <oev-racket at sibmail.com>
> To: dev at racket-lang.org
> Sent: Wednesday, May 29, 2013 7:30:59 AM GMT -05:00 US/Canada Eastern
Subject: [racket-dev]  for/fold feature request
>
> Hi, all!
>
> Using `for/fold' with more than one accumulator is inconvenient, when
there is a need for auxiliary operations with accumulators before
return.
>
> For example:
>
> (define (partition pred lst)
>   (define-values (a1 a2)
>     (for/fold ([acc1 null]
>                [acc2 null])
>       ([v (in-list lst)])
>       (if (pred v)
>           (values (cons v acc1) acc2)
>           (values acc1 (cons v acc2)))))
>   (values (reverse a1)
>           (reverse a2)))
>
> In example, it's impossible to reverse accumulators without using
intermediate definitions and applying `values' again.
>
> IMHO, very often in `loop with accumulator' cases, there is a final
operation with accumulator before return.
>
> Is it possible to add ability for defining such final operation as
optional or keyword argument?
>
> For example:
>
> (define (partition pred lst)
>   (for/fold ([acc1 null reverse]
>              [acc2 null reverse])
>     ([v (in-list lst)])
>     (if (pred v)
>         (values (cons v acc1) acc2)
>         (values acc1 (cons v acc2))))))
>
> ... or even better:
>
> (define (partition pred lst)
>   (for/fold ([acc1 null #:do-finally reverse]
>              [acc2 null #:do-finally reverse])
>     ([v (in-list lst)])
>     (if (pred v)
>         (values (cons v acc1) acc2)
>         (values acc1 (cons v acc2))))))
>
>
>
>
> _________________________
>   Racket Developers list:
>   http://lists.racket-lang.org/dev
>





Posted on the dev mailing list.