Skip to content

feat(stdlib): improve String module with functional API and optimizat…#1527

Open
sqrew wants to merge 3 commits intocarp-lang:masterfrom
sqrew:stdlib-string-improvements
Open

feat(stdlib): improve String module with functional API and optimizat…#1527
sqrew wants to merge 3 commits intocarp-lang:masterfrom
sqrew:stdlib-string-improvements

Conversation

@sqrew
Copy link
Copy Markdown
Contributor

@sqrew 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

@sqrew sqrew force-pushed the stdlib-string-improvements branch from 31228f9 to 708c6bc Compare April 9, 2026 19:18
Comment thread core/String.carp Outdated
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)))))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change that? Will this genericize the function?

Comment thread core/String.carp Outdated
(split-by s &[\newline]))

(doc map "Maps a function over the characters of a string, returning a new string.")
(defn map [f s]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we call this copy-map, to be explicit like in the array module?

Comment thread core/String.carp Outdated
(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.")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn’t we leave this to the Pattern module?

Comment thread CHANGELOG.md Outdated
- 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)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changelog entries are in the wrong version, and shouldn’t be part of the PR.

@sqrew sqrew force-pushed the stdlib-string-improvements branch from 708c6bc to 4b2b7aa Compare April 10, 2026 02:39
Copy link
Copy Markdown
Member

@hellerve hellerve left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also mention that these all operate on chars, not bytes (as we do in chars itself).

Comment thread core/String.carp Outdated
(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]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is quadratic and allocation-heavy. Can we optimize? Also, we can collapse patterns like let+do into let-do.

sqrew added 3 commits April 27, 2026 10:49
…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.
@sqrew sqrew force-pushed the stdlib-string-improvements branch from 437362b to f9dfdbb Compare April 27, 2026 15:26
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.

2 participants