Rewrite rules for ranges

bearophile via Digitalmars-d digitalmars-d at puremagic.com
Sat Dec 20 06:16:04 PST 2014


When you use UFCS chains there are many coding patterns that 
probably are hard to catch for the compiler, but are easy to 
optimize very quickly:

.sort().group.canFind => binary search

.sort().front => .reduce!min

.reverse.reverse => id

.reverse.back => .front

If the filtering and mapping functions are pure nothrow:
.map.filter => .filter.map

.map(a).map(b) => .map(compose(a, b))

and so on.

Such sub-optimal patterns can be written by programmers that are 
not caring a lot about performance, by mistake, or after inlining.

In Haskell this is so common that GHC has a feature named rewrite 
rules:

https://downloads.haskell.org/~ghc/6.12.2/docs/html/users_guide/rewrite-rules.html

https://www.haskell.org/haskellwiki/GHC/Using_rules

Such rules are simpler to do in Haskell because the code is much 
more pure compared to usual D code. But perhaps the same is 
possible and useful in D too.

Currently some rewrite rules are present inside the Phobos ranges.

Bye,
bearophile


More information about the Digitalmars-d mailing list