`compose()`와 `negate()`로 함수 만들기

purrr로 배우는 중급 함수형 프로그래밍

Colin Fay

Data Scientist & R Hacker at ThinkR

자유롭게 compose() 사용하기

제한 없는 합성:

library(broom)
library(purrr)
clean_aov <- compose(tidy, anova, lm)
clean_aov(Sepal.Length ~ Sepal.Width, data = iris)
# A tibble: 2 x 6
  term           df  sumsq meansq statistic p.value
  <chr>       <int>  <dbl>  <dbl>     <dbl>   <dbl>
1 Sepal.Width     1   1.41  1.41       2.07   0.152
2 Residuals     148 101.    0.681     NA     NA    
purrr로 배우는 중급 함수형 프로그래밍

negate()

논리값 반전:

is_not_na <- negate(is.na)
x <- c(1,2,3,4, NA)
is.na(x)
FALSE FALSE FALSE FALSE  TRUE
is_not_na(x)
TRUE  TRUE  TRUE  TRUE FALSE
purrr로 배우는 중급 함수형 프로그래밍

매퍼와 함께 사용하기

negate()와 매퍼:

under_hundred <- as_mapper(~ mean(.x) < 100)
not_under_hundred <- negate(under_hundred)
map_lgl(98:102, under_hundred)
TRUE  TRUE FALSE FALSE FALSE
map_lgl(98:102, not_under_hundred)
FALSE FALSE  TRUE  TRUE  TRUE
purrr로 배우는 중급 함수형 프로그래밍

HTTP 코드 소개

purrr로 배우는 중급 함수형 프로그래밍

포함 여부 확인

x가 y에 %in% 포함되는가?

status <- 201

good_status <- c(200, 201, 202, 203)

status %in% good_status
TRUE
purrr로 배우는 중급 함수형 프로그래밍

연습해 봅시다!

purrr로 배우는 중급 함수형 프로그래밍

Preparing Video For Download...