Skip to content

The "@ P" and "@ PP" Syntax#7

Open
abersheeran wants to merge 2 commits intomasterfrom
new-call-syntax
Open

The "@ P" and "@ PP" Syntax#7
abersheeran wants to merge 2 commits intomasterfrom
new-call-syntax

Conversation

@abersheeran
Copy link
Copy Markdown
Owner

@abersheeran abersheeran commented Apr 19, 2021

The "@ P" and "@ PP" Syntax

Related discussion: #6

Motivation

For partial functions, currently we have syntax F(func, *args, **kwargs) or FF(func, *args, **kwargs). The syntax have some drawbacks:

  1. It's hard to tell what the function is at the first sight, especially when func is a lambda expression.
  2. It's hard to tell what the parameters are, since the function and parameters are enclosed by the same parenthesis.

This PR propose an alternative syntax, placing the function to the very front of the expression, and separating function and the parameters using some operators. The syntax is described in the following section.

Syntax Description

The new syntax has four variants:

  1. func @ P(*args, **kwargs) is equivalent to F(func, *args, **kwargs);
  2. func @ P is equivalent to F(func);
  3. func @ PP(*args, **kwargs) is equivalent to FF(func, *args, **kwargs);
  4. func @ PP is equivalent to FF(func).

Examples

# Sum up all odd numbers in [0, 9]
range(10) | filter @ P(lambda x: x % 2) | sum @ P == 25
# equiv. to
range(10) | F(filter, lambda x: x % 2) | F(sum)

# Spread parameters to a summing function
[1, 2, 3] | (lambda x, y, z: x + y + z) @ PP == 6
# equiv. to
[1, 2, 3] | FF(lambda x, y, z: x + y + z)

@hsfzxjy
Copy link
Copy Markdown
Collaborator

hsfzxjy commented Apr 21, 2021

Checks on MacOS are failing these days due to nonintrinsic factors. See actions/runner-images#841

@abersheeran
Copy link
Copy Markdown
Owner Author

It's ok. Before the final merger, we will re-run CI.

Can you write some documentation to explain to others how to use it? Like #3. If you don’t have editing rights for this PR, I can close it and you will start another PR.

@hsfzxjy
Copy link
Copy Markdown
Collaborator

hsfzxjy commented Apr 21, 2021

OK. I'll edit it.

@hsfzxjy hsfzxjy changed the title Syntax to highlight the partial-ed callable The "@ P" and "@ PP" Syntax Apr 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants