问题
While working with lodash, I found that it applied shortcut fusion when I used implicit chaining.
$ node -e 'const _ = require("lodash"); _([1,2,3]).map(n => { console.log(n); return n }).find(n => n <= 1)'
1
But when I changed this snippet to use explicit chaining, it doesn't apply shortcut fusion.
$ node -e 'const _ = require("lodash"); _.chain([1,2,3]).map(n => { console.log(n); return n }).find(n => n <= 1).value()'
1
2
3
As you can see, a function passed to map was called three times instead of once, which indicates lodash didn't apply shortcut fusion.
From their document, shortcut fusion should seem to be applied in both cases.
- Is this an intended behavior?
- Where in the document do they explain this behavior if so?
- Is there any ways to apply shortcut fusion with explicit chaining?
I use lodash 4.17.5 on node.js v8.10.0.
回答1:
I found a ticket about this issue at their GitHub project.
It's unexpected but is what it is for now.
来源:https://stackoverflow.com/questions/49894215/explicit-chaining-with-lodash-doesnt-apply-shortcut-fusion