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

Anonymous functions and/or pipelining? #374

Open
bsidhom opened this issue Feb 23, 2024 · 3 comments
Open

Anonymous functions and/or pipelining? #374

bsidhom opened this issue Feb 23, 2024 · 3 comments

Comments

@bsidhom
Copy link

bsidhom commented Feb 23, 2024

Thought of how this might be useful when trying to work around #305. I'm picturing being able to do something such as the following:

1 / 30 mpg -> L / km // (fn(x) = x * 100)

In the above scenario, the main benefit is clarity. You could scale the incoming value, but then it's unclear what's going on. In any case, you're still "lying" about the units, so it's not sufficient to actually address #305. Operator precedence of // also makes this a bit nicer, though the parentheses above are a bit annoying.

Partial function application via // could also make this a bit less annoying, e.g.:

fn scale(a, x) = a * x
1 / 30 mpg -> L / km // scale(100)

As noted above, this example is technically incorrect because the units display incorrectly given the intention.

In general, there are a lot of directions to go in terms of syntax, so I'm not sure what this would best look like. The focus should probably be on ergonomics and making things obvious and unambiguous without tons of parens.

@bsidhom
Copy link
Author

bsidhom commented Feb 23, 2024

Just noticed #261 while searching for similar issues. That contains a discussion about pipelines and partial function application.

@sharkdp
Copy link
Owner

sharkdp commented Feb 25, 2024

when trying to work around #305

Just a note: a workaround is to introduce a new unit:

>>> unit liter_per_100km = liter per 100 km
>>> 1 / 30 mpg -> liter_per_100km

    = 7.84049 liter_per_100km    [Area]

@sharkdp
Copy link
Owner

sharkdp commented Feb 25, 2024

As for the original issue: Yes, we definitely want:

Things I'm not yet sure about:

  • Do we want partial function application "by default"? Or would users have to curry/uncurry manually, i.e. write scale as fn scale(a) = |x| a * x and then use it with expr -> scale(10).
  • Do we need anonymous functions if we have closures and lambdas?
  • Do we want to keep both expr // fn and expr -> fn? Right now, they are not 100% equivalent, expr // fn can only be used for "proper" functions, while expr -> fn can also be used for other callables (function pointers), like in now() -> tz("Europe/Berlin")

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

No branches or pull requests

2 participants