Skip to content

Commit

Permalink
Clean list API (#7290)
Browse files Browse the repository at this point in the history
* remove List.t<'a> from Core and use list<'a> instead

* rename `toShuffled` to `shuffle` and deprecate it

* deprecate List.*Assoc functions towards the use of Map

* update changelog

* fix typo in fromInitializer docstring

* fix typo in docstring

* fix completion tests
  • Loading branch information
tsnobip authored Feb 17, 2025
1 parent 80744a5 commit d1c9aef
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 76 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
# 12.0.0-alpha.9 (Unreleased)

#### :boom: Breaking Change

- Clean list API. https://github.com/rescript-lang/rescript/pull/7290

#### :nail_care: Polish

- Allow single newline in JSX. https://github.com/rescript-lang/rescript/pull/7269
Expand Down
5 changes: 4 additions & 1 deletion lib/es6/Stdlib_List.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ function toArray(x) {
return arr;
}

function toShuffled(xs) {
function shuffle(xs) {
let v = toArray(xs);
Stdlib_Array.shuffle(v);
return fromArray(v);
Expand Down Expand Up @@ -1298,6 +1298,8 @@ function zip(l1, l2) {

let size = length;

let toShuffled = shuffle;

export {
length,
size,
Expand All @@ -1310,6 +1312,7 @@ export {
getExn,
make,
fromInitializer,
shuffle,
toShuffled,
drop,
take,
Expand Down
5 changes: 4 additions & 1 deletion lib/js/Stdlib_List.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ function toArray(x) {
return arr;
}

function toShuffled(xs) {
function shuffle(xs) {
let v = toArray(xs);
Stdlib_Array.shuffle(v);
return fromArray(v);
Expand Down Expand Up @@ -1298,6 +1298,8 @@ function zip(l1, l2) {

let size = length;

let toShuffled = shuffle;

exports.length = length;
exports.size = size;
exports.head = head;
Expand All @@ -1309,6 +1311,7 @@ exports.get = get;
exports.getExn = getExn;
exports.make = make;
exports.fromInitializer = fromInitializer;
exports.shuffle = shuffle;
exports.toShuffled = toShuffled;
exports.drop = drop;
exports.take = take;
Expand Down
17 changes: 11 additions & 6 deletions runtime/Stdlib_List.res
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@

@@config({flags: ["-bs-noassertfalse"]})

type t<'a> = list<'a>

module A = {
@new external makeUninitializedUnsafe: int => array<'a> = "Array"
external min: ('a, 'a) => 'a = "%bs_min"
Expand All @@ -85,7 +83,7 @@ module A = {
}
}

external mutableCell: ('a, t<'a>) => t<'a> = "#makemutablelist"
external mutableCell: ('a, list<'a>) => list<'a> = "#makemutablelist"

/*
`mutableCell x []` == `x`
Expand All @@ -94,7 +92,7 @@ external mutableCell: ('a, t<'a>) => t<'a> = "#makemutablelist"
dont inline a binding to mutable cell, it is mutable
*/
/* INVARIANT: relies on Literals.tl (internal representation) */
@set external unsafeMutateTail: (t<'a>, t<'a>) => unit = "tl"
@set external unsafeMutateTail: (list<'a>, list<'a>) => unit = "tl"

/*
- the cell is not empty
Expand Down Expand Up @@ -480,19 +478,22 @@ let rec fromArrayAux = (a, i, res) =>

let fromArray = a => fromArrayAux(a, Stdlib_Array.length(a) - 1, list{})

let toArray = (x: t<_>) => {
let toArray = (x: list<_>) => {
let len = length(x)
let arr = A.makeUninitializedUnsafe(len)
fillAux(arr, 0, x)
arr
}

let toShuffled = xs => {
let shuffle = xs => {
let v = toArray(xs)
Stdlib_Array.shuffle(v)
fromArray(v)
}

@deprecated("Use `shuffle` instead")
let toShuffled = shuffle

let rec reverseConcat = (l1, l2) =>
switch l1 {
| list{} => l2
Expand Down Expand Up @@ -688,6 +689,7 @@ let rec has = (xs, x, eq) =>
| list{a, ...l} => eq(a, x) || has(l, x, eq)
}

@deprecated("Use a `Map` instead")
let rec getAssoc = (xs, x, eq) =>
switch xs {
| list{} => None
Expand All @@ -699,12 +701,14 @@ let rec getAssoc = (xs, x, eq) =>
}
}

@deprecated("Use a `Map` instead")
let rec hasAssoc = (xs, x, eq) =>
switch xs {
| list{} => false
| list{(a, _), ...l} => eq(a, x) || hasAssoc(l, x, eq)
}

@deprecated("Use a `Map` instead")
let removeAssoc = (xs, x, eq) =>
switch xs {
| list{} => list{}
Expand All @@ -722,6 +726,7 @@ let removeAssoc = (xs, x, eq) =>
}
}

@deprecated("Use a `Map` instead")
let setAssoc = (xs, x, k, eq) =>
switch xs {
| list{} => list{(x, k)}
Expand Down
Loading

0 comments on commit d1c9aef

Please sign in to comment.