Should pure functions be prevented from reading changeable

bearophile bearophileHUGS at lycos.com
Sat Nov 6 18:01:38 PDT 2010


Jonathan M Davis:

> If they're not cacheable, what's the point of pure?

Pure functions allow you to be sure global variables are not modifying the results of the function, there are less ways to shoot yourself in the foot. Controlling or removing the unwanted flow of information between subsystems of your system is one of the best ways to reduce its whole complexity.

Also pure functions allow the compiler to perform some optimizations, like replace foo(x)+foo(x) with 2*foo(x), or to pull out a function call from a loop. Eventually a bit improved D compiler that allows this too:
http://d.puremagic.com/issues/show_bug.cgi?id=5125
may perform higher level transformations on the code, like replace:
filter!pure1(map!pure2(range))
With a faster:
map!pure2(filter!pure1(range))

Functional languages are able to be not too much slow because they are able to perform several of such complex transformations, that generally are not done by C-derived compilers because the compiler doesn't know enough semantics about the functions it is compiling. The more constraints that are already implicitly present in your code get somehow known by the compiler, the better the smart compiler can digest your code.

Bye,
bearophile


More information about the Digitalmars-d mailing list