Hackernoon logoAn Introduction to Functional Programming Basics with Python by@priyar

An Introduction to Functional Programming Basics with Python

Author profile picture

@priyarPriya Rathore

Priya rathore is best Writer

Functional programming is a style of coding whose primary purpose is to do rather than to perform a task. Functional programming comes from a style of mathematical thinking when you define a type of input that goes into a function and the output we can expect from a function.

In a functional code, the output of a function depends only on the arguments that are passed. Calling a function to the same value of x should give the same result f (x), no matter how many times you leave it. So it requires a completely different style of thinking when you rarely change your state. Instead of going through the steps, you feel that the data is transformed and the desired result is the final state.

Python has several constructions that allow programmers to understand functional programming. To learn more about functional programming, see our notes on this topic.

Functions as first-class objects in Python:

Using functions as first-class objects means that they need to be used similarly to data. So, you can pass them as parameters, such as passing one function to another function as an argument. For example, in this example, you can pass the int function as a map function parameter.

>>> list (map (int, ["1", "2", "3"]))
[1, 2, 3]

You can assign them to variables and return them. For example, in this code, you can assign the function hello_world, and then the variable will run as a function.

Boost your social accounts through SociallyFan with our automatic system, your orders are delivered in a few minutes.

Python Functional Accuracy:

Python has a variety of built-in features that can help you avoid procedural code features. So something like that

def naive_sum (list):
    s = 0
    For list l:
        s + = l
    Come back

Can be replaced by the following constructions:

Amount (list)

Similarly, built-in features such as Python, Map, Reduce, and Itthon modules can be used to avoid side effects in your code.

Reducing use of Python loops:

Loops appear in an image when you want to move to a set of objects and implement a particular argument or function.

x x l:
    Funk (x)

The above construction stems from the traditional thinking of phasing out the entire program in which you define how things should be done. To make it more functional, it will be necessary to change the way of thinking. You can convert the following loop to Python.

Map (Funk, L)

It is read as 'linking a task to a list', which corresponds to our idea of โ€‹โ€‹defining the question 'who'.

If you take advantage of this idea and apply it to continuous tasks, you will get the following constructions.

Diff funk 1 ():

Note that this does not really work for features, but provides a lazy map object. To execute the code, you must pass this object to a list or other desired function.

What is repetition?

Recombination is a way of breaking up a problem into a subtype that is essentially the same as the original problem. You solve the main problems and then combine the results. This usually involves calling the function.

How to set up iteration in code

Python has recursion support, allowing you to optimize dynamic programming constructions in your code. In principle, two conditions must be satisfied. There should be a situation where repetition should end, and it should apply to all other situations. The final position should be restrictive; That is, features should call for smaller versions.

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.