feat(stdlib): add threading, conditional, and monadic macros#1545
Open
sqrew wants to merge 3 commits into
Open
feat(stdlib): add threading, conditional, and monadic macros#1545sqrew wants to merge 3 commits into
sqrew wants to merge 3 commits into
Conversation
- Added as-> threading macro for more flexible piping. - Added cond-let for multibranch conditional with Maybe bindings. - Integrated tests into test/control_macros.carp.
… macros - Added as-> and as->> for flexible named threading (first and last). - Added cond-let for multibranch conditional with Maybe bindings. - Added cond-> and cond->> for conditional threading. - Added some-> and some->> for monadic threading with short-circuiting. - Added maybe-and-then-> and result-and-then-> for complex monadic chaining. - Integrated comprehensive tests into test/control_macros.carp.
hellerve
suggested changes
Apr 23, 2026
| Each form in `steps` must return a value, not a Maybe.") | ||
| (defmacro some-> [val :rest steps] | ||
| `(match %val | ||
| (Maybe.Just x) (Maybe.Just (-> x %@steps)) |
Member
There was a problem hiding this comment.
Shouldn’t this call some-> here?
Member
There was a problem hiding this comment.
Also can we gensym here to avoid the hygiene leak with x?
| Each form in `steps` must return a value, not a Maybe.") | ||
| (defmacro some->> [val :rest steps] | ||
| `(match %val | ||
| (Maybe.Just x) (Maybe.Just (--> x %@steps)) |
Member
There was a problem hiding this comment.
Shouldn’t this call some->> here?
| acc | ||
| (maybe-and-then-internal | ||
| `(match %acc | ||
| (Maybe.Just x) (-> x %(car steps)) |
| acc | ||
| (result-and-then-internal | ||
| `(match %acc | ||
| (Result.Success x) (-> x %(car steps)) |
Member
There was a problem hiding this comment.
And here also a gensym? (And the e below)
|
|
||
| (doc cond-> "Conditional threading macro (first). | ||
| Threads `val` through each `form` in `steps` only if the corresponding `test` is true.") | ||
| (defmacro cond-> [val :rest steps] |
Member
There was a problem hiding this comment.
We should check the numbers of forms is even here.
| (hidden cond-let-internal) | ||
| (defndynamic cond-let-internal [xs] | ||
| (if (= (length xs) 0) | ||
| (list) |
Member
There was a problem hiding this comment.
This restricts the typing. It should be a Maybe.Nothing, right?
…to avoid duplication, even len check on cond->/cond->>, cond-let empty returns maybe.nothing, added test
Contributor
Author
|
One thing I noticed: I use -> and ->> whereas the stdlib uses both -> and --> for first and last threading. I wonder if I should alter these to follow the same convention. Anyone have input on this? |
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.
This PR introduces a suite of powerful control-flow macros to the Carp standard library, enhancing ergonomics for data threading, conditional logic, and monadic operations.
Changes:
Maybebindings, providing clean unwrapping for multiple options.MaybeorResult.test/control_macros.carp.These macros significantly improve the ergonomics of complex data pipelines and conditional logic.
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.