4 ms·
That's a lovely little demonstration. The method name `inject` needs to be formally deprecated in Ruby. It's fundamentally a simple method, but the name _reall
by noneeeed 4y ago
That's a lovely little demonstration.
The method name `inject` needs to be formally deprecated in Ruby. It's fundamentally a simple method, but the name _really_ throws people off. Reduce isn't perfect, but it's a bit closer to what's happening.
- galaxyLogic 4y agoIn Smalltalk it is called inject:into: which I think is quite descriptive. You "inject" (as in push, insert) elements of the recipient-collection one by one INTO a 2-argument function, which function gets replaced by a new version of itself after each element has been "injected" into it. In contrast I am always confused by names like "foldLeft". Does it mean "fold elements starting FROM left", or "fold elements TO left" ? Here the Object-Oriented syntax of Smalltalk and Ruby has an advantage: The name of the method is a command (-verb) to the recipient, passing along arguments. The list to iterate over is not one of the arguments, the list is the recipient of the method-call. Therefore the method-name does not need to say anything about the recipient, and can thus focus on what should be done with the arguments, and thus can can be more clear about that.
- noneeeed 4y agoI certainly agree with you that `fold` is a terrible name for it. That makes me think of some kind of halving action. "inject:into" is definitely a bit better. It's amazing how quite a small addition to a name can make a big difference. I'm always keen on my team using longer method/variable names when it can help with meaning, sometimes just a small expansion can really help readability.
- JadeNB 4y ago> I certainly agree with you that `fold` is a terrible name for it. That makes me think of some kind of halving action. I think the poster expressed no opinion on `fold` itself, only on the obviousness or un- of which way `foldl` folds: that is, ignoring base conditions, afold f z (x:xs) = f x (afold f z xs) can be said to fold from the left, whereas bfold f z (x:xs) = bfold f (f z x) xs can be said to fold to the left. It happens that the latter is what we call `foldl`, but I think one can't reasonably argue that a clean-room re-discovery of folding—which would surely discover both implementations—would be guaranteed to impose the same convention. (As weak evidence for this, I note that I would be willing to bet only a tiny sum of fake internet points that your grandparent would agree on which of these they meant by "fold from the left", and which of these they meant by "fold to the left".)
- a1369209993 4y ago> Does it mean "fold elements starting FROM left", or "fold elements TO left" ? Neither; it means "left-associative fold", like left-associative operator: ((a + b) + c).
- MaxBarraclough 4y agoYou've not explained how that differs from folding to the left. It's true than foldl cannot be used with infinite lists, as it will recurse infinitely, unlike foldr. The point to emphasise is that neither foldl nor foldr will swap the ordering of the values in the list, which folding to the left might wrongly imply. That is, if using fold with a function that is associative, foldl and foldr will give the same result, regardless of whether that function is commutative. Wikipedia has a visualisation: https://en.wikipedia.org/wiki/Fold_(higher-order_function)#As_structural_transformations https://en.wikipedia.org/wiki/Fold_(higher-order_function)#A...