Skip to content

Commit d2099af

Browse files
committed
Add isEmpty function to Array, Dict, Map, Set with corresponding documentation
1 parent 2a12328 commit d2099af

16 files changed

+145
-6
lines changed

lib/es6/Stdlib_Array.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ function fromInitializer(length, f) {
2222
return arr;
2323
}
2424

25+
function isEmpty(arr) {
26+
return arr.length === 0;
27+
}
28+
2529
function equal(a, b, eq) {
2630
let len = a.length;
2731
if (len === b.length) {
@@ -183,6 +187,7 @@ export {
183187
fromInitializer,
184188
equal,
185189
compare,
190+
isEmpty,
186191
indexOfOpt,
187192
lastIndexOfOpt,
188193
reduce,

lib/es6/Stdlib_Dict.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ function size(dict) {
99
return Object.keys(dict).length;
1010
}
1111

12+
function isEmpty(dict) {
13+
return Object.keys(dict).length === 0;
14+
}
15+
1216
function forEach(dict, f) {
1317
Object.values(dict).forEach(value => f(value));
1418
}
@@ -29,6 +33,7 @@ function mapValues(dict, f) {
2933
export {
3034
$$delete$1 as $$delete,
3135
size,
36+
isEmpty,
3237
forEach,
3338
forEachWithKey,
3439
mapValues,

lib/es6/Stdlib_Map.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */
1+
2+
3+
4+
function isEmpty(map) {
5+
return map.size === 0;
6+
}
7+
8+
export {
9+
isEmpty,
10+
}
11+
/* No side effect */

lib/es6/Stdlib_Set.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */
1+
2+
3+
4+
function isEmpty(set) {
5+
return set.size === 0;
6+
}
7+
8+
export {
9+
isEmpty,
10+
}
11+
/* No side effect */

lib/js/Stdlib_Array.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ function fromInitializer(length, f) {
2222
return arr;
2323
}
2424

25+
function isEmpty(arr) {
26+
return arr.length === 0;
27+
}
28+
2529
function equal(a, b, eq) {
2630
let len = a.length;
2731
if (len === b.length) {
@@ -182,6 +186,7 @@ exports.make = make;
182186
exports.fromInitializer = fromInitializer;
183187
exports.equal = equal;
184188
exports.compare = compare;
189+
exports.isEmpty = isEmpty;
185190
exports.indexOfOpt = indexOfOpt;
186191
exports.lastIndexOfOpt = lastIndexOfOpt;
187192
exports.reduce = reduce;

lib/js/Stdlib_Dict.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ function size(dict) {
99
return Object.keys(dict).length;
1010
}
1111

12+
function isEmpty(dict) {
13+
return Object.keys(dict).length === 0;
14+
}
15+
1216
function forEach(dict, f) {
1317
Object.values(dict).forEach(value => f(value));
1418
}
@@ -28,6 +32,7 @@ function mapValues(dict, f) {
2832

2933
exports.$$delete = $$delete$1;
3034
exports.size = size;
35+
exports.isEmpty = isEmpty;
3136
exports.forEach = forEach;
3237
exports.forEachWithKey = forEachWithKey;
3338
exports.mapValues = mapValues;

lib/js/Stdlib_Map.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */
1+
'use strict';
2+
3+
4+
function isEmpty(map) {
5+
return map.size === 0;
6+
}
7+
8+
exports.isEmpty = isEmpty;
9+
/* No side effect */

lib/js/Stdlib_Set.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */
1+
'use strict';
2+
3+
4+
function isEmpty(set) {
5+
return set.size === 0;
6+
}
7+
8+
exports.isEmpty = isEmpty;
9+
/* No side effect */

runtime/Stdlib_Array.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ let fromInitializer = (~length, f) =>
4444

4545
@get external length: array<'a> => int = "length"
4646

47+
let isEmpty = arr => arr->length === 0
48+
4749
let rec equalFromIndex = (a, b, i, eq, len) =>
4850
if i === len {
4951
true

runtime/Stdlib_Array.resi

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,24 @@ someArray->Array.length == 2
8080
@get
8181
external length: array<'a> => int = "length"
8282

83+
/**
84+
`isEmpty(array)` returns `true` if the array is empty (has length 0), `false` otherwise.
85+
86+
## Examples
87+
88+
```rescript
89+
[]->Array.isEmpty->assertEqual(true)
90+
[1, 2, 3]->Array.isEmpty->assertEqual(false)
91+
92+
let emptyArray = []
93+
emptyArray->Array.isEmpty->assertEqual(true)
94+
95+
let nonEmptyArray = ["hello"]
96+
nonEmptyArray->Array.isEmpty->assertEqual(false)
97+
```
98+
*/
99+
let isEmpty: array<'a> => bool
100+
83101
// TODO: Docs
84102
@deprecated("Use `copyWithin` instead") @send
85103
external copyAllWithin: (array<'a>, ~target: int) => array<'a> = "copyWithin"
@@ -909,7 +927,7 @@ external mapWithIndex: (array<'a>, ('a, int) => 'b) => array<'b> = "map"
909927
/**
910928
`reduce(xs, init, fn)`
911929
912-
Applies `fn` to each element of `xs` from beginning to end. Function `fn` has two parameters: the item from the list and an accumulator; which starts with a value of `init`. `reduce` returns the final value of the accumulator.
930+
Applies `fn` to each element of `xs` from beginning to end. Function `fn` has two parameters: the item from the list and an "accumulator"; which starts with a value of `init`. `reduce` returns the final value of the accumulator.
913931
914932
## Examples
915933
@@ -928,7 +946,7 @@ let reduce: (array<'a>, 'b, ('b, 'a) => 'b) => 'b
928946
/**
929947
`reduceWithIndex(x, init, fn)`
930948
931-
Applies `fn` to each element of `xs` from beginning to end. Function `fn` has three parameters: the item from the array and an accumulator, which starts with a value of `init` and the index of each element. `reduceWithIndex` returns the final value of the accumulator.
949+
Applies `fn` to each element of `xs` from beginning to end. Function `fn` has three parameters: the item from the array and an "accumulator", which starts with a value of `init` and the index of each element. `reduceWithIndex` returns the final value of the accumulator.
932950
933951
## Examples
934952

0 commit comments

Comments
 (0)