When will you use reduce () instead of sum ()?

I recently started learning functional programming and came up with this example, trying to calculate the average quiz for a class.

An example I came up with is:

scores = [90, 91, 92, 94, 95, 96, 97, 99, 100] def add(num1, num2): '''returns the sum of the parameters''' return num1 + num2 import operator timeit reduce(add, scores) / len(scores) #--> 1000000 loops, best of 3: 799 ns per loop timeit sum(scores) / len(scores) #--> 1000000 loops, best of 3: 207 ns per loop timeit reduce(operator.add, scores) / len(scores) #--> 1000000 loops, best of 3: 485 ns per loop 

It would seem that in the above example, using a higher-order function is almost 4 times slower.

So my questions are, when would there be a good time to use a higher order function, because it is clear that the example above is not it?

+7
python profiling functional-programming
source share
4 answers

reduce() makes sense when you need an arbitrary operation on a list of data, and not when you already have a highly optimized library function that not only surpasses reduce() in small lists, but significantly surpasses it in larger ones.

reduce() gives you the flexibility to create arbitrary folds, but this flexibility comes at the cost of some performance overhead, especially in a language where most basic functional constructs are considered slightly outside the main thread.

Python is โ€œfunctionalโ€ because it has first-class functions, but it is not primarily a functional language. It provides a dust supply for iterators for use in a loop and has all kinds of language functions that allow you to write simple loops, but are not oriented to recursively defined operations with lists (although this allows them to a limited extent - the lack of TCO prevents me from, say, rephrasing my Erlang code or Guile directly in Python, but it gives me the ability to do things like comparative competing approaches that adhere to similar interfaces ).

+4
source share

Having fixed performance issues, I have to say the following: there is nothing wrong with using sum() , and stylistically you should choose sum() over reduce() ! reduce() is more general and therefore can be used to write other abbreviations than just summing. sum() is an abbreviation that is common enough that it deserves a personal name and definition.

If you look at functional programming languages, you will find, for example, that they have large libraries of common utility functions for working with sequences, such as Haskell Data.List or Scheme SRFI-1 . Many functions in these libraries can be written from the perspective of others; for example, the map function in Haskell can be written in terms of foldr (which is similar to reduce() ):

 map :: (a -> b) -> [a] -> [b] map f = foldr go [] where fa bs = fa : bs 

But no one claims that foldr thereby makes it unnecessary or something to avoid. Rather, more general operations, such as foldr or reduce() , are considered building blocks to create more specialized functions that make writing and understanding programs easier.

reduce() and sum() are in the same relationship. reduce() is the building block that you are accessing when you no longer have a function of type sum() .

+2
source share

Instead of the amount? Never.

But invoking shrinking will be a way to transition when aggregated using a custom method.

For example, product can be defined as:

 product = lambda iterable: reduce(operator.mul, iterable) 

Also, sum is implemented in C.

+1
source share

reduce and sum do different things. Consider a question like "I have a nested dictionary ...

 d = {'foo': {'bar': {'baz': 'qux'}}} 

and I would like to get the value associated with the list of keys: ['foo', 'bar', 'baz'] ". This can cause reduce (if you are functional programming):

 >>> reduce(lambda subdict, k: subdict[k], ['foo', 'bar', 'baz'], d) 'qux' 

Note you cannot do this with sum . It just happens that summation is a simple example showing what happens with a decrease (since you can write it with parentheses, and most programmers are familiar with how mathematical operations are in parentheses).

0
source share

All Articles