1

Currently, I am creating primitive equivalents of function like map, filter, reduce etc.

By primitive equivalents, I mean creating the equivalent of the above stated function, in shortest way possible, via language tools like loops, conditions etc.

To give insight of what i am trying to refer, I have already created primitive equivalents for map and filter.

SYNTAX FOR MAP STATEMENT:-

zip(function(), sequence)

EQUIVALENT FOR MAP:-

[function() for x in sequence]

------------------------------------------------------------------------------------------------------------------------------------

SYNTAX FOR FILTER STATEMENT:-

filter(function(), sequence)

EQUIVALENT FOR FILTER:-

[x for x in sequence if (condition)]

The difference between map and filter is that, map takes a generic function, on the other hand filter takes in a function that contains a condition, therefore, the output sequence size may be different then the input size in filter

----------------------------------------------------------------------------------------------------------------------------------*

Now, I am not able to do the same for reduce. For those who are not familiar with reduce()-

reduce(function(), sequence) function is used to apply a particular function passed in its argument to all of the sequence elements mentioned in the sequence passed along. reduce() has only a single return value, as opposed to a sequence returned by map and filter.

Example demonstrating working of reduce():-

reduce(lambda x, y: x*y, [10, 20, 30])

Output of above statement:-

6000

P.S.:- I don't want lengthy functions/for loops, I want something equivalent to a one liner, like what i did when creating an equivalent for map and filter.

Vasu Deo.S
  • 1,820
  • 1
  • 11
  • 23
  • 1
    [This question](https://stackoverflow.com/questions/44070877/one-liner-reduce-in-python3) is asking much the same thing, and the answers there indicate that it can't really be done—you can certainly write the reduce function in one line, but the canonical way to reduce a list is to import `functools.reduce`. – jirassimok Jul 01 '19 at 23:58
  • The very nature of reduce is why Guido removed it from Python 3. – GeeTransit Jul 02 '19 at 00:04
  • 1
    https://repl.it/repls/CluelessUnkemptEvaluation – Tezirg Jul 02 '19 at 00:36

0 Answers0