We have seen some simple examples of pure functions, so lets see impure functions as a comparison.
The above function is using a global value rather than the argument passed makes this function deterministic
Side Effect: Printing to the console logλ︎
Although the following example is probably quite harmless, it is a simple example of a function effecting the state of something outside. These side effects should be avoided where possible to keep your code simpler to reason about.
Side Causes: Calling librariesλ︎
To demonstrate a side causes form of impure functions, lets create a task-comple function that marks a current task complete using the current timestamp.
Hint:: The function
(java.util.Date.)is actually a call to instantiate a java.util.Date object. The full-stop character at the end of the name makes it a function call and is the short form of
In this example we have called to the outside world to generate a value for us. The above example is fairly simple, however by calling the outside world rather than passing in a date it makes the functions purpose far less clear.
Re-write as a pure functionλ︎
Change the task-complete function definition and function call to take both the task-name and completed-date as arguments.
Required values should be generated outside a function where possible. In this case in the
(js/Date) function is first evaluated and replaced by its value, then that date value is passed to the function as an argument, keeping the function pure.
The pure version of the function in Clojure, using the java.util.Date function.