1

I am following this guide to begin learning functional programming w/ Javascript: https://medium.com/@cscalfani/so-you-want-to-be-a-functional-programmer-part-1-1f15e387e536#.iynj38h83

It defines a Pure function as:

  • Operate only on input parameters
  • Useful Pure functions take at least one parametre
  • Useful Pure functions must return something
  • Pure functions cannot change external variables/No side effects
  • Pure functions will always produce the same output given the same input

The following function violates the contract:

function notPure(data) {
  let ts = new Date();
  return md5(data + ts);
}

But the following would be Pure:

function pureFunction(data, ts) {
  return md5(data + ts);
  }

Assuming I understand correctly, what is the point? I'm young to this part of the field. I don't yet understand how this is useful. Does the inclusion of the time stamp inside the function make the function stateful and break purity?

What is the advantage to forcing these values to be created elsewhere and passed into a pure function as a parameter?

ArnonRednon
  • 75
  • 1
  • 6
  • hmm from where did you get that the first version isn't pure ? looks pure to me :) – niceman Oct 18 '16 at 17:41
  • @niceman its not pure at all, gives different results on same inputs – Jared Smith Oct 18 '16 at 17:43
  • @JaredSmith ahhh I see, `new Date()` get's the current date, my mistake – niceman Oct 18 '16 at 17:45
  • 3
    Functions that always return the same output given the same input are easier to reason about. In addition, this guarantee allows functional programming languages to use tactics like delayed execution and memoization to improve performance. – Robert Harvey Oct 18 '16 at 17:45

1 Answers1

3

Programs will always have state. Always. The idea of pure functional programming is that you push state (insofar as is possible) to the edges of your programs: for example scan in a line of user input, do a bunch of pure functions on it, and spit the output back to the console.

This has a lot of advantages:

  • Pure functions are easy to test
  • State-related bugs have fewer places to hide
  • Pure functions can be made to generate very performant machine code.
  • No cache invalidating. Since the functions are referentially transparent, you can memoize at will.
  • It opens the door to really cool stuff like hot code reloading. I cannot over-state how awesome that is.

So for your example function, how would you test the impure version? With the pure version its simple, you pass it a date and assert it returns the expected output.

Jared Smith
  • 19,721
  • 5
  • 45
  • 83
  • hmmm I think the OP wants an answer more specific to his example(why passing timestamp is better than creating it) – niceman Oct 18 '16 at 17:47
  • also I don't know if javascript's VMs(be it Google V8 or spidermonkey or ...) optimize pure functions the way Haskell does – niceman Oct 18 '16 at 17:49
  • I can see the benefit to organizing code & debugging but that can't be the only reason for pure functions, can it? – ArnonRednon Oct 18 '16 at 17:49
  • 1
    @user1349419 that's like saying that breathing can't be the only reason for oxygen right? Of course it isn't, but its reason enough. – Jared Smith Oct 18 '16 at 17:51
  • @JaredSmith Gotcha. So I should think of these as building blocks. Requirements for the foundation. Right now it's an empty hole with some supports. Later, the value of these will be revealed? The better able I am to leverage these rules the stronger a functional programmer I will be? – ArnonRednon Oct 18 '16 at 17:55
  • Yes, just remember that we're in this to solve people's problems, anything that helps us do that more effectively or efficiently is a good thing. – Jared Smith Oct 18 '16 at 17:57
  • @niceman I'm not a compiler guy, but I would imagine it would be easier for the JIT heuristics to decide its safe to (for example) in-line a short, simple, referentially transparent function. – Jared Smith Oct 18 '16 at 17:59
  • @JaredSmith I'm not suspecting whether it's possible for a javascript VM to optimize pure code or not, I'm suspecting whether current implementations actually do it as good as Haskell(a language that was made pure from the beginning) or not :) – niceman Oct 18 '16 at 19:13
  • Should then all hash function be implemeted as pure and in case they are not, would this lead to easly reverse engine or is the other way around as I landed here because try to understand why "salt" is recommended in authentication https://stackoverflow.com/questions/30315599/understanding-the-purpose-of-a-salt-in-authentication – Carmine Tambascia Nov 12 '19 at 13:00