Pure Functions And Side Effects

preview_player
Показать описание


In computer programming pure function is a function that satisfies two conditions:

It is deterministic, which means that for any given input it will always return same output.
It has no side effects. Which means it doesn’t change outside world. Doesn’t perform input/output operation or change global variables.
Рекомендации по теме
Комментарии
Автор

What a great, simple concise video. Thanks man!

alecjordan
Автор

thank you for the clear and simple explanation

Amal-qokx
Автор

i bit confused, sorry for lack of english,
but when you x = x+1 actually you make change the input value so that is not pure function as,
it should be like this let y =x+1..
i hope it is considerable

muhammadovais
Автор

A pure function can call another pure function without explicitly receiving it from a parameter. None of the two required rules is violated by calling a pure function from within another pure function.

In fact, the properties of pure functions make them a safe target for inlining. Create as many pure functions as you want and call them inside another pure function as many number of times as you want, without explicitly passing them through an argument. Pure function calling other pure functions inside it will still be pure.

theteachr
Автор

Hi, Please increase your sound. It is not audible. Thanks for your effort.

azharmohammed
Автор

In the start of the video, you describe deterministic functions as "for any given input, it will always return same output"
This might be my english skills lacking, but does this mean tha A func is deterministic if:
- It always return the same output whatever the input is
- if the input is x, it will always return x. and if the input i y, the output will always be y

bubblesgrappling
Автор

const identity = (x) => x;

const increment = (x) => {
x = identity(x);
x = x + 1;
return x;
};

why is the increment function not a pure function? It conforms to both the rules. It will return the same output for the same inputs, and secondly it does not change anything outside it's scope. Then why isn't it pure? Technically "*_+_*" is also a function being used inside another function, then wouldn't that also make functions impure?

prince
Автор

Why does if condition makes it no longer deterministic?

wryltxw