feat(stdlib): add variadic arithmetic macros and in-place inc/dec#1529
feat(stdlib): add variadic arithmetic macros and in-place inc/dec#1529sqrew wants to merge 4 commits intocarp-lang:masterfrom
Conversation
hellerve
left a comment
There was a problem hiding this comment.
I like this in general!
One thing: if we want to express that integers form a ring, we should handle identity in sum and product(0 and 1, respectively). That’s not necessary.
|
Ci seems unhappy. 365m runtime seems, uh, excessive. |
- Added sum, product, sub, and div macros.\n- Added inc! and dec! for in-place updates.\n- Implemented left-associative expansion for non-commutative operations.\n- Added comprehensive tests in test/macros.carp.
fc1bd7f to
e7f14b3
Compare
…nterminable loop that murdered github CI
| (doc inc! "Increments a variable in-place.") | ||
| (defmacro inc! [var] | ||
| (list 'set! var (list 'inc var))) | ||
|
|
||
| (doc dec! "Decrements a variable in-place.") | ||
| (defmacro dec! [var] | ||
| (list 'set! var (list 'dec var))) |
There was a problem hiding this comment.
I just realized we already have ++ and --, which do the same thing. I actually like this naming better. What do the others think? I think one should go in favor of the other in any case.
There was a problem hiding this comment.
The thing I like about inc!/dec! is that the ! makes it clear it's an in place mutation and that it's a macro. I personally prefer ++ (because it's cooler) and totally spaced that it was even here. I'm totally open to either option honestly.
|
Turns out the CI issues were name conflicts causing a hang up during macro expansion with common names like sum/sub/mul/div etc, so I've added a -variadic suffix (ie sum-variadic, etc) to each of them. This definitely makes it ugly as hell. Unsure if we really want them added to the language in that form. Thoughts from you guys? |
This PR introduces several ergonomic improvements to the arithmetic facilities in the Carp standard library.
Changes:
sum,product,sub, anddiv. These allow for more idiomatic Lisp-style arithmetic like(sum 1 2 3 4).(sub x)now correctly expands to(neg x).inc!anddec!macros for cleaner variable updates.build-vararg-left) to ensure correct results for non-commutative operations like subtraction and division.test/macros.carp.Disclaimer: This PR was built with the assistance of Gemini (an AI assistant) working in collaboration with @sqrew to improve the Carp standard library ergonomics.