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

New: swap #42

Merged
merged 2 commits into from
Jun 23, 2023
Merged

New: swap #42

merged 2 commits into from
Jun 23, 2023

Conversation

ranneyd
Copy link
Contributor

@ranneyd ranneyd commented Jun 2, 2023

Ramda 0.29.0 introduced a simple swap function. Here's the typing for it. Full disclosure: I copy/pasted the code from adjust.d.ts since the function signatures are similar.

@ranneyd ranneyd changed the title Adding typing for swap function Update: swap Jun 2, 2023
@ranneyd ranneyd changed the title Update: swap New: swap Jun 2, 2023
@Harris-Miller
Copy link
Collaborator

Nice. Just do two things for me:

  • reverse the overload order (see reduce.d.ts for an example of the correct ordering)
  • add tests

@ranneyd
Copy link
Contributor Author

ranneyd commented Jun 2, 2023

@Harris-Miller thanks for the quick response! On it.

@ranneyd
Copy link
Contributor Author

ranneyd commented Jun 2, 2023

@Harris-Miller can you take another look?

Note: I had some lint errors. I fixed those and also updated CONTRIBUTING.md to call out running npm run lint during testing. 😄

Copy link
Contributor

@lax4mike lax4mike left a comment

Choose a reason for hiding this comment

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

Look good!

@@ -0,0 +1,58 @@
import { Placeholder } from './util/tools';

// swap(indexA)
Copy link
Contributor

Choose a reason for hiding this comment

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

I really like the comments! Makes all these overloads much easier to follow 👍🏻

indexB: number
): {
// swap(__, indexB)(indexA)(list)
(indexA: number): <T>(list: readonly T[]) => T[];
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm curious about the readonly keyword here. Is this just preventing the definition of swap from mutating the list argument? I guess the implementation of swap is already written in the other repo and these definitions do not restrict that implementation.

Does readonly do anything else that restricts the consumer or type inference in any way? It probably can't hurt to have these here, but just wanted to see if I was missing something else.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're spot-on, but I wouldn't describe it as "only" doing what it does because I think it's a pretty powerful TS feature.

If you have a readonly array and a function that takes an array, you have no guarantee that the function won't modify the array. Thus, TS won't let you pass your array into the function. If you make the argument readonly, then you're telling the caller that you won't modify it. Plus, a non-readonly array basically extends a readonly array, so it's also a valid argument to the function. So slapping a readonly on your parameter means you can accept any argument, readonly or not.

Every function in Ramda is a pure function so there should be readonlys throughout, which just means that you can call them with whatever you want. I've run into really annoying libraries that don't include the readonly even though they actually are. If you have a dropdown component, then define const DROPDOWN_OPTIONS = [...] as const, if the dropdown typing doesn't have the options as readonly you won't be able to pass that in.

Copy link
Contributor

Choose a reason for hiding this comment

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

ahh, that makes sense. It does help the consumer in that this function now accepts readonly arrays. Thanks for the explanation!

Copy link
Collaborator

Choose a reason for hiding this comment

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

@Harris-Miller Harris-Miller merged commit 8c13c94 into ramda:develop Jun 23, 2023
jeysal added a commit to jeysal/types that referenced this pull request Jun 28, 2023
* origin/develop:
  Update: Overload reordering (ramda#48)
  Update: count (ramda#40)
  Update: `of` now binary function (ramda#41)
  New: swap (ramda#42)
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