-
Notifications
You must be signed in to change notification settings - Fork 275
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
Add mapper support for compose #556
Comments
@ColinFay, I'm not sure whether this well documented, but map(plop, list("b", "c", 1)) %>% str
#> List of 3
#> $ a : num 10
#> $ aa : num 10
#> $ aaa: num 10 As for your other sample_round <- purrr::compose(
as_mapper(~ round(.x, 2)),
mean,
as_mapper(~ sample(.x, 10))
) you can write library(gestalt)
sample_round <- sample(10) %>>>% mean %>>>% round(2)
set.seed(1)
purrr::map_dbl(mtcars, sample_round)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> 19.68 6.40 169.12 153.60 3.48 3.28 17.69 0.50 0.20 3.90 2.20 Like |
@egnha ah, my example was indeed poorly chosen, I had a case lately where I wanted to do something which would require a mix of index extraction and function modification: So maybe something more like: # Modified version of compose
plucker <- compose(~ .x * 10, 1, "c", "b")
map(plop, plucker)
$a
[1] 100
$aa
[1] 100
$aaa
[1] 100 Which doesn't work with: map(plop, list("b", "c", 1, ~ .x * 10))
Error: Index 4 must have length 1 I'll update my issue. Thanks for pointing |
The "mapper composition" you want is a composition of higher-order functions. So instead of modifying library(purrr)
library(gestalt)
mcompose <- list %>>>% map(as_mapper) %>>>% gestalt::compose Note that such an expression works with I believe this does what you want: map(plop, mcompose("b", "c", 1, ~.x * 10)) %>% str
#> List of 3
#> $ a : num 100
#> $ aa : num 100
#> $ aaa: num 100 (Actually, in this particular example, it would be better to do map_dbl(plop, list("b", "c", 1)) * 10 optionally followed by |
@egnha yep, this totally works for these fringe use cases, thanks for the advice :) Still, I think adding mapper support to |
We'd have to deprecate string-matching first so it'd make more sense to use However if you're using the lambda formula already, isn't it simpler and more readable to just construct the function you need directly? |
I can indeed use a function to do that. library(purrr)
library(dplyr)
random_mean <- compose(round, mean, ~ sample(.x, 10))
rounded_sd <- compose(round, sd)
iris %>%
group_by(Species) %>%
summarize(rm = random_mean(Sepal.Length),
rs = rounded_sd(Sepal.Length)) Than : rounded_sd <- compose(round, sd)
random_mean <- function(x){
sample(x) %>%
mean() %>%
round()
}
# Or even
rounded_sd <- compose(round, sd)
random_mean <- as_mapper(~ round(mean(sample(.x, 10))))
iris %>%
group_by(Species) %>%
summarize(rm = random_mean(Sepal.Length),
rs = rounded_sd(Sepal.Length)) Also, it's a situation I've faced several times in training when I showed
|
I was talking about plucking support, i.e. using |
And |
Ah, ok. It indeed seems like a very rare use case (this kind of plucking where you need to combine lambda and numeric/characters — I had to do this twice and I indeed implemented my own function to do that). Not worth the breaking change if Should I make a PR with the modified version of |
That'd be great! It should still use |
I posted this issue at |
Issue
Today, if I want to insert mappers in a
compose
call, I'll have to wrap it into :As this is not supported:
This is because
compose
useslapply(list(...), match.fun)
, andmatch.fun
doesn't know how to deal with formulas.Proposed solution
I suggest a modification for :
This would also allow constructing complex "pluckers" :
Downside
Current version of
compose
allows to compose with characters elements. I'm not sure if this is widely used though (the documentation do not reference this use case, so I guess we can safely assume this is seldom used):The text was updated successfully, but these errors were encountered: