ComenzarEmpieza gratis

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.

Este ejercicio forma parte del curso

Intermediate Functional Programming with purrr

Ver curso

Ejercicio interactivo práctico

Prueba este ejercicio y completa el código de muestra.

# Launch Sys.time(), Sys.sleep(1), & Sys.time()
___()
___(___)
___()
Editar y ejecutar código