Inline / pipeline functions whenever possible #72
Replies: 4 comments
-
So you want the code to become something like this? arr.map(x => x * 2 * x) Honestly, if you want something like this, it would be much better to use generators instead: function* map(iterable, callback) {
for (const x of iterable) {
yield callback(x)
}
}
map(map(arr, x => x * 2), x => x * x) Perhaps we should make ECMAScript generators have a greater focus in BorrowScript? |
Beta Was this translation helpful? Give feedback.
-
While my example is simplistic there's more complicated ones where being able to automatically pipeline pure functions would be useful. |
Beta Was this translation helpful? Give feedback.
-
Generators solve this exact problem; both maps are running at pretty much the same time. The first value of the array goes through the first map, and then immediately through the second. I guess I'm not really following then. Are there any existing languages that do the optimizations you're describing so that I can read about them? |
Beta Was this translation helpful? Give feedback.
-
Alan lang does it, pretty sure Julia does too along with other functional languages |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
In Typescript we can currently do something like this:
From what I understand most javascript interpreters will run the first map to completion before starting the second as javascript is too dynamic to allow for proper analysis of pure functions.
Would it be possible to investigate limiting certain dinamicness of borrowScript to allow for merging each step of the above code in compilation so the array is not iterated twice?
Beta Was this translation helpful? Give feedback.
All reactions