Skip to content

Implement preduce? #163

Closed
Closed

Description

@hadley

Parallel reduce would allow you to specific arguments that varied across each call. Would be useful if you want to collapse many joins. Need to think if we could have some convention for naming input elements and matching with arguments.

Activity

  1. hadley commented on Mar 8, 2017

    @hadley
    MemberAuthor

    Note that the presence of init changes how the arguments align:

    out <- init
    out <- f(out, x[[1]], y[[1]])
    out <- f(out, x[[2]], y[[2]])
    
    out <- x[[1]]
    out <- f(out, x[[2]], y[[1]])
    out <- f(out, x[[3]], y[[2]])

    To match pmap() the call might look like:

    preduce(list(dfs, vars), left_join, ...) 

    but vars would have length one less than dfs (which makes sense because of fence-posting). Maybe also need reduce2():

    reduce2(dfs, vars, left_join, ...)

    Maybe just start with reduce2() and leave preduce() for now? Seems harder to imagine use case given reduce2().

  2. Ljupch0 commented on May 2, 2021

    @Ljupch0

    In terms of use case, I reach for reduce functions when adding cumulatively to an initial object. For instance with the officer package you can generate a powerpoint presentation. You start with an empty presentation and every function call adds a slide to the cumulative presentation. So instead of using pmap that would generate 50 presentations, I've needed a single presentation with 50 slides based on the same template, which sounds like preduce. The init is the starting empty template, and then every function call adds a slide. Some pseudo code:

    preduce <- function(.init, .l, .f, ... ) {
      # smart code
    }
    
    preduce(.init = starting_template, .l = data, .f = add_slide) 
    
    #would be
      starting_template %>% 
        add_slide(..1, ..2, ...) %>% 
        add_slide(..1, ..2, ...) %>% 
        add_slide(..1, ..3, ...) %>% ...
    

    I have working code of this example here. I needed to iterate a single vector so just reduce worked, but a more general preduce would be fantastic: https://github.com/Ljupch0/stock_presentations/blob/master/Multiple%20Tickers.RMd

  3. moodymudskipper commented on May 31, 2021

    @moodymudskipper
    Contributor

    I've been needing this too, the following seems to work fine :

    preduce <- function(.l,.f, ..., .init, .dir = c("forward", "backward")) {
      .dir <- match.arg(.dir)
      reduce(transpose(.l), function(x,y) exec(.f, x, !!!y, ...), .init = .init, .dir = .dir)
    }
    

    .init is mandatory here, the intended use case is to have a data.frame of arguments to apply row-wise in sequence on an object passed through .init

  4. added a commit that references this issue on Jan 10, 2025
    2d7fd45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions