Design of everyday functions

7ba164f40a50bc23dbb2aa825fb7bc16?s=47 Hadley Wickham
September 18, 2018

Design of everyday functions

7ba164f40a50bc23dbb2aa825fb7bc16?s=128

Hadley Wickham

September 18, 2018
Tweet

Transcript

  1. 3.

    Interactive Easily detect & resolve problems Packaged In production You

    hear your code Things break and people at you Practitioner Programmer
  2. 6.

    Code is a conversation Ambiguity can be tolerated early and

    often Practitioner Programmer Implicit Explicit
  3. 9.
  4. 10.

    “When you have trouble with things—whether it’s figuring out whether

    to push or pull a door or the arbitrary vagaries of the modern computer and electronics industries—it’s not your fault. Don't blame yourself: blame the designer...” — Donald A. Norman
  5. 18.

    “Rule of thumb: if you think something is clever and

    sophisticated, beware — it is probably self-indulgence.” — Donald A. Norman
  6. 22.

    today <- as.Date("2018-09-18") lunch <- as.POSIXct("2018-09-18 12:00", tz = "Europe/Belgrade")

    c(today, lunch) What happens when you combine a date and a date-time?
  7. 23.

    today <- as.Date("2018-09-18") lunch <- as.POSIXct("2018-09-18 12:00", tz = "Europe/Belgrade")

    c(today, lunch) #> [1] "2018-09-18" #> [2] "4210927-01-24" What happens when you combine a date and a date time? WAT!
  8. 24.

    today <- as.Date("2018-09-18") lunch <- as.POSIXct("2018-09-18 12:00", tz = "Europe/Belgrade")

    c(lunch, today) What happens when you combine a date and a date-time?
  9. 25.

    today <- as.Date("2018-09-18") lunch <- as.POSIXct("2018-09-18 12:00", tz = "Europe/Belgrade")

    c(lunch, today) #> [1] "2018-09-18 12:00:00 CDT" #> [2] "1969-12-31 22:56:32 CST" What happens if you touch a date-time the wrong way? WAT!
  10. 26.

    lunch <- as.POSIXct("2018-09-18 12:00", tz = "Europe/Belgrade") lunch #> [1]

    "2018-09-18 12:00:00 CEST" c(lunch) c(NULL, lunch) What happens if you look at a date-time the wrong way?
  11. 27.

    lunch <- as.POSIXct("2018-09-18 12:00", tz = "Europe/Belgrade") lunch #> [1]

    "2018-09-18 12:00:00 CEST" c(lunch) #> [1] "2018-09-18 05:00:00 CDT" c(NULL, lunch) #> [1] 1537264800 What happens if you look at a date-time the wrong way? WAT! WAT!!
  12. 29.

    c(<factor>, <factor>) -> <integer> c(<datetime<tz>>) -> <datetime> c(NULL, <datetime<tz>>) ->

    <double> Types can give us a high-level overview of a function I’ll put types in angle brackets to make clear that this is not real R code
  13. 30.

    To do that we need to review some foundations Atomic

    Numeric Logical Integer Double Character
  14. 31.

    c(<logical>, <logical>) -> <logical> c(<logical>, <integer>) -> <integer> c(<logical>, <double>)

    -> <double> c(<logical>, <character>) -> <character> c(<integer>, <logical>) -> <logical> c(<integer>, <integer>) -> <integer> c(<integer>, <double>) -> <double> c(<integer>, <character>) -> <character> c(<double>, <logical>) -> <double> c(<double>, <integer>) -> <integer> c(<double>, <double>) -> <double> c(<double>, <character>) -> <character> For atomic vectors, the rules are simple
  15. 32.

    For atomic vectors, the rules are simple Logical Integer Double

    Character Logical logical integer double character Integer integer integer double character Double double double double character Character character character character character
  16. 33.

    For atomic vectors, the rules are simple Logical Integer Double

    Character Even if you’re never explicitly learned this, I think you internalise it quickly.
  17. 34.

    Unfortunately c() breaks down when we get to S3 vectors

    Atomic Numeric Logical Integer Double Character factor POSIXct Date S3 vectors
  18. 35.

    Figuring out the rules is the goal of the vctrs

    package http://vctrs.r-lib.org
  19. 36.

    mutate(<data.frame>) -> <data.frame> filter(<data.frame>) -> <data.frame> select(<data.frame>) -> <data.frame> arrange(<data.frame>)

    -> <data.frame> summarise(<data.frame>) -> <data.frame> group_by(<data.frame>) -> <grouped_df> The types of the primary dplyr functions are simple
  20. 37.

    if_else(<logical>, <integer>, <integer>) -> <integer> if_else(<logical>, <double>, <double>) -> <double>

    if_else(<logical>, <integer>, <logical>) -> ??? if_else(<logical>, <integer>, <double>) -> ??? if_else(<logical>, <factor>, <character>) -> ??? if_else(<logical>, <date>, <datetime>) -> ??? But there are a few that are more complex
  21. 38.

    x <- runif(6) if_else(x > 0.5, x, NA) #> Error:

    `false` must be type double, #> not logical Which leads to this annoyance
  22. 39.

    x <- runif(6) if_else(x > 0.5, x, NA) #> Error:

    `false` must be type double, #> not logical if_else(x > 5, x, NA_real_) #> [1] NA 0.700 0.557 NA NA NA Which leads to this annoyance You’re currently forced to learn about the “typed” NAs
  23. 40.

    if_else(<logical>, <integer>, <logical>) -> vec_c(<integer>, <logical>) -> <integer> if_else(<logical>, <factor>,

    <character>) -> vec_c(<factor>, <character>) -> <error> I think I'm starting to see the principles
  24. 41.

    Fin

  25. 42.

    Practitioner Programmer Implicit Explicit Interactive Easily detect & resolve problems

    Packaged In production Code is a conversation Ambiguity can be tolerated early and often