Repository navigation
Pure functions: parts of the body without slots are not walked - #2102
Merged
Merged
Conversation
Applying a pure function replaces its slots with replace_slots, which went over every subexpression of the body. Parts without slots were already kept as they are (#2085), but still walked: a list passed to a function and written into a pure function inside it, like a graph in Combinatorica's code, was walked once per application, so mapping over its own elements cost the square of its length. Now an expression knows whether a Slot or SlotSequence occurs in it, heads included (f[#][x] has one in its head): worked out once and kept with it, forgotten when its head or elements change. replace_slots returns a part without slots at once. Five applications of {#1, Length[big]} & with a 300-element big make 3020 replace_slots calls on master, now 15. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Member
|
LGTM |
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.
Applying a pure function replaces its slots with
replace_slots, which went over every subexpression of the body. Parts without slots were already kept as they are (#2085), but they were still walked. In Combinatorica's code, a graph is passed to a function and written into a pure function inside it. That graph was walked once per application, so mapping over its own elements cost the square of its length.Change
SlotorSlotSequenceoccurs in it, heads included (f[#][x]has one in its head).replace_slotsreturns a part without slots at once.Measurement: five applications of
{#1, Length[big]} &with a 300-elementbigmake 3020replace_slotscalls on master, now 15.Tests:
test/core/test_replace_slots.py.##, nestedFunction,Hold.#[0] &,{a, {#[0]}} &,f[#][x] &,g[h[#]][x] &,f[##][y] &.The full pytest run and the doctests pass.
🤖 Generated with Claude Code