resyntax icon indicating copy to clipboard operation
resyntax copied to clipboard

(apply + (map ...)) -> for/sum

Open sorawee opened this issue 2 years ago • 1 comments

E.g.

(apply + (map length '((1) (2 3))))

is equivalent to

(for/sum ([x (in-list '((1) (2 3)))])
  (length x))

More generally:

(apply + (map f xs))

is equivalent to

(for/sum ([x (in-list xs)])
  (f x))

But the latter doesn't create an intermediate data structure, so it's better.

Probably can do the same with filter.

sorawee avatar Dec 25 '23 05:12 sorawee

This rule already exists, but it doesn't fire when the function given to map isn't a lambda because there isn't a good candidate for a variable name to pick automatically. The fusion seems valuable enough that it might be worth doing it anyway in that case and just using v as the variable for everything.

jackfirth avatar Jan 08 '24 12:01 jackfirth