I am fine with the concept of pure function on fairly simple examples like ...
function addTwo(val){ return val + 2; }
Given the same arguments, it gives the same result, which leads to referential transparency and good deterministic code.
But then I came across such examples (taken from professor frisby of basically an adequate guide , but I found similar examples on other FP JS)
//pure var signUp = function(Db, Email, attrs) { return function() { var user = saveUser(Db, attrs); welcomeUser(Email, user); }; }; var saveUser = function(Db, attrs) { ... }; var welcomeUser = function(Email, user) { ... };
and I don’t understand why it is not considered an external dependency (so unclean) to call saveUser or welcomeUser .
I know that from the point of view of the / IO function, signUp always returns the “same” (equivalent) wire function, but it seems strange to me.
It’s hard for me to understand why even
function multiplyBy(times){ return value => value * times; } const fiveTimes = multiplyBy(5); fiveTimes(10);
considered pure . From the returned POV function, access to times is a search in the chain of visibility areas, it can be obtained from the immediate external region or because of it (for example, the global region).
Does anyone want to bring some light?
javascript functional-programming pure-function
sminutoli
source share