Identifying pure functions

A pure function satisfies two properties:

  • Its output only depends on its inputs: when you input a value, the output is always the same.
  • It has no side-effect, that is to say, no effect outside the function.

A lot of functions in R are not pure, yet they are vital for a day to day use of R: when doing an analysis, you need to download files, create a plot, save results…

When programming, you should aim at making your functions either as pure as possible or as impure as possible (for example, a function that downloads a file should only download this file). But for that, you first need to be able to recognize a pure function from an impure one.

This is what we'll do in this exercise: run functions which are either pure or impure, and see what their outputs are.

This exercise is part of the course

Intermediate Functional Programming with purrr

View Course

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Launch Sys.time(), Sys.sleep(1), & Sys.time()
___()
___(___)
___()