Skip to content

Add a few Stdlib helpers #7516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/es6/Stdlib_Array.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ function fromInitializer(length, f) {
return arr;
}

function isEmpty(arr) {
return arr.length === 0;
}

function equal(a, b, eq) {
let len = a.length;
if (len === b.length) {
Expand Down Expand Up @@ -183,6 +187,7 @@ export {
fromInitializer,
equal,
compare,
isEmpty,
indexOfOpt,
lastIndexOfOpt,
reduce,
Expand Down
10 changes: 10 additions & 0 deletions lib/es6/Stdlib_Dict.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ function $$delete$1(dict, string) {
delete(dict[string]);
}

function size(dict) {
return Object.keys(dict).length;
}

function isEmpty(dict) {
return Object.keys(dict).length === 0;
}

function forEach(dict, f) {
Object.values(dict).forEach(value => f(value));
}
Expand All @@ -24,6 +32,8 @@ function mapValues(dict, f) {

export {
$$delete$1 as $$delete,
size,
isEmpty,
forEach,
forEachWithKey,
mapValues,
Expand Down
12 changes: 11 additions & 1 deletion lib/es6/Stdlib_Map.js
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */



function isEmpty(map) {
return map.size === 0;
}

export {
isEmpty,
}
/* No side effect */
12 changes: 11 additions & 1 deletion lib/es6/Stdlib_Set.js
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */



function isEmpty(set) {
return set.size === 0;
}

export {
isEmpty,
}
/* No side effect */
14 changes: 14 additions & 0 deletions lib/es6/Stdlib_String.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,23 @@ function searchOpt(s, re) {

}

function isEmpty(s) {
return s.length === 0;
}

function capitalize(s) {
if (s.length === 0) {
return s;
} else {
return s[0].toUpperCase() + s.slice(1);
}
}

export {
indexOfOpt,
lastIndexOfOpt,
searchOpt,
isEmpty,
capitalize,
}
/* No side effect */
5 changes: 5 additions & 0 deletions lib/js/Stdlib_Array.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ function fromInitializer(length, f) {
return arr;
}

function isEmpty(arr) {
return arr.length === 0;
}

function equal(a, b, eq) {
let len = a.length;
if (len === b.length) {
Expand Down Expand Up @@ -182,6 +186,7 @@ exports.make = make;
exports.fromInitializer = fromInitializer;
exports.equal = equal;
exports.compare = compare;
exports.isEmpty = isEmpty;
exports.indexOfOpt = indexOfOpt;
exports.lastIndexOfOpt = lastIndexOfOpt;
exports.reduce = reduce;
Expand Down
10 changes: 10 additions & 0 deletions lib/js/Stdlib_Dict.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ function $$delete$1(dict, string) {
delete(dict[string]);
}

function size(dict) {
return Object.keys(dict).length;
}

function isEmpty(dict) {
return Object.keys(dict).length === 0;
}

function forEach(dict, f) {
Object.values(dict).forEach(value => f(value));
}
Expand All @@ -23,6 +31,8 @@ function mapValues(dict, f) {
}

exports.$$delete = $$delete$1;
exports.size = size;
exports.isEmpty = isEmpty;
exports.forEach = forEach;
exports.forEachWithKey = forEachWithKey;
exports.mapValues = mapValues;
Expand Down
10 changes: 9 additions & 1 deletion lib/js/Stdlib_Map.js
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */
'use strict';


function isEmpty(map) {
return map.size === 0;
}

exports.isEmpty = isEmpty;
/* No side effect */
10 changes: 9 additions & 1 deletion lib/js/Stdlib_Set.js
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */
'use strict';


function isEmpty(set) {
return set.size === 0;
}

exports.isEmpty = isEmpty;
/* No side effect */
14 changes: 14 additions & 0 deletions lib/js/Stdlib_String.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,21 @@ function searchOpt(s, re) {

}

function isEmpty(s) {
return s.length === 0;
}

function capitalize(s) {
if (s.length === 0) {
return s;
} else {
return s[0].toUpperCase() + s.slice(1);
}
}

exports.indexOfOpt = indexOfOpt;
exports.lastIndexOfOpt = lastIndexOfOpt;
exports.searchOpt = searchOpt;
exports.isEmpty = isEmpty;
exports.capitalize = capitalize;
/* No side effect */
2 changes: 2 additions & 0 deletions runtime/Stdlib_Array.res
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ let fromInitializer = (~length, f) =>

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

let isEmpty = arr => arr->length === 0

let rec equalFromIndex = (a, b, i, eq, len) =>
if i === len {
true
Expand Down
22 changes: 20 additions & 2 deletions runtime/Stdlib_Array.resi
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,24 @@ someArray
@get
external length: array<'a> => int = "length"

/**
`isEmpty(array)` returns `true` if the array is empty (has length 0), `false` otherwise.

## Examples

```rescript
[]->Array.isEmpty->assertEqual(true)
[1, 2, 3]->Array.isEmpty->assertEqual(false)

let emptyArray = []
emptyArray->Array.isEmpty->assertEqual(true)

let nonEmptyArray = ["hello"]
nonEmptyArray->Array.isEmpty->assertEqual(false)
```
*/
let isEmpty: array<'a> => bool

// TODO: Docs
@send external copyAllWithin: (array<'a>, ~target: int) => array<'a> = "copyWithin"

Expand Down Expand Up @@ -929,7 +947,7 @@ external mapWithIndex: (array<'a>, ('a, int) => 'b) => array<'b> = "map"
/**
`reduce(xs, init, fn)`

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.
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.

## Examples

Expand All @@ -950,7 +968,7 @@ let reduce: (array<'a>, 'b, ('b, 'a) => 'b) => 'b
/**
`reduceWithIndex(x, init, fn)`

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.
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.

## Examples

Expand Down
4 changes: 4 additions & 0 deletions runtime/Stdlib_Dict.res
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ let delete = (dict, string) => {

@val external keysToArray: dict<'a> => array<string> = "Object.keys"

let size = dict => dict->keysToArray->Stdlib_Array.length

let isEmpty = dict => dict->size === 0

@val external valuesToArray: dict<'a> => array<'a> = "Object.values"

@val external assign: (dict<'a>, dict<'a>) => dict<'a> = "Object.assign"
Expand Down
38 changes: 38 additions & 0 deletions runtime/Stdlib_Dict.resi
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,44 @@ Console.log(keys) // Logs `["someKey", "someKey2"]` to the console
@val
external keysToArray: dict<'a> => array<string> = "Object.keys"

/**
`size(dictionary)` returns the number of key/value pairs in the dictionary.

## Examples
```rescript
let dict = Dict.make()
dict->Dict.size->assertEqual(0)

dict->Dict.set("someKey", 1)
dict->Dict.set("someKey2", 2)
dict->Dict.size->assertEqual(2)

// After deleting a key
dict->Dict.delete("someKey")
dict->Dict.size->assertEqual(1)
```
*/
let size: dict<'a> => int

/**
`isEmpty(dictionary)` returns `true` if the dictionary is empty (has no key/value pairs), `false` otherwise.

## Examples
```rescript
let emptyDict = Dict.make()
emptyDict->Dict.isEmpty->assertEqual(true)

let dict = Dict.make()
dict->Dict.set("someKey", 1)
dict->Dict.isEmpty->assertEqual(false)

// After clearing all keys
dict->Dict.delete("someKey")
dict->Dict.isEmpty->assertEqual(true)
```
*/
let isEmpty: dict<'a> => bool

/**
`valuesToArray(dictionary)` returns an array of all the values of the dictionary.

Expand Down
2 changes: 2 additions & 0 deletions runtime/Stdlib_Map.res
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ type t<'k, 'v>

@get external size: t<'k, 'v> => int = "size"

let isEmpty = map => map->size === 0

@send external clear: t<'k, 'v> => unit = "clear"

@send external forEach: (t<'k, 'v>, 'v => unit) => unit = "forEach"
Expand Down
19 changes: 19 additions & 0 deletions runtime/Stdlib_Map.resi
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,25 @@ let size = map->Map.size // 1
@get
external size: t<'k, 'v> => int = "size"

/**
`isEmpty(map)` returns `true` if the map has no key/value pairs, `false` otherwise.

## Examples
```rescript
let emptyMap = Map.make()
emptyMap->Map.isEmpty->assertEqual(true)

let map = Map.make()
map->Map.set("someKey", "someValue")
map->Map.isEmpty->assertEqual(false)

// After clearing the map
map->Map.clear
map->Map.isEmpty->assertEqual(true)
```
*/
let isEmpty: t<'k, 'v> => bool

/**
Clears all entries in the map.

Expand Down
2 changes: 2 additions & 0 deletions runtime/Stdlib_Set.res
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ type t<'a>

@get external size: t<'a> => int = "size"

let isEmpty = set => set->size === 0

@send external clear: t<'a> => unit = "clear"

@send external add: (t<'a>, 'a) => unit = "add"
Expand Down
19 changes: 19 additions & 0 deletions runtime/Stdlib_Set.resi
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,25 @@ let size = set->Set.size // 2
@get
external size: t<'a> => int = "size"

/**
`isEmpty(set)` returns `true` if the set has no values, `false` otherwise.

## Examples
```rescript
let emptySet = Set.make()
emptySet->Set.isEmpty->assertEqual(true)

let set = Set.make()
set->Set.add("someValue")
set->Set.isEmpty->assertEqual(false)

// After clearing the set
set->Set.clear
set->Set.isEmpty->assertEqual(true)
```
*/
let isEmpty: t<'a> => bool

/**
Clears all entries in the set.

Expand Down
9 changes: 9 additions & 0 deletions runtime/Stdlib_String.res
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,13 @@ external splitByRegExpAtMost: (string, Stdlib_RegExp.t, ~limit: int) => array<op

@send external localeCompare: (string, string) => float = "localeCompare"

let isEmpty = s => length(s) == 0

let capitalize = s =>
if isEmpty(s) {
s
} else {
toUpperCase(getUnsafe(s, 0)) ++ sliceToEnd(s, ~start=1)
}

external ignore: string => unit = "%ignore"
Loading
Loading