Compute if a function is pure

Posted by Oni on Programmers See other posts from Programmers or by Oni
Published on 2012-11-21T22:55:46Z Indexed on 2012/11/21 23:11 UTC
Read the original article Hit count: 339

As per Wikipedia:

In computer programming, a function may be described as pure if both these statements about the function hold: The function always evaluates the same result value given the same argument value(s). The function result value cannot depend on any hidden information or state that may change as program execution proceeds or between different executions of the program, nor can it depend on any external input from I/O devices. Evaluation of the result does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to I/O devices.

I am wondering if it is possible to write a function that compute if a function is pure or not. Example code in Javascript:

function sum(a,b) {
    return a+b;
}

function say(x){
    console.log(x);
}

isPure(sum) // True
isPure(say) // False

© Programmers or respective owner

Related posts about JavaScript

Related posts about functional-programming