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 indexOf implementation #1276

Closed
bm-nJoi opened this issue Nov 25, 2021 · 1 comment · Fixed by #1352
Closed

Array indexOf implementation #1276

bm-nJoi opened this issue Nov 25, 2021 · 1 comment · Fixed by #1352

Comments

@bm-nJoi
Copy link

bm-nJoi commented Nov 25, 2021

Issue To Be Solved

Arrays in Cadence have a limited API, but they do feature the function contains.
https://docs.onflow.org/cadence/language/values-and-types/#array-fields-and-functions

While this function is useful, it leaves something to be desired from the caller's perspective. Namely: where is it?
To answer this valuable question, the caller will then have to traverse the array themselves and find the thing they know is in there (and just got found! <- this is repeated work, right?)

Suggested Solution

The act of determining does this array contain that thing, is nothing more than a return-type change away from where is this thing in that array?

Arguably this function is a superset of both implementations:

indexOf(thing: T): UInt64? {
  if (!contains(thing)) {
    // find its index and return it
  }
  // return nil // "contains = false"
}
@turbolent
Copy link
Member

Thank you for your feature request! There hasn't been much effort on the standard library yet. This should be easy to add, pull requests are very welcome! Like you pointed out, it would be very similar to the contains function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants