Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a pure function?

I have following function:

  def timestamp(key: String)
  : String
  = Monoid.combine(key, Instant.now().getEpochSecond.toString)

and wanted to know, if it is pure or not? A pure function for me is, given the same input returns always the same output. But the function above, given always the same string will returns another string with another time, that it is in my opinion not pure.

like image 285
softshipper Avatar asked Mar 08 '23 05:03

softshipper


1 Answers

No, it's not pure by any definition I know of. A good discussion of pure functions is here: https://alvinalexander.com/scala/fp-book/definition-of-pure-function. In Alvin's definition of purity he says:

A pure function has no “back doors,” which means:

...

It cannot depend on any external I/O. It can’t rely on input from files, databases, web services, UIs, etc; it can’t produce output, such as writing to a file, database, or web service, writing to a screen, etc.

Reading the time of the current system uses I/O so it is not pure.

like image 88
Rich Dougherty Avatar answered Mar 10 '23 11:03

Rich Dougherty