Skip to content

support map, filter, reduce as list methods#199

Merged
kacy merged 1 commit into
mainfrom
feat/list-method-syntax
May 23, 2026
Merged

support map, filter, reduce as list methods#199
kacy merged 1 commit into
mainfrom
feat/list-method-syntax

Conversation

@kacy
Copy link
Copy Markdown
Owner

@kacy kacy commented May 23, 2026

summary

map, filter, and reduce can now be called as methods on a list, so
higher-order pipelines read left to right and chain naturally instead of
nesting calls inside-out:

doubled := nums.map(fn(x: Int) => x * 2)
evens   := nums.filter(fn(x: Int) => x % 2 == 0)
total   := nums.reduce(0, fn(acc: Int, x: Int) => acc + x)

# chaining: filter then map
big := nums.filter(fn(x: Int) => x > 5).map(fn(x: Int) => x * 2)

the free-function spellings (map(nums, fn), ...) still work unchanged.

how

  • checker: check_list_method_call now recognizes map/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 emitter: the call-form lowerings were split into shared helpers
    (ir_emit_list_map_lowering, etc.) that take explicit operand indices; the
    method forms reuse them with the receiver as the list. no new runtime code
    and no new loop codegen.
  • examples/functional.pith is reworked to method syntax as a worked example
    (its output snapshot is unchanged).

what was tested

  • new regression tests/cases/test_list_method_syntax.pith covering map,
    filter, reduce, left-to-right chaining, and string-element lists
  • full regression suite (92 passed, 0 failed)
  • examples via both the native and self-hosted compilers (83 each, 0 failed),
    including higher_order.pith which still uses the free-function forms
  • lint clean on the changed self-host sources

notes

while testing i noticed List[Int].join(",") segfaults — but this reproduces
identically with the free-function form on main, so it is a pre-existing
runtime issue unrelated to this change. left out of scope; the tests format
int lists manually.

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.
@kacy kacy merged commit 6aed3b4 into main May 23, 2026
2 checks passed
@kacy kacy deleted the feat/list-method-syntax branch May 23, 2026 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant