Support array-first argument order for higher-order functions#36
Conversation
- map(array, fn) alongside map(fn, array) - filter(array, fn) alongside filter(fn, array) - fold/reduce(array, init, fn) alongside fold/reduce(fn, init, array) - find(array, fn) alongside find(fn, array) - some(array, fn) alongside some(fn, array) - every(array, fn) alongside every(fn, array) Function-first order still works for backwards compatibility. Updated documentation to only show array-first syntax. Updated language service documentation for autocompletions. Co-authored-by: Sander-Toonen <5106372+Sander-Toonen@users.noreply.github.com>
…nization Co-authored-by: Sander-Toonen <5106372+Sander-Toonen@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This pull request adds support for array-first argument order for higher-order array functions (e.g., map([1, 2, 3], x => x * 2)) while maintaining backward compatibility with the existing function-first order. The new array-first syntax is now the documented and recommended approach, with autocompletion hints showing this order exclusively.
Changes:
- Modified seven array operation functions (
map,filter,fold,reduce,find,some,every) to detect and support both argument orders at runtime - Updated all documentation examples in
docs/syntax.mdto use array-first syntax - Updated language service parameter hints to show array-first order
- Added 46 comprehensive tests covering both argument orders and edge cases
- Updated error messages to reflect array-first signature
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/functions/array/operations.ts | Implemented runtime argument order detection for all higher-order array functions with backward compatibility |
| test/functions/functions-array-first-order.ts | Added comprehensive test suite covering array-first usage, backward compatibility, and edge cases |
| test/functions/functions-array-errors.ts | Updated error message tests to reflect new array-first signature format |
| docs/syntax.md | Updated all function signatures and examples to demonstrate array-first syntax (with minor typo) |
| src/language-service/language-service.documentation.ts | Updated autocompletion hints to show array-first parameter order |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -359,14 +359,14 @@ factorial(x) = x < 2 ? 1 : x * factorial(x - 1) | |||
| These functions can than be used in other functions that require a function argument, such as `map`, `filter` or `fold`: | |||
There was a problem hiding this comment.
Typo: "can than be" should be "can then be".
| These functions can than be used in other functions that require a function argument, such as `map`, `filter` or `fold`: | |
| These functions can then be used in other functions that require a function argument, such as `map`, `filter` or `fold`: |
| } else if (arg3 === undefined) { | ||
| return undefined; |
There was a problem hiding this comment.
The condition else if (arg3 === undefined) at line 69 is problematic. If arg3 is undefined, the function-first detection at line 64 would have already matched (since it checks Array.isArray(arg3) || arg3 === undefined).
This check will only be reached if arg1 is not a function AND arg1 is not an array. In that case, returning undefined silently masks what should be an error. For example:
fold("string", 0, undefined)would return undefined instead of throwing an errorfold(123, 0, undefined)would return undefined instead of throwing an error
This should either be removed (so the error is thrown) or the logic should be reconsidered. The behavior is inconsistent with other functions like map, filter, find, some, and every which don't have this special case.
| } else if (arg3 === undefined) { | |
| return undefined; |
Higher-order functions like
map,filter,foldnow accept array-first argument order (e.g.,map(items, fn)) in addition to the existing function-first order. Documentation and autocompletions exclusively suggest the array-first form.Changes
src/functions/array/operations.ts): Modifiedmap,filter,fold,reduce,find,some,everyto detect argument order at runtime and handle both formsdocs/syntax.md): Updated all examples to array-first syntaxsrc/language-service/language-service.documentation.ts): Parameter order in autocompletion hints now shows array-firstExample
Detection logic: if first argument is an array and last is a function → array-first; if first is a function → function-first.
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.