rescript-compiler icon indicating copy to clipboard operation
rescript-compiler copied to clipboard

Add a few Stdlib helpers

Open DZakh opened this issue 6 months ago • 1 comments

  • String.capitalize
  • String.isEmpty
  • Dict.size
  • Dict.isEmpty
  • Array.isEmpty
  • Map.isEmpty
  • Set.isEmpty

Let me know if I need to cherry-pick some changes.

DZakh avatar May 24 '25 14:05 DZakh

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript-lang/rescript@7516
@rescript/darwin-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-arm64@7516
@rescript/darwin-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-x64@7516
@rescript/linux-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-arm64@7516
@rescript/linux-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-x64@7516
@rescript/win32-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/win32-x64@7516

commit: 669de38

pkg-pr-new[bot] avatar May 24 '25 14:05 pkg-pr-new[bot]

I like these. @cknitt what do you think?

@DZakh could you rebase?

zth avatar Jul 12 '25 18:07 zth

I like these. @cknitt what do you think?

Certainly useful, my only concern is that Dict.size and Dict.empty sort of hide their performance implications (array allocation, O(n)).

Actually they could be implemented without the array allocation:

function size(dict) {
  let count = 0;
  for (let key in dict) {
    if (dict.hasOwnProperty(key)) count++;
  }
  return count;
}

cknitt avatar Jul 13 '25 06:07 cknitt

Done, here's the benchmark.

You can see how everything is ~5-10% faster than Object.keys version. Where Dict.isEmpty is two times faster.

The hasOwnProperty is not needed, since this is how for..in works anyways.

DZakh avatar Aug 03 '25 20:08 DZakh

Try running make test-analysis to update those snapshots.

nojaf avatar Aug 04 '25 06:08 nojaf

@nojaf I get the following error:


Package name mismatch for /Users/dzakh/code/DZakh/rescript/tests/analysis_tests/tests:
The package.json name is "@tests/analysis", while the rescript.json name is "test"
This inconsistency will cause issues with package resolution.

WARN:

Package name mismatch for /Users/dzakh/code/DZakh/rescript/tests/dependencies/rescript-react:
The package.json name is "@tests/rescript-react", while the rescript.json name is "@rescript/react"
This inconsistency will cause issues with package resolution.

[1/2] 🧹 Cleaned compiler assets in 0.00s
[2/2] 🧹 Cleaning .res.js files...

thread 'main' panicked at src/build/clean.rs:68:78:
called `Option::unwrap()` on a `None` value
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
make[1]: *** [clean] Error 101
make: *** [clean] Error 2

DZakh avatar Aug 04 '25 07:08 DZakh

Yeah, I've seen this one. Try and replace rescript clean with rescript legacy clean in the package.json files of those test projects.

nojaf avatar Aug 04 '25 07:08 nojaf