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

Feature: Function to create functional sequences #254

Open
mikmart opened this issue Feb 12, 2022 · 1 comment
Open

Feature: Function to create functional sequences #254

mikmart opened this issue Feb 12, 2022 · 1 comment
Labels
feature a feature request or enhancement

Comments

@mikmart
Copy link

mikmart commented Feb 12, 2022

Inspired by this StackOverflow question, I was wondering if it might be useful to have a function to create functional sequences? AFAIK, the only way to create a fseq is to start a pipe sequence with .:

library(magrittr)

. %>%
  mean() %>% 
  format(nsmall = 3)
#> Functional sequence with the following components:
#> 
#>  1. mean(.)
#>  2. format(., nsmall = 3)
#> 
#> Use 'functions' to extract the individual functions.

For defining each step in the sequence programmatically, it might be nice to have other interfaces. Perhaps something like this to create the above?

functional_sequence(
  mean(),
  format(nsmall = 3)
)

steps <- expression(
  mean(),
  format(nsmall = 3)
)

as_functional_sequence(step)

I haven’t thought about how to actually implement these, but I was wondering if this might be something that could potentially be added to magrittr?

@ggrothendieck
Copy link

ggrothendieck commented Aug 6, 2022

What would be nice here is a compose operator.

Consider this example. We need to use list here because otherwise by strips the class.

x <- .Date(1:3)
g <- c("a", "a", "b")
do.call("c", by(x, g, function(z) list(min(z))))

The function part can be expressed as shown below using magrittr:

library(magrittr)
do.call("c", by(x, g, . %>% min %>% list))

but it would be expressible in a more compact form if there were a compose operator. Suppose we call it $.>% and that f %.>% g is regarded to be equivalent to . %>% f %>% g. Then we could reduce the above to:

do.call("c", by(x, g, min %.>% list))

@lionel- lionel- added the feature a feature request or enhancement label Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants