On 03/30/2016 09:02 PM, Michael Haufe wrote:
> Does the JavaScript Engine utilize anything similar to the fusion laws for methods as described in a number of academic papers in the functional programming community[1][2]?
>
> For example:
>
> xs.map(f).map(g) <==> xs.map(g ∘ f)
I would love to have our Jit implement deforestation optimizations, but the
problem is worse than what you might expect.
This would hold only if you can prove that you have no side effects, both in
the functions, and also in the data of xs.
For example:
xs.map((x, i) => { if (i % 2 == 0) return x + 1; return x; })
.map((x, i) => { if (i % 2 == 1) return x + 1; return x; })
This sounds like this would be side-effect free, but if two of the value
contained in the array are objects, with a coercion function that I would
not name, then you can no longer guarantee that the order of the side-effect
is maintained.
Then if we were to optimize this in the Jit, we might have a bigger problem
being that IonMonkey has to resume to Baseline when we face unexpected
cases. These unexpected cases could be things from a mismatch type, or a
shrinking GC. These implies that we would have to reconstruct the temporary
array and discard half of the result we computed so far.
Typed and Pure languages have good guarantees. Unfortunately, JavaScript
has none of that.
--
Nicolas B. Pierron