From: jzakiya@... Date: 2018-01-25T18:41:18+00:00 Subject: [ruby-core:85123] [Ruby trunk Feature#14399] Add Enumerable#product Issue #14399 has been updated by jzakiya (Jabari Zakiya). That's interesting because I never intuitively understood the use of `inject`, especially for numerical purposes, and always now use `reduce`, which was (I believe) created by Haskell (the person and programming language), and has long been a concept in functional programming. To me it says that an array of numbers (items) will be reduced|collapsed into a single value based on the given operation. Also, after reading your comment and thinking about it, there is a way to reclaim the `product` method name and recast it for this new (better) use. A better name for the current `Array#product` is `Array#combine`, which has the same number of characters, so it can replace the word inplace in code without the need to possibly reformat it. ``` [1,2,3].product [5,6,7] which seems like a vector|matrix operation can be renamed as [1,2,3].combine [5,6,7] or even [1,2,3].combine_with [5,6,7} ``` I think this is much clearer indication of its intent and purpose. Here's a proposed path to reclaim and recast `product` as proposed. Ruby 2.6 Introduce `Array#combine(_with)` as an alias to the current `Array#product`. Replace its use in the Ruby core and stdlib code. Make announcement to deprecate use of `Array#product` to give people time to change. Introduce `Enumerable#multiply` to act as proposed `Enumerable#product`. Ruby 2.7 Eliminate use of `Array#product` Create `Enumerable#product` as alias of `Enumerable#multiply` Since everyone knows Ruby 3 is going to be a breaking some old code change, if people are weaned off old code use then this change really won't create problems. On one hand you are creating a new capability in `Enumerable#[product|multiply]` while keeping, but renaming, a current capability. Plus, I can't think that alot of code|people currently use `Array#product`, so I doubt it affects a lot of code|people. (Is this ever used in Rails, etc?) Python went through this going from 2 to 3, famously with the `print` method change. Basically what I'm illustrating, if there is a will there is a way to make this change, without a lot of disruption and pain. ---------------------------------------- Feature #14399: Add Enumerable#product https://bugs.ruby-lang.org/issues/14399#change-69837 * Author: jzakiya (Jabari Zakiya) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- For similar reasons for creating `Enumerable#sum` a companion method `Enumerable#product` is also very useful. Taking the product of numbers in arrays is a common operation in many numerical algorithms, especially in number theory and cryptography, and its optimization in Ruby will make it more conducive to math heavy algorithms and tasks. This ``` > [2,3,5,7].reduce(:*) => 210 ``` can be optimized to this ``` > [2,3,5,7].product => 210 ``` It should also allow an initial value ``` > [2,3,5,7].product(2) => 420 > [2,3,5,7].product(0.5) => 105 ``` Crystal already has this `method`. -- https://bugs.ruby-lang.org/ Unsubscribe: