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

Make the reverse function call more useful #518

Merged

Conversation

irevoire
Copy link
Contributor

@irevoire irevoire commented Aug 3, 2024

Hey, while working on #515, I didn't like the API around lists, especially the way we don't chain calls, which makes the code very hard to read.

With this PR, the following chunks of code are equivalent:

sum(map(sqrt, range(0, 10_000)))
range(0, 10_000) // map(sqrt) // sum

It's clearly inspired by the |> operator of OCaml; I believe Haskell has something similar with the $ or & operator as well.
Obviously, it's not as clean as in these two languages since numbat uses parens to call functions.
But it still works, and it should become even more useful once we have closures.

Note

For the future, we should take care while designing the APIs (and especially the list/set/map APIs) that the data structure is always the last parameter


Also, I think I already asked you a long time ago and forgot the answer, but what do you think of renaming (or duplicating) this operator with another symbol?
I’ve never seen // anywhere else, and it doesn't ring a bell at all when I use it. I would be way more comfortable with the arroy of OCaml |>, which shows its intent more clearly.

@sharkdp
Copy link
Owner

sharkdp commented Aug 4, 2024

With this PR, the following chunks of code are equivalent:

sum(map(sqrt, range(0, 10_000)))
range(0, 10_000) // map(sqrt) // sum

Very nice. I hadn't thought of that!

I’ve never seen // anywhere else, and it doesn't ring a bell at all when I use it. I would be way more comfortable with the arroy of OCaml |>, which shows its intent more clearly.

// is inspired by Mathematica which uses it for the same (https://reference.wolfram.com/language/guide/FunctionCompositionAndOperatorForms.html)

what do you think of renaming (or duplicating) this operator with another symbol?

I'd be okay with that. I don't think // is a particularly fine choice. My only minor concern is that it's easier to type than |> (at least on a German keyboard). I like how |> looks. And if it has precedent in OCaml, even better.

Let's go with a breaking change (instead of providing |> in addition to //).

For the future, we should take care while designing the APIs (and especially the list/set/map APIs) that the data structure is always the last parameter

Is there anything that you would suggest changing now? The list feature is not even released officially, so I'm happy to change function signatures/names/etc.

Copy link
Owner

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice. Feel free to rewrite some (list-related) Numbat code using this new feature. But that is not a must, of course. I'm fine merging this as is

@irevoire
Copy link
Contributor Author

irevoire commented Aug 5, 2024

Feel free to rewrite some (list-related) Numbat code using this new feature

I looked at all the functions in list.nbt and statistics.nbt, only cons_end had the list and element inverted.

I think we can merge now, I’ll do another PR to rename the operator so it's easier to revert if we need to.

My only minor concern is that it's easier to type than |> (at least on a German keyboard).

Huum yes it's a bit harder on french keyboard as well I guess 🤔
Haskell uses & from what I understand which I find strange since & is used so much for binary computation in other languages.
So that could be an option as well, I think it still convey the idea that you're going to chain stuff up.

But IMO |> it better at avoiding confusions.

I like how |> looks. And if it has precedent in OCaml, even better.

And it looks even better with ligatures on 😁
image

@irevoire irevoire force-pushed the make-the-reverse-function-call-more-useful branch from 5ecdc17 to 889705b Compare August 5, 2024 08:23
Copy link
Owner

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@sharkdp sharkdp merged commit 64b8626 into sharkdp:master Aug 5, 2024
15 checks passed
@irevoire irevoire deleted the make-the-reverse-function-call-more-useful branch October 5, 2024 21:31
@irevoire
Copy link
Contributor Author

irevoire commented Oct 5, 2024

Hey, coming back to this one thousand years later.
I discovered a language that uses exactly our syntax: The gleam https://gleam.run/
Just in case they change their front page, here's another random example from the internet: https://github.com/trilowy/exercism/blob/b12cb0becab36e88d0fb5c6b14e520b3505dd220/gleam/sum-of-multiples/src/sum_of_multiples.gleam#L5-L7

So I guess we weren't wrong about that in the end 😁

@sharkdp
Copy link
Owner

sharkdp commented Oct 8, 2024

Cool. Thank you for the feedback.

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