support map, filter, reduce as list methods#199
Merged
Conversation
list.map(fn), list.filter(fn) and list.reduce(init, fn) now lower to the
same loops as the free-function map/filter/reduce, so higher-order pipelines
read left to right and chain naturally:
nums.filter(fn(x: Int) => x > 5).map(fn(x: Int) => x * 2)
the checker gained method-call type rules mirroring the existing builtins
(receiver is the list, so they take one fewer argument), and the ir emitter
routes the method forms through shared lowering helpers extracted from the
call forms. the free-function spellings still work unchanged.
functional.pith is reworked to method syntax as a worked example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
summary
map,filter, andreducecan now be called as methods on a list, sohigher-order pipelines read left to right and chain naturally instead of
nesting calls inside-out:
the free-function spellings (
map(nums, fn), ...) still work unchanged.how
check_list_method_callnow recognizesmap/filter/reduce.the new method-form rules mirror the existing free-function builtins, but the
receiver is the list, so they take one fewer argument.
(
ir_emit_list_map_lowering, etc.) that take explicit operand indices; themethod forms reuse them with the receiver as the list. no new runtime code
and no new loop codegen.
examples/functional.pithis reworked to method syntax as a worked example(its output snapshot is unchanged).
what was tested
tests/cases/test_list_method_syntax.pithcovering map,filter, reduce, left-to-right chaining, and string-element lists
including
higher_order.pithwhich still uses the free-function formsnotes
while testing i noticed
List[Int].join(",")segfaults — but this reproducesidentically with the free-function form on
main, so it is a pre-existingruntime issue unrelated to this change. left out of scope; the tests format
int lists manually.