理解reduce(), map(), filter(), anonymous in Python

  • Overview

    What is an anonymous function/method or lambda?

  • Anonymous function

    An anonymous method is a method without a name, i.e. not bound to an identifier like when we define a method using def method:.

    Anonymous functions are often arguments being passed to higher-order functions, or used for constructing the result of a higher-order function that needs to return a function.

  • Lambda

    From [2], lambda comes from Lambda Calculus and refers to anonymous functions in programming.

    Lambda Calculus is a formal system in mathematical logic for expressing computation based on function abstraction and application using variable binding and substitution.

    It allows you to write quick throw away functions without naming them.

    lambda arguments: expression
    # def method
    def add(x, y):
        return x + y
    # lambda method
    lambda x, y: x + y
    

    lambdas differ from Python methods because:

    1. they can have only one expression, can’t contain any statements
    2. their return type is a function object
  • Anonymous method vs. Lambda

    Though most people use the term “anonymous function” and “lambda function” interchangeably - they’re not the same.

    This mistake happens because in most programming languages lambdas are anonymous and all anonymous functions are lambdas. This is also the case in Python.

  • Why are lambda relevant to map(), filter(), reduce() ?

    All three of these methods expect a function object as the first argument.

    The function object could be a pre-defined method with a name (like def add(x,y)), though, more often than not, would be the ones you’d use only once.

  • map(function_to_apply, list_of_inputs)

    map() applies a function to all the items in an input_list.

  • filter(function, list_of_inputs)

    filter() forms a new list that contains only elements that satisfy a certain condition.

  • reduce(function, sequence[, initial])

    reduce() works differently than map() and filter() .

    Instead return a new list based on the function and iterables we’ve passed, it returns a single value.

    In Python 3, reduce() isn’t a built-in function anymore, it can be found in the functools module.

    reduce() works by calling the function we passed for the first two items in the sequence. The result returned by the function is used in another call to function alongside with the next (third in this case) element.

    The optional argument initial is used, when present, at the beginning of this “loop” with the first element in the first call to function.

  • References

  1. map(), filter(), and reduce() in Python with Examples
  2. StackOverflow: What is a lambda (function)?
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值