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 bymap
andfilter
.
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
.