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

Array additions #49

Merged
merged 19 commits into from
Mar 1, 2023
Merged

Array additions #49

merged 19 commits into from
Mar 1, 2023

Conversation

glennsl
Copy link
Contributor

@glennsl glennsl commented Feb 18, 2023

This adds:

  • make, seems necessary to be able to create initialized arrays without imperatively pushing onto an empty array.
  • init, is make with an initializer function instead of a constant.
  • findMap, is surprisingly useful, to find an element in a nested array, for example ([[1, 2], [3]]->findMap(find(_, el => ...))), or to find the first element in an array<option<_>> that is not None.
  • keepSome, unwraps the options in an array<option<_>> while discarding the Nones.

It also replaces the reimplementation of flatMap with a binding to Array.prototype.flatMap.

This is, to begin with, just to gather some feedback on the relevance, naming and implementations, before I add documentation and tests.

TODO:

  • Documentation
  • Tests
  • Changelog

@glennsl glennsl marked this pull request as draft February 18, 2023 14:20
@glennsl glennsl force-pushed the feat/array-additions branch from 1129d62 to 39c6af0 Compare February 18, 2023 14:28
Copy link
Member

@cknitt cknitt left a comment

Choose a reason for hiding this comment

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

Thanks! Here is some feedback from my part.

What do you think @zth?

src/Core__Array.res Outdated Show resolved Hide resolved
src/Core__Array.res Outdated Show resolved Hide resolved
src/Core__Array.res Outdated Show resolved Hide resolved
src/Core__Array.res Show resolved Hide resolved
@zth
Copy link
Collaborator

zth commented Feb 18, 2023

Thanks @glennsl, these additions look quite useful. I added my comments to the discussion as well.

@glennsl glennsl force-pushed the feat/array-additions branch from b4d2003 to 889e993 Compare February 19, 2023 19:16
@glennsl glennsl marked this pull request as ready for review February 19, 2023 19:17
@glennsl
Copy link
Contributor Author

glennsl commented Feb 19, 2023

I suppose this could be merged now, since neither documentation for the rest of the module or a test framework is in place yet. It could also be what initiates both. Up to you!

@zth
Copy link
Collaborator

zth commented Feb 19, 2023

If you don't mind, I think adding both in this PR would be good, so it doesn't get left behind. Would that be OK? Let me know how I can help out if needed.

@zth
Copy link
Collaborator

zth commented Feb 19, 2023

Also, a change log entry would be good as well.

@glennsl
Copy link
Contributor Author

glennsl commented Feb 19, 2023

Sure. Do you have any thoughts on testing framework? Should I pull in something like rescript-jest or make something very simple and bespoke?

@zth
Copy link
Collaborator

zth commented Feb 19, 2023

We have a simple one already in Test.res that we can start by using. Check out PromiseTest.res for how to use it.

Thank you for all your contributions so far by the way, very valuable! 👍

@glennsl glennsl force-pushed the feat/array-additions branch from 889e993 to ffc18ee Compare February 26, 2023 14:53
@glennsl
Copy link
Contributor Author

glennsl commented Feb 26, 2023

Added documentation, tests and updated the changelog. Also fixed reduce and reduceReverse, which were reimplemented instead of binding to its native counterparts. That did require changing the type signatures and renaming reduceReverse to reduceRight though.

@zth
Copy link
Collaborator

zth commented Feb 27, 2023

Great! Taking a look again soon.

Copy link
Collaborator

@zth zth left a comment

Choose a reason for hiding this comment

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

Actually, reduce and reduceReverse were intentionally taken from Belt instead of using their native counterparts because the Belt version allows for full type inference whereas the native one does not. So that was a deliberate decision.

Just had a small comment, other than that and the reduce stuff this is good to go from my end. Thanks!

cc @cknitt if you want to have a last look as well.

src/Core__Array.resi Outdated Show resolved Hide resolved
@cknitt
Copy link
Member

cknitt commented Feb 28, 2023

Actually, reduce and reduceReverse were intentionally taken from Belt instead of using their native counterparts because the Belt version allows for full type inference whereas the native one does not. So that was a deliberate decision.

If it's just for the improved argument order for better type inference, then we could simply do

@send external reduce: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduce"

let reduce = (arr, x, f) => reduce(arr, f, x)

(same for reduceRight).

@glennsl
Copy link
Contributor Author

glennsl commented Feb 28, 2023

Indeed. Seems odd to have them reimplemented and with different names when only a reordering of the arguments is needed.

Could probably also just label the argument to allow it to be moved before the callback function?

@send external reduce: (array<'b>, ('a, 'b) => 'a, ~init: 'a) => 'a = "reduce"

@zth
Copy link
Collaborator

zth commented Feb 28, 2023

IIRC the Belt version actually has better performance than the native one. Anyway, just reordering the arguments like that is fine. Let's do that instead and axe the reimplementation.

Side note - it's entirely possible that we'll have a way to do this directly in the binding in the future too, which would make it zero cost. Not there yet though.

@glennsl
Copy link
Contributor Author

glennsl commented Feb 28, 2023

So, reorder, not label?

@zth
Copy link
Collaborator

zth commented Feb 28, 2023

Correct.

@glennsl
Copy link
Contributor Author

glennsl commented Feb 28, 2023

Arguments reordered! Also reordered the type variable names for consistency.

@zth
Copy link
Collaborator

zth commented Feb 28, 2023

Fantastic, great work @glennsl ! Good to go from my end. @cknitt ?

@cknitt
Copy link
Member

cknitt commented Mar 1, 2023

Yes, great work! I think it would be good to use pipe -> in the examples, but that can also be done separately later as it applies to other modules, too. Let's merge now! 🙂

@zth zth merged commit 23ace1f into rescript-lang:main Mar 1, 2023
@glennsl glennsl deleted the feat/array-additions branch March 1, 2023 11:52
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.

3 participants