LoslegenKostenlos starten

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.

Diese Übung ist Teil des Kurses

<Kurs>Intermediate Functional Programming with purrr</Kurs>
Kurs ansehen

Interaktive praktische Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# Launch Sys.time(), Sys.sleep(1), & Sys.time()
___()
___(___)
___()
Code bearbeiten und ausführen