Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compose returnes only output of first function 'composed' #639

Closed
benjaminschwetz opened this issue Feb 11, 2019 · 4 comments

Comments

@benjaminschwetz
Copy link

commented Feb 11, 2019

This might be identical #629, but I am afraid I don't exactly understand that issue, so posting mine to be sure.

Previously this worked

n_unique <- purrr::compose(length, unique)
       n_unique(iris$Species)
#> [1] 3

Now with 0.3.0 it returns:

n_unique <- purrr::compose(length, unique)
n_unique(iris$Species)
#> [1] setosa     versicolor virginica 
#> Levels: setosa versicolor virginica

Created on 2019-02-11 by the reprex package (v0.2.1)

I read the news about the changes made to compose but in my understanding, this code should still return the same output as before.

@egnha

This comment was marked as resolved.

Copy link
Contributor

commented Feb 11, 2019

Minor digression from the actual issue at hand: dplyr::n_distinct() is a faster alternative.

@romatik

This comment has been minimized.

Copy link

commented Feb 11, 2019

Looks like a bug since manual composition works as before (and expected)

n_unique <- purrr::compose(length, unique) 
n_unique(iris$Species)
#> [1] setosa     versicolor virginica 
#> Levels: setosa versicolor virginica

length(unique(iris$Species))
#> [1] 3

Created on 2019-02-11 by the reprex package (v0.2.1)

Session info
devtools::session_info()
#> ─ Session info ──────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.5.2 (2018-12-20)
#>  os       Ubuntu 18.04.1 LTS          
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language en_US                       
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       Europe/Berlin               
#>  date     2019-02-11                  
#> 
#> ─ Packages ──────────────────────────────────────────────────────────────
#>  package     * version date       lib source                         
#>  assertthat    0.2.0   2017-04-11 [1] CRAN (R 3.5.0)                 
#>  backports     1.1.3   2018-12-14 [1] CRAN (R 3.5.1)                 
#>  callr         3.1.1   2018-12-21 [1] CRAN (R 3.5.2)                 
#>  cli           1.0.1   2018-09-25 [1] CRAN (R 3.5.1)                 
#>  crayon        1.3.4   2017-09-16 [1] CRAN (R 3.5.1)                 
#>  desc          1.2.0   2018-05-01 [1] CRAN (R 3.5.0)                 
#>  devtools      2.0.1   2018-10-26 [1] CRAN (R 3.5.1)                 
#>  digest        0.6.18  2018-10-10 [1] CRAN (R 3.5.1)                 
#>  evaluate      0.12    2018-10-09 [1] CRAN (R 3.5.1)                 
#>  fs            1.2.6   2018-08-23 [1] CRAN (R 3.5.1)                 
#>  glue          1.3.0   2018-10-14 [1] Github (tidyverse/glue@4e74901)
#>  highr         0.7     2018-06-09 [1] CRAN (R 3.5.0)                 
#>  htmltools     0.3.6   2017-04-28 [1] CRAN (R 3.5.0)                 
#>  knitr         1.21    2018-12-10 [1] CRAN (R 3.5.1)                 
#>  magrittr      1.5     2014-11-22 [1] CRAN (R 3.5.0)                 
#>  memoise       1.1.0   2017-04-21 [1] CRAN (R 3.5.0)                 
#>  pkgbuild      1.0.2   2018-10-16 [1] CRAN (R 3.5.1)                 
#>  pkgload       1.0.2   2018-10-29 [1] CRAN (R 3.5.1)                 
#>  prettyunits   1.0.2   2015-07-13 [1] CRAN (R 3.5.0)                 
#>  processx      3.2.1   2018-12-05 [1] CRAN (R 3.5.1)                 
#>  ps            1.3.0   2018-12-21 [1] CRAN (R 3.5.2)                 
#>  purrr         0.3.0   2019-01-27 [1] CRAN (R 3.5.2)                 
#>  R6            2.3.0   2018-10-04 [1] CRAN (R 3.5.1)                 
#>  Rcpp          1.0.0   2018-11-07 [1] CRAN (R 3.5.1)                 
#>  remotes       2.0.2   2018-10-30 [1] CRAN (R 3.5.1)                 
#>  rlang         0.3.1   2019-01-08 [1] CRAN (R 3.5.2)                 
#>  rmarkdown     1.11    2018-12-08 [1] CRAN (R 3.5.1)                 
#>  rprojroot     1.3-2   2018-01-03 [1] CRAN (R 3.5.0)                 
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 3.5.1)                 
#>  stringi       1.2.4   2018-07-20 [1] CRAN (R 3.5.1)                 
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 3.5.2)                 
#>  testthat      2.0.1   2018-10-13 [1] CRAN (R 3.5.1)                 
#>  usethis       1.4.0   2018-08-14 [1] CRAN (R 3.5.1)                 
#>  withr         2.1.2   2018-03-15 [1] CRAN (R 3.5.0)                 
#>  xfun          0.4     2018-10-23 [1] CRAN (R 3.5.1)                 
#>  yaml          2.2.0   2018-07-25 [1] CRAN (R 3.5.0)                 
#> 
#> [1] /home/misha/R/x86_64-pc-linux-gnu-library/3.5
#> [2] /usr/local/lib/R/site-library
#> [3] /usr/lib/R/site-library
#> [4] /usr/lib/R/library
@yutannihilation

This comment has been minimized.

Copy link
Member

commented Feb 11, 2019

Yes, this is the same as #629... unique() is a generic funciton.

@ColinFay

This comment has been minimized.

Copy link
Contributor

commented Feb 11, 2019

This seems to be linked to the useMethod() of unique().

The first out is a UseMethod("unique") while the second is a .Primitive("length")(x), so the x is not processed in the first function :

library(purrr)

n_unique <- purrr::compose(length, unique)
n_unique(iris$Species)
#> [1] setosa     versicolor virginica 
#> Levels: setosa versicolor virginica
body(n_unique)
#> {
#>     out <- {
#>         UseMethod("unique")
#>     }
#>     fns <- list(function (x) 
#>     .Primitive("length")(x))
#>     for (fn in fns) {
#>         out <- fn(out)
#>     }
#>     out
#> }

unique_length <- compose(unique, length)
unique_length(iris$Species)
#> [1] 150
body(unique_length)
#> {
#>     out <- {
#>         .Primitive("length")(x)
#>     }
#>     fns <- list(function (x, incomparables = FALSE, ...) 
#>     UseMethod("unique"))
#>     for (fn in fns) {
#>         out <- fn(out)
#>     }
#>     out
#> }

foo <- purrr::compose(length, as.list)
foo(head(iris, 2))
#> $Sepal.Length
#> [1] 5.1 4.9
#> 
#> $Sepal.Width
#> [1] 3.5 3.0
#> 
#> $Petal.Length
#> [1] 1.4 1.4
#> 
#> $Petal.Width
#> [1] 0.2 0.2
#> 
#> $Species
#> [1] setosa setosa
#> Levels: setosa versicolor virginica
body(foo)
#> {
#>     out <- {
#>         UseMethod("as.list")
#>     }
#>     fns <- list(function (x) 
#>     .Primitive("length")(x))
#>     for (fn in fns) {
#>         out <- fn(out)
#>     }
#>     out
#> }

bar <- purrr::compose(nrow, head)
bar(iris)
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1          5.1         3.5          1.4         0.2  setosa
#> 2          4.9         3.0          1.4         0.2  setosa
#> 3          4.7         3.2          1.3         0.2  setosa
#> 4          4.6         3.1          1.5         0.2  setosa
#> 5          5.0         3.6          1.4         0.2  setosa
#> 6          5.4         3.9          1.7         0.4  setosa
body(bar)
#> {
#>     out <- {
#>         UseMethod("head")
#>     }
#>     fns <- list(function (x) 
#>     dim(x)[1L])
#>     for (fn in fns) {
#>         out <- fn(out)
#>     }
#>     out
#> }

Created on 2019-02-11 by the reprex package (v0.2.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
You can’t perform that action at this time.