+ 1

What makes a function pure?

I'm enrolled in a course that ask me to make a function pure, it says in the code bellow the two characteristics of a pure function, but it is asking to turn the function into a pure one, and the answer involves just removing the functions "alert" and "console.log". Could someone tell me in what/how those two functions conflicts with the "two elements of a pure function"? Thanks in advance. https://code.sololearn.com/WoWi0fGNT4ok/?ref=app

30th Jun 2018, 2:29 AM
Juvenal Martins dos Santos Netto
Juvenal Martins dos Santos Netto - avatar
9 Réponses
+ 9
A pure function is one that, given the same input, always returns the same output value and has no other observable side effect. The functions alert() and console.log() are considered not pure, since they modify a state, and that is a side effect. In this case you can imagine that the state is your screen. This is best explained in the following article. Although it's about Haskell in the introduction, they mention why text output is considered like this: http://learnyouahaskell.com/input-and-output#hello-world
30th Jun 2018, 2:48 AM
Mickel
Mickel - avatar
+ 10
Excellent topic! I agree with what the others have said already. Here is a good article specific to Javascript. https://medium.com/@jamesjefferyuk/javascript-what-are-pure-functions-4d4d5392d49c
30th Jun 2018, 3:54 AM
David Carroll
David Carroll - avatar
+ 6
No, there is no context where both functions can be considered pure as far as I know. In that case they would be non-pure functions. Functions that are not pure have nothing wrong, but when you work with pure functions it is recommended to separate them from non-pure functions. That is, if you need to use a function that is considered not pure, it is advisable to use them outside of your pure functions. This has benefits, since your implementation can be used later for other tasks since it does not modify any state.
30th Jun 2018, 3:03 AM
Mickel
Mickel - avatar
+ 6
Yep
30th Jun 2018, 3:21 AM
Mickel
Mickel - avatar
+ 4
A pure function is a function always produce a same output with the given same input value. It's mapping the input to produce a new output. alert and console.log is the debugging functions, it's nothing relate or define a pure function here.
30th Jun 2018, 2:42 AM
Calviղ
Calviղ - avatar
+ 2
Mickel What you are trying to say is, to have functions doing only "pure" tasks, and if it's necessary, make functions for "no-pure" tasks, for exemple a function "showElements (arrayNum) => arrayNum.forEach(num => console.log(num))", correct?
30th Jun 2018, 3:19 AM
Juvenal Martins dos Santos Netto
Juvenal Martins dos Santos Netto - avatar
+ 1
Jay Matthews You must be right about the fact that if a function works with global variables, it can't be pure because the output could possibly rely on a external variable value, leading to different outputs even with the same input sometimes. But if I'm not mistaken, there is more to it (as explained throughout this discussion).
30th Jun 2018, 6:39 AM
Juvenal Martins dos Santos Netto
Juvenal Martins dos Santos Netto - avatar
0
Mickel Is there a context in which these two functions (alert and console.log) are considered pure inside a function? What if a function is intended to show something in the screen?
30th Jun 2018, 2:57 AM
Juvenal Martins dos Santos Netto
Juvenal Martins dos Santos Netto - avatar
0
When a function calls other functions, constants, variables, objects, etc defined outside the function, its impure. A pure function is one that doesnt do that. For example lets say I have a function with no parameters and the function’s function is that it parses the string at document.cookies to show the user their cookie values as an array. This is impure because inside the function body, it references document.cookie which is a global variable. However, if I were to make the function with a parameter called cookies, and reference cookies in place of document.cookie in the function body, then call my function with document.cookie as the cookie parameter, this would become a pure function.
9th Jul 2018, 4:03 PM
privrax.
privrax. - avatar