Skip to content

[All] Add missing Array, List, and Seq random choice/shuffle/sample members and tests#4488

Merged
ncave merged 11 commits intofable-compiler:mainfrom
ncave:random
Apr 6, 2026
Merged

[All] Add missing Array, List, and Seq random choice/shuffle/sample members and tests#4488
ncave merged 11 commits intofable-compiler:mainfrom
ncave:random

Conversation

@ncave
Copy link
Copy Markdown
Collaborator

@ncave ncave commented Apr 5, 2026

  • [All] Add missing Array, List, and Seq random choice/shuffle/sample members and tests
  • [Dart/Rust] Add missing System.Random implementations and tests

@ncave ncave changed the title Random [All] Add missing Array, List, and Seq random choice/shuffle/sample members and tests Apr 5, 2026
@ncave ncave requested a review from Copilot April 5, 2026 08:14
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds the missing randomChoice / randomShuffle / randomSample (and *By/*With variants) across Array, List, and Seq for multiple Fable targets, along with cross-target test coverage. Also introduces/updates System.Random implementations (notably Dart + Rust) and updates changelogs accordingly.

Changes:

  • Implement Array/List/Seq random APIs in the TS, Python, Dart, and Rust libraries; add Beam compiler replacements + Erlang implementations.
  • Add test coverage for the new APIs across Rust, Python, JS, Dart, and Beam test suites.
  • Add a Dart System.Random runtime implementation and adjust Dart/Rust random-related plumbing and changelog entries.

Reviewed changes

Copilot reviewed 37 out of 37 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
tests/Rust/tests/src/ArrayTests.fs Adds tests for Array.randomShuffleInPlace*.
tests/Python/TestSeq.fs Adds tests for Seq.random* APIs.
tests/Python/TestList.fs Adds tests for List.random* APIs.
tests/Python/TestArray.fs Adds tests for Array.random* + randomShuffleInPlace*.
tests/Js/Main/SeqTests.fs Adds JS target tests for Seq.random* APIs.
tests/Js/Main/ListTests.fs Adds JS target tests for List.random* APIs.
tests/Js/Main/ArrayTests.fs Adds JS target tests for Array.random* + randomShuffleInPlace*.
tests/Dart/src/SeqTests.fs Adds Dart target tests for Seq.random* APIs.
tests/Dart/src/ListTests.fs Adds Dart target tests for List.random* APIs.
tests/Dart/src/ArrayTests.fs Adds Dart target tests for Array.random* + randomShuffleInPlace*.
tests/Dart/src/ArithmeticTests.fs Enables/adjusts Dart System.Random test strategy to compare seeded streams.
tests/Beam/SeqTests.fs Adds Beam target tests for Seq.random* APIs.
tests/Beam/ListTests.fs Adds Beam target tests for List.random* APIs.
tests/Beam/ArrayTests.fs Adds Beam target tests for Array.random* + randomShuffleInPlace*.
src/Fable.Transforms/Dart/Replacements.fs Adds Dart Array.Contains replacement + updates Dart System.Random ctor handling.
src/Fable.Transforms/Dart/Fable2Dart.fs Maps System.Random to Dart library type reference.
src/Fable.Transforms/Beam/Replacements.fs Adds Beam replacements for Array/List random APIs.
src/Fable.Compiler/CHANGELOG.md Updates changelog entries to reflect “All” random APIs and Dart/Rust Random work.
src/Fable.Cli/CHANGELOG.md Same changelog updates as compiler changelog.
src/fable-library-ts/Seq.fs Implements Seq.random* in TS library via Array helpers.
src/fable-library-ts/List.fs Implements List.random* in TS library via Array helpers.
src/fable-library-ts/Array.fs Implements Array.random* + randomShuffleInPlace* in TS library.
src/fable-library-rust/src/Seq.fs Implements Seq.random* via Array helpers (in-place shuffle for perf).
src/fable-library-rust/src/Random.rs Refactors Rust Random implementation around RngImpl + LrcPtr.
src/fable-library-rust/src/List.fs Refactors List.random* to delegate to Array implementations.
src/fable-library-rust/src/Array.fs Adds randomShuffleInPlace* and aligns empty-source error messages.
src/fable-library-rust/Cargo.toml Updates rand feature flags used by Rust library.
src/fable-library-py/fable_library/Seq.fs Implements Seq.random* in Python library via Array helpers.
src/fable-library-py/fable_library/List.fs Implements List.random* in Python library via Array helpers.
src/fable-library-py/fable_library/Array.fs Implements Array.random* + randomShuffleInPlace* in Python library.
src/fable-library-py/fable_library/array_.py Adds Python runtime implementations for Array.random* functions.
src/fable-library-dart/Seq.fs Implements Seq.random* in Dart library via Array helpers.
src/fable-library-dart/Random.dart Adds Dart runtime System.Random implementation.
src/fable-library-dart/List.fs Implements List.random* in Dart library via Array helpers.
src/fable-library-dart/Fable.Library.Dart.fsproj Includes Random.dart in the Dart library package.
src/fable-library-dart/Array.fs Implements Array.random* + randomShuffleInPlace* in Dart library.
src/fable-library-beam/fable_list.erl Adds Erlang implementations for list-based random APIs used by Beam replacements.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ncave
Copy link
Copy Markdown
Collaborator Author

ncave commented Apr 5, 2026

@dbrattli Do you mind taking a quick look at the Beam and Python changes (only if you have some spare time, of course).

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 38 out of 38 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@dbrattli
Copy link
Copy Markdown
Collaborator

dbrattli commented Apr 5, 2026

@ncave Nice additions. I can review and help out more this evening. Python type error are most likely caused by missing type stubs in array.pyi file (needed for the type checker to know about the new functions in array.rs).

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 39 out of 39 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

dbrattli added a commit that referenced this pull request Apr 5, 2026
…errors

Review follow-ups for #4488:

Python:
- Delete orphaned fable-library-py/fable_library/Array.fs (not in fsproj,
  declared module ArrayModule, never compiled).
- Replace bare PyException("argname") raises in Rust extension with
  PyValueError carrying descriptive messages aligned with dotnet/fsharp
  FSharp.Core resource strings. Randomizer validation now matches .NET's
  dynamic format exactly, including the actual returned value.
- Fix core/array.pyi: change Callable[[], float] -> Callable[[], SupportsFloat]
  for all 15 random *_by signatures. Fable transpiles F# (fun () -> 0.0) as
  returning Float64, which caused 13 pyright errors in test_array.py.

Beam:
- Align randomizer error message with .NET's dynamic format via io_lib:format.
- Use andalso in random_sample_by guard (idiomatic Erlang).
- Refactor random_choices_by: drop lists:map + lists:seq + lists:nth O(n)
  per-pick and use a tail-recursive choices_loop/5 helper with list_to_tuple
  for O(1) indexed access, matching the pattern used by shuffle_loop and
  sample_loop in the same file.

Verification:
- cargo check fable-library-core: clean
- ./build.sh test python: 2233 passed, 0 failed
- pyright on temp/tests/Python/: 0 errors (was 13)
- ./build.sh test beam: 2390 passed, 0 failed

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@dbrattli
Copy link
Copy Markdown
Collaborator

dbrattli commented Apr 5, 2026

@ncave See #27

dbrattli and others added 3 commits April 5, 2026 11:15
…errors (#27)

Review follow-ups for fable-compiler#4488:

Python:
- Delete orphaned fable-library-py/fable_library/Array.fs (not in fsproj,
  declared module ArrayModule, never compiled).
- Replace bare PyException("argname") raises in Rust extension with
  PyValueError carrying descriptive messages aligned with dotnet/fsharp
  FSharp.Core resource strings. Randomizer validation now matches .NET's
  dynamic format exactly, including the actual returned value.
- Fix core/array.pyi: change Callable[[], float] -> Callable[[], SupportsFloat]
  for all 15 random *_by signatures. Fable transpiles F# (fun () -> 0.0) as
  returning Float64, which caused 13 pyright errors in test_array.py.

Beam:
- Align randomizer error message with .NET's dynamic format via io_lib:format.
- Use andalso in random_sample_by guard (idiomatic Erlang).
- Refactor random_choices_by: drop lists:map + lists:seq + lists:nth O(n)
  per-pick and use a tail-recursive choices_loop/5 helper with list_to_tuple
  for O(1) indexed access, matching the pattern used by shuffle_loop and
  sample_loop in the same file.

Verification:
- cargo check fable-library-core: clean
- ./build.sh test python: 2233 passed, 0 failed
- pyright on temp/tests/Python/: 0 errors (was 13)
- ./build.sh test beam: 2390 passed, 0 failed

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@dbrattli
Copy link
Copy Markdown
Collaborator

dbrattli commented Apr 6, 2026

@ncave See #28

dbrattli and others added 2 commits April 5, 2026 21:44
…ate (#28)

Rewrote fable_random.erl to use Erlang's functional rand API (seed_s/uniform_s)
with per-instance state stored in the process dictionary, keyed by make_ref().
This fixes the bug where creating a second Random(seed) instance would reset
the global PRNG state, breaking independent seeded sequences.

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@ncave ncave merged commit 69c15cc into fable-compiler:main Apr 6, 2026
24 checks passed
@ncave ncave deleted the random branch April 6, 2026 05:04
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.

3 participants