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

Implement additional Array & Variable-size Array functions #2605

Open
3 of 4 tasks
darkdrag00nv2 opened this issue Jun 23, 2023 · 4 comments
Open
3 of 4 tasks

Implement additional Array & Variable-size Array functions #2605

darkdrag00nv2 opened this issue Jun 23, 2023 · 4 comments
Assignees

Comments

@darkdrag00nv2
Copy link
Contributor

darkdrag00nv2 commented Jun 23, 2023

Issue to be solved

A few more functions can be implemented for Array and Variable-size Array types.

Suggested Solution

For [T] or [T; N], these include:

  • reverse(): Reverses the array by mutating it. Available on both Array and Variable-size Arrays
  • map<T: AnyStruct, U: AnyStruct>(_ f: (T: U)) : [U; N] and map<T: AnyStruct, U: AnyStruct>(_ f: (T: U)) : [U]. Will create a new Array without mutating
  • transform<T: AnyStruct, U: T>(_ f: (T: U)) and transform<T: AnyStruct, U: T>(_ f: (T: U)). Will mutate the value. Some discussions happened in Discord on this.
  • filter(_ pred: (T: Bool)) : [T]. Only available on variable-size Arrays. Will create a new value without mutating

1, 2 and 3 are taken from https://github.com/green-goo-dao/flow-utils/blob/main/cadence/contracts/ArrayUtils.cdc.

We could also add zip, flatten and others but I think they are not needed right now. We can wait for requests for those before implementing them.

Epic: #1972

@turbolent
Copy link
Member

Good idea!

For functions that take a function which is passed the elements, e.g. map, transform and filter:
For resource-kinded inputs, the callbacks can't be the resources themselves, but could be references, or the functions are not defined for such inputs (already done for other functions).

For transform in particular cannot exist for resource-kinded inputs: The result would overwrite the existing resource element, implicitly destroying it, which is not allowed.

For filter, the type parameter for U should be defined. Again, for resource-kinded inputs, the function should probably return references, or not be defined.

@darkdrag00nv2
Copy link
Contributor Author

Thanks for the insights.

For filter, the type parameter for U should be defined.

It was a typo. I meant to write T instead of U. Basically filter on [T] will also return [T].

@darkdrag00nv2
Copy link
Contributor Author

@turbolent I started looking into reverse.

In the issue, I've proposed that we mutate the array itself. To do that we probably need support for reverse in atree itself.

Alternatively, we can have the function return a new array with entries reversed (except for resources). Similar to what the function in https://github.com/green-goo-dao/flow-utils/blob/main/cadence/contracts/ArrayUtils.cdc does.

Thoughts?

@turbolent
Copy link
Member

Starting with either variant works (mutating in-place or returning a new value)

It might be a good idea to name the functions accordingly, e.g. Swift has reverse for the in-place operation, and reversed for the one returning a new value (likewise, e.g. sort and sorted).

For the in-pace operation, there shouldn't be any need for support in atree, a simple loop over half the array swapping elements using the Cadence interpreter.ArrayValue functions like Get and Set should work.

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

No branches or pull requests

3 participants