สร้างฟังก์ชันด้วย `compose()` และ `negate()`

Functional Programming ระดับกลางด้วย 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    
Functional Programming ระดับกลางด้วย 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
Functional Programming ระดับกลางด้วย purrr

ใช้กับ mapper

negates() และ mapper:

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
Functional Programming ระดับกลางด้วย purrr

ข้อมูลเพิ่มเติมเกี่ยวกับรหัส HTTP

Functional Programming ระดับกลางด้วย purrr

ทดสอบว่าค่าอยู่ในกลุ่มหรือไม่

x อยู่ใน %in% y หรือไม่?

status <- 201

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

status %in% good_status
TRUE
Functional Programming ระดับกลางด้วย purrr

มาฝึกกันเถอะ!

Functional Programming ระดับกลางด้วย purrr

Preparing Video For Download...