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

后端 未结 4 1900
温柔的废话
温柔的废话 2021-01-02 00:52

I began learning functional programming recently, and came up with this example when attempting to calculate my quiz average for a class.

The example I came up with

4条回答
  •  感情败类
    2021-01-02 01:17

    In place of sum? Never.

    But a reduce call would be the way to go when aggregating by a custom method.

    For instance product could be defined as:

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

    Also sum is implemented in C.

提交回复
热议问题