The mathematical equivalent of a Ruby injection

Is there a Mathematica function like injectin Ruby? For example, if I want a product of items in a list, in Ruby I can write:

list.inject(1) { |prod,el| prod * el }

I found what I can use Productin Mathematica:

Apply[Product, list]

However, for me this is not enough for me (for example, if I do not just want a product or a sum of numbers). What is the closest equivalent inject?

+3
source share
1 answer

Equivalent to Fold . I think this is more typically called "reduce" - this is the name of Python anyway.

Translation of your example:

Fold[#1*#2&, 1, list]

, #1*#2& -, . Times :

Fold[Times, 1, list]

, , Times :

Apply[Times, list]

, :

Times @@ list

. , Times, . Product - , Sum.

+7

Source: https://habr.com/ru/post/1748258/


All Articles