feat(stdlib): improve String module with functional API and optimizat…#1527
Open
sqrew wants to merge 3 commits intocarp-lang:masterfrom
Open
feat(stdlib): improve String module with functional API and optimizat…#1527sqrew wants to merge 3 commits intocarp-lang:masterfrom
sqrew wants to merge 3 commits intocarp-lang:masterfrom
Conversation
Contributor
sqrew
commented
Apr 9, 2026
- Added String.map, String.filter, String.any?, String.all?, String.reduce
- Added String.replace utility
- Optimized starts-with? and ends-with? with efficient C implementations
- Integrated tests for new functionality into test/string.carp
- Updated CHANGELOG.md
31228f9 to
708c6bc
Compare
hellerve
suggested changes
Apr 9, 2026
| lstrings (Array.length strings)] | ||
| (for [i 0 lstrings] | ||
| (set! sum (+ sum (String.length (Array.unsafe-nth strings i))))) | ||
| (set! sum (+ sum (length (Array.unsafe-nth strings i))))) |
Member
There was a problem hiding this comment.
Why did you change that? Will this genericize the function?
| (split-by s &[\newline])) | ||
|
|
||
| (doc map "Maps a function over the characters of a string, returning a new string.") | ||
| (defn map [f s] |
Member
There was a problem hiding this comment.
Should we call this copy-map, to be explicit like in the array module?
| (defn reduce [f init s] | ||
| (Array.reduce f init &(chars s))) | ||
|
|
||
| (doc replace "Replaces all occurrences of a substring in a string with another string.") |
Member
There was a problem hiding this comment.
Shouldn’t we leave this to the Pattern module?
| - fix: #1347 by ignoring generically typed symbols on printing C (#1373) | ||
| - fix: nix install by using correct pkg-configDepends config key (#1372) | ||
| - fix: type signature of Array.unsafe-raw (#1375) | ||
| - feat: add map, filter, any?, all?, reduce, and replace to String (#XXXX) |
Member
There was a problem hiding this comment.
The changelog entries are in the wrong version, and shouldn’t be part of the PR.
708c6bc to
4b2b7aa
Compare
hellerve
suggested changes
Apr 26, 2026
Member
hellerve
left a comment
There was a problem hiding this comment.
We should also mention that these all operate on chars, not bytes (as we do in chars itself).
| (Array.reduce f init &(chars s))) | ||
|
|
||
| (doc replace "Replaces all occurrences of a literal substring in a string with another string.") | ||
| (defn replace [s needle replacement] |
Member
There was a problem hiding this comment.
This is quadratic and allocation-heavy. Can we optimize? Also, we can collapse patterns like let+do into let-do.
…ions - Added String.copy-map, String.copy-filter, String.any?, String.all?, String.reduce - Added String.replace utility (literal substring replacement) - Optimized starts-with? and ends-with? with efficient C implementations - Integrated tests for new functionality into test/string.carp
- Reverted tail signature to use standard Fn and &String. - Renamed map/filter to copy-map/copy-filter for consistency. - Documented String.replace as literal substring replacement. - Verified 116 tests passing.
- Implemented high-performance C versions of starts-with?, ends-with?, and replace.\n- Added functional helpers: copy-map, copy-filter, any?, all?, and reduce.\n- Updated docstrings to clarify UTF-8 character handling.\n- Added comprehensive edge-case and Unicode tests.\n- All 121 string tests passing.
437362b to
f9dfdbb
Compare
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.