Skip to content

Map-like helper functions #3339

Description

@WardBrian

The map-family of functions would make writing certain kinds of models easier. Because we lack closures (#2197) it would make sense to make them variadic, so a basic map would have the signature

map(f, T, ...) which would call f(T[1], ...), f(T[2], ...) etc.

A few variants of this would be useful, such as map2 which maps over two containers of the same size,
and specialized row_map/column_map for matrices, which would allow e.g.

  matrix[N, K] x;  // original covariates
  matrix[N, K] x_std = row_map(standardize, x);  // standardized covariates

(suggested by @bob-carpenter)

Activity

  1. WardBrian commented on Jun 30, 2026

    @WardBrian
    MemberAuthor

    @florence-bockting this may be a nice stepping stone beyond modifying the existing Laplace code if you'd like to get involved more in the C++ development after your first PR is done!

  2. SteveBronder commented on Jul 14, 2026

    @SteveBronder
    Collaborator

    I think this is a very cool idea. I think we would want something that has similar mechanics to filter_map(...), but has specific rules for operating on particular containers.

    To make @WardBrian 's a little more concrete I think we would want these signatures

    // General map on elements of `T[i]`
    // Calls f(t[i])
    template <typename F, typename T>
    auto map(F&& f, T&& t);
    
    /**
     * Iterate over N items with identical dimensions
     *  f(args<0>[i], args<1>[i], ..., args<j>[i]);
     */
    template <typename F, typename... Types>
    auto mapN(F&& f, Types&&... args);

    And then we would have something similar for row_map and row_mapN along with col_map and col_mapN

    // General map on rows of `T`
    // Calls f(t.row(i))
    template <typename F, typename T>
    auto row_map(F&& f, T&& t);
    
    /**
     * Iterate over the rows of N items with identical dimensions
     *  f(args<0>.row(i), args<1>.row(i), ..., args<j>.row(i));
     */
    template <typename F, typename... Types>
    auto row_mapN(F&& f, Types&&... args);
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions