CommencerCommencez gratuitement

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.

Cet exercice fait partie du cours

<cours>Intermediate Functional Programming with purrr</cours>
Voir le cours

Exercice interactif pratique

Essayez cet exercice en complétant ce code d’exemple.

# Launch Sys.time(), Sys.sleep(1), & Sys.time()
___()
___(___)
___()
Modifier et exécuter le code