Skip to content

Commit 1696209

Browse files
committed
Add isEmpty function to Array, Dict, Map, Set with corresponding documentation
1 parent 9b6e506 commit 1696209

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
@@ -43,6 +43,8 @@ let fromInitializer = (~length, f) =>
4343

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

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

runtime/Stdlib_Array.resi

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,24 @@ someArray
8787
@get
8888
external length: array<'a> => int = "length"
8989

90+
/**
91+
`isEmpty(array)` returns `true` if the array is empty (has length 0), `false` otherwise.
92+
93+
## Examples
94+
95+
```rescript
96+
[]->Array.isEmpty->assertEqual(true)
97+
[1, 2, 3]->Array.isEmpty->assertEqual(false)
98+
99+
let emptyArray = []
100+
emptyArray->Array.isEmpty->assertEqual(true)
101+
102+
let nonEmptyArray = ["hello"]
103+
nonEmptyArray->Array.isEmpty->assertEqual(false)
104+
```
105+
*/
106+
let isEmpty: array<'a> => bool
107+
90108
// TODO: Docs
91109
@send external copyAllWithin: (array<'a>, ~target: int) => array<'a> = "copyWithin"
92110

@@ -929,7 +947,7 @@ external mapWithIndex: (array<'a>, ('a, int) => 'b) => array<'b> = "map"
929947
/**
930948
`reduce(xs, init, fn)`
931949
932-
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.
950+
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.
933951
934952
## Examples
935953
@@ -950,7 +968,7 @@ let reduce: (array<'a>, 'b, ('b, 'a) => 'b) => 'b
950968
/**
951969
`reduceWithIndex(x, init, fn)`
952970
953-
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.
971+
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.
954972
955973
## Examples
956974

0 commit comments

Comments
 (0)