Project

General

Profile

Feature #16781

alias :fold :reduce

Added by 0x81000000 (/ /) 4 months ago. Updated 3 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:97861]

Description

  1. Method "collect" has short synonym "map", short synonym of "inject" and "reduce" doesn't exist.
  2. Name "fold" is more common/popular than "reduce" or "inject".

In my own code I use:

module Enumerable
    alias :fold :reduce
end

I have no idea how to properly add "fold" in Ruby's stdlib/corelib.

#1

Updated by 0x81000000 (/ /) 4 months ago

  • Description updated (diff)

Updated by shan (Shannon Skipper) 4 months ago

0x81000000 (/ /) wrote:

  1. Name "fold" is more common/popular than "reduce" or "inject".

I was surprised to hear this, since I think of "reduce" as most popular. A quick survey of popular languages seems to confirm that "reduce" is most popular, followed by "accumulate."

Reduce:

JavaScript
Python
Java
PHP
Ruby
TypeScript
Swift

Accumulate:

C#
C++

Fold:

Not found

You do also see "fold" in functional languages like Clojure, Scala, Elixir, et cetera, where it has a different meaning than "reduce," which is also present. In any case, "inject" has fallen out of favor.

I have no idea how to properly add "fold" in Ruby's stdlib/corelib.

You could an rb_define_method(rb_mEnumerable, "fold", enum_inject, -1); here: https://github.com/ruby/ruby/blob/v2_7_1/enum.c#L4172-L4173

Updated by 0x81000000 (/ /) 3 months ago

shan (Shannon Skipper) wrote in #note-2:

I was surprised to hear this, since I think of "reduce" as most popular.

https://en.wikipedia.org/wiki/Fold_(higher-order_function)
Article 'Reduce_(higher-order_function)' - just redirect to link above.

https://www.quora.com/Is-there-a-difference-between-fold-and-reduce-in-functional-languages

C++ STL - bad reference (an example to follow), std::accumulate 4 times slower than for-loop:
https://habr.com/ru/company/dbtc/blog/498374/#comment_21542236

Also available in: Atom PDF