-
Notifications
You must be signed in to change notification settings - Fork 137
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
Comments
Good idea! For functions that take a function which is passed the elements, e.g. For For |
Thanks for the insights.
It was a typo. I meant to write |
@turbolent I started looking into In the issue, I've proposed that we mutate the array itself. To do that we probably need support for reverse in 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? |
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 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 |
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 Arraysmap<T: AnyStruct, U: AnyStruct>(_ f: (T: U)) : [U; N]
andmap<T: AnyStruct, U: AnyStruct>(_ f: (T: U)) : [U]
. Will create a new Array without mutatingtransform<T: AnyStruct, U: T>(_ f: (T: U))
andtransform<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 mutating1, 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
The text was updated successfully, but these errors were encountered: