tidyverse / purrr

A functional programming toolkit for R
https://purrr.tidyverse.org/
Other
1.28k stars 272 forks source link

Use `length()` dispatch internally, not `Rf_xlength()` #963

Closed DavisVaughan closed 2 years ago

DavisVaughan commented 2 years ago

We have an internal rlang function that does this (you wouldn't get access even if you import the whole rlang lib) https://github.com/r-lib/rlang/blob/b3327104bac9c9fec90fa081fdd9cb321c3f29bb/src/internal/attr.c#L172

Used like this with r_is_object() https://github.com/r-lib/rlang/blob/b3327104bac9c9fec90fa081fdd9cb321c3f29bb/src/internal/attr.c#L96-L101

library(clock)
x <- year_month_day(2019, 1:3)

purrr::map(x, identity)
#> $year
#> <year_month_day<month>[1]>
#> [1] "2019-01"
#> 
#> $month
#> <year_month_day<month>[1]>
#> [1] "2019-02"

lapply(x, identity)
#> [[1]]
#> <year_month_day<month>[1]>
#> [1] "2019-01"
#> 
#> [[2]]
#> <year_month_day<month>[1]>
#> [1] "2019-02"
#> 
#> [[3]]
#> <year_month_day<month>[1]>
#> [1] "2019-03"

Created on 2022-09-26 with reprex v2.0.2.9000

DavisVaughan commented 2 years ago

Reasonable test that doesn't rely on clock

x <- new_rcrd(list(x = 1:3, y = 2:4))
expect_identical(map(x, identity), vec_chop(x))

For completely stability, you could manually craft expect rather than using vec_chop()