-
Couldn't load subscription status.
- Fork 143
Add reduce function to arrays #4290
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
base: master
Are you sure you want to change the base?
Add reduce function to arrays #4290
Conversation
Implement array reduce function with signature: fun reduce<U>(initial: U, _ f: fun (U, T): U): U Changes: - sema: Define ArrayReduceFunctionType and register in getArrayMembers - interpreter: Implement ArrayValue.Reduce and NativeArrayReduceFunction - vm: Register reduce function for both variable and constant-sized arrays - tests: Add tests for reduce in arrays_dictionaries_test.go and misc_test.go
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
Benchstat comparison
Results
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Per review comment: add function signature comments similar to ArrayMapFunctionType
|
Added comments in commit 86d3e8e. The function now includes:
Similar to how |
Per review comment: inline the argumentTypes slice directly in the invokeFunctionValue call
|
Done in commit a8942f8. Inlined |
function type is the same for variable-sized and constant-sized array types
Per review comment: Keep only the function signature comment
…git-manager.devin.ai/proxy/github.com/onflow/cadence into devin/1760470003-add-array-reduce-function
…git-manager.devin.ai/proxy/github.com/onflow/cadence into devin/1760470003-add-array-reduce-function
…based inner array mutation; update expectations
Description
Adds a
reducefunction to arrays in Cadence with the signature:The implementation follows the same three-layer pattern as the existing
mapfunction:Ufor the accumulator type, allowing it to differ from the array element typeTChanges
Type System
ArrayReduceFunctionTypewith generic type parameterUInterpreter
ArrayValue.Reducemethod that iterates and accumulates valuesNativeArrayReduceFunctionwrapperGetMethodfor array valuesTests
TestInterpretArrayReducefrom misc_test.go to array_test.go (per review feedback)ContainerMutatedDuringIterationError)Critical Review Areas
Generic type parameter handling: Verify
ArrayReduceFunctionTypecorrectly handles the case where accumulator typeUdiffers from element typeTStruct array test: The test
testStructArrayReduceoperates on an unauthorized reference&[S]and mutates struct fields inside the reducer. This works because struct elements are passed by value (copies), not by reference. Verify this is the intended test case - it demonstrates that mutations happen on copies when using unauthorized references.Resource array rejection: Confirm that rejecting reduce on resource arrays matches the intended behavior and is consistent with map
Link to Devin run: https://app.devin.ai/sessions/f6bc7843ea324388964f82a1cdc1b1b1
Requested by: [email protected]
masterbranchFiles changedin the Github PR explorer