Functional Programming & High-order functions in Python map reduce filter and sorted

Source: Internet
Author: User

1. Functional Programming

1) concept

Functional programming is a programming model that considers computer operations to be calculations of functions in mathematics and avoids the concept of state and Variable. Wiki

We know that object is the first type of object-oriented, then functional programming is the same, the function is the first type of functional programming. In object-oriented programming, We transmit the object, that in functional programming, we have to do is to pass the function, and this, as a term, we call him the Higher-order Function. Fei Lin Sha

2) Features

    • Calculations are treated as functions rather than directives
    • Pure functional Programming: No variables required, No side effects, simple testing (the result is the same each Time)
    • Supports Higher-order functions with simple code

2. Python supports functional programming

    • Not purely functional programming: allowed variables
    • supports higher order functions: functions can also be passed in as arguments (variables)
    • Support Closures: the ability to return functions with closures
    • Limited support for anonymous functions

3. Higher-order functions in Python

1) Custom High-order functions (functions as Parameters)

Import Math def add (x, y, f):     return F (x) + f (y)print add (9, math.sqrt)

2) Built-in High-order functions

    • Map function

format: map (f, lst)

F is the function name, can be a built-in function, or it can be a custom function

The LST is a sequence to be processed, and the type can be list,tuple and string, and the sequence can be

function: Each element in LST performs the operation of the F function

For example: assume that the user entered the English name is not standardized, not according to the first letter, followed by the letter lowercase rules, Please use the map () function, A list (including some nonstandard English name) into a list containing the English name of the specification: [' Adam ', ' LISA ', ' Bart '] output: [' Adam ', ' Lisa ', ' Bart ']

def format_name (s):     return s[0].upper () +s[1:].lower ()print map (format_name, ['Adam'  'LISA'barT')

Functional Programming & High-order functions in Python map reduce filter and sorted

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.