Red Security

Full Version: Python 2.5: Pure Functions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
[Image: BFMMlbcQvml9HSqXcvNp]
Pure Functions:
           In the previous thread we started a new concept called functional programming and we discussed it deeply and now we are going to see some other concepts inside the functional programming. In the previous thread we talked about something i.e Pure functions. So  in this thread we are going to look at this concept. And how these are beneficial and why we should use this over other functions.
Any function can be pure functions but it needs to meet two requirements:



Quote:first one is for the same input output is always the same.


It is same as functions in mathematics for example we have a function  f(x) = x2+ x . So has you can see if you say x = 2 for that value the output is always 6 it will never change this is really the same for functions in programming. 



Quote:And the second one is that the function should not have any side effects.

In simple terms, the side effect means the function should not modify the state of 

the variable globally. Look at this example to get a better understanding of this concept of side effects.

Code:
list = []

def alphabets(bar):
    list.append(bar)

alphabets('a')
print(list)


In this example the function alphabet is changing the state of the list which is outside the function is changing the state of the list. Now let’s see how we can change this function to a pure function.

Code:
def alphabets(bar, lst):
    return lst + [bar]

list = []
list_now = alphabets('a', list)

print (list)

Quote:Exclamation   Note : Print function is used to see the effect of both functions


      In impure every time even if we give the same input the output is different and also has the side effects but in impure functions the output is the same every time for the same input. And it is not affecting the list which is a global variable.
The pure functions have greater advantages over impure ones.
These are easy to debug because as it is not have any side effects so if the problem occurs you will know that fault is inside the function so it will make program easy to debug in this sense
The second advantage of pure function is that, The Pure functions don't read or write to any shared memory, and therefore can run in separate threads without any unexpected consequence

So that’s it for now.




"Keep Good care of your Health
Allah Hafiz and Good Bye Till the next post"
"Keep Learning and Keep exploring"
In the first example, We had a list and we wanted to create another list that doesn't affect the main list which you succefully achived in the second example?

Am I right? or I miss something.

Also, please debug first example.
(05-07-2022, 10:05 AM)Mr.Kurd Wrote: [ -> ]In the first example, We had a list and we wanted to create another list that doesn't affect the main list which you succefully achived in the second example?

Am I right? or I miss something.

Also, please debug first example.
Yes you are right but keep in mind the second condition of pure function also that if you run the print statement for first time in first example it will add a to list and if you run it second time this will add another a now the list has two elements. As both the input are same but the output is different.
And degugged the example thanks for it