Skip to content

feat: Support reduce top level function #2591

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

feat: Support reduce top level function #2591

wants to merge 4 commits into from

Conversation

FBruzzesi
Copy link
Member

@FBruzzesi FBruzzesi commented May 22, 2025

What type of PR is this? (check all applicable)

  • πŸ’Ύ Refactor
  • ✨ Feature
  • πŸ› Bug Fix
  • πŸ”§ Optimization
  • πŸ“ Documentation
  • βœ… Test
  • 🐳 Other

Related issues

Checklist

  • Code follows style guide (ruff)
  • Tests added
  • Documented the changes

If you have comments or can explain your changes, please do so below

@MarcoGorelli , @dangotbanned I am opening a PR since I said I was working on this, but I am not particularly proud* of it to be honest.

In the lazy cases, there is a lot of code duplication, which also leads to reduce being poorly typed at the compliant level.

Now, if there was a way to unpack multi output expression (related to discord comment, the entire function would become:

def reduce(
    function: Callable[[Expr, Expr], Expr], exprs: IntoExpr | Iterable[IntoExpr]
) -> Expr:

    from functools import reduce as freduce

    if not exprs:
        msg = "At least one expression must be passed to `reduce`"
        raise ValueError(msg)
    if isinstance(exprs, Expr):
        exprs = [exprs]
    flat_exprs = flatten(exprs)

    return freduce(function, flat_exprs)

*there is one exception to this statement which is the example which for once is not the usual horizontal sum πŸ˜…


Edit: Clearly should have started with a draft

@FBruzzesi FBruzzesi added the enhancement New feature or request label May 22, 2025
@FBruzzesi FBruzzesi marked this pull request as draft May 22, 2025 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant