diff --git a/src/Core__Array.mjs b/src/Core__Array.mjs index 74183c35..cbe561c4 100644 --- a/src/Core__Array.mjs +++ b/src/Core__Array.mjs @@ -24,6 +24,50 @@ function fromInitializer(length, f) { return arr; } +function equal(a, b, eq) { + var len = a.length; + if (len === b.length) { + var _i = 0; + while(true) { + var i = _i; + if (i === len) { + return true; + } + if (!Curry._2(eq, a[i], b[i])) { + return false; + } + _i = i + 1 | 0; + continue ; + }; + } else { + return false; + } +} + +function compare(a, b, cmp) { + var lenA = a.length; + var lenB = b.length; + if (lenA > lenB) { + return 1; + } else if (lenA < lenB) { + return -1; + } else { + var _i = 0; + while(true) { + var i = _i; + if (i === lenA) { + return 0; + } + var c = Curry._2(cmp, a[i], b[i]); + if (c !== 0) { + return c; + } + _i = i + 1 | 0; + continue ; + }; + } +} + function indexOfOpt(arr, item) { var index = arr.indexOf(item); if (index !== -1) { @@ -126,6 +170,8 @@ function findMap(arr, f) { export { make , fromInitializer , + equal , + compare , indexOfOpt , lastIndexOfOpt , reduce , diff --git a/src/Core__Array.res b/src/Core__Array.res index e0d6cd5a..5c45b83b 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -37,6 +37,48 @@ let fromInitializer = (~length, f) => @get external length: array<'a> => int = "length" +let rec equalFromIndex = (a, b, i, eq, len) => + if i === len { + true + } else if eq(a->getUnsafe(i), b->getUnsafe(i)) { + equalFromIndex(a, b, i + 1, eq, len) + } else { + false + } + +let equal = (a, b, eq) => { + let len = a->length + if len === b->length { + equalFromIndex(a, b, 0, eq, len) + } else { + false + } +} + +let rec compareFromIndex = (a, b, i, cmp, len) => + if i === len { + 0 + } else { + let c = cmp(a->getUnsafe(i), b->getUnsafe(i)) + if c === 0 { + compareFromIndex(a, b, i + 1, cmp, len) + } else { + c + } + } + +let compare = (a, b, cmp) => { + let lenA = a->length + let lenB = b->length + if lenA > lenB { + 1 + } else if lenA < lenB { + -1 + } else { + compareFromIndex(a, b, 0, cmp, lenA) + } +} + @send external copyAllWithin: (array<'a>, ~target: int) => array<'a> = "copyWithin" @send diff --git a/src/Core__Array.resi b/src/Core__Array.resi index ccbb4e1d..0984fe1e 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -27,6 +27,10 @@ let make: (~length: int, 'a) => array<'a> */ let fromInitializer: (~length: int, int => 'a) => array<'a> +let equal: (array<'a>, array<'a>, ('a, 'a) => bool) => bool + +let compare: (array<'a>, array<'a>, ('a, 'a) => int) => int + @val external isArray: 'a => bool = "Array.isArray" /** diff --git a/src/Core__Date.mjs b/src/Core__Date.mjs index f73ca00a..bb84ed38 100644 --- a/src/Core__Date.mjs +++ b/src/Core__Date.mjs @@ -1,9 +1,20 @@ // Generated by ReScript, PLEASE EDIT WITH CARE +import * as Core__Float from "./Core__Float.mjs"; var UTC = {}; +function equal(a, b) { + return a.getTime() === b.getTime(); +} + +function compare(a, b) { + return Core__Float.compare(a.getTime(), b.getTime()); +} + export { UTC , + equal , + compare , } /* No side effect */ diff --git a/src/Core__Date.res b/src/Core__Date.res index 02f639b9..f3b98fdf 100644 --- a/src/Core__Date.res +++ b/src/Core__Date.res @@ -86,6 +86,10 @@ module UTC = { @send external getTime: t => msSinceEpoch = "getTime" @send external getTimezoneOffset: t => int = "getTimezoneOffset" +let equal = (a, b) => a->getTime === b->getTime + +let compare = (a, b) => Core__Float.compare(a->getTime, b->getTime) + // Locale @send external getFullYear: t => int = "getFullYear" @send external getMonth: t => int = "getMonth" diff --git a/src/Core__Date.resi b/src/Core__Date.resi index b97a16f2..38145e6e 100644 --- a/src/Core__Date.resi +++ b/src/Core__Date.resi @@ -432,6 +432,10 @@ Returns the time, in milliseconds, between UNIX epoch (January 1, 1970 00:00:00 @val external now: unit => msSinceEpoch = "Date.now" +let equal: (t, t) => bool + +let compare: (t, t) => int + /** `getTime(date)` diff --git a/src/Core__Float.mjs b/src/Core__Float.mjs index 2d9f7f0b..88c808e0 100644 --- a/src/Core__Float.mjs +++ b/src/Core__Float.mjs @@ -3,6 +3,20 @@ var Constants = {}; +function equal(a, b) { + return a === b; +} + +function compare(a, b) { + if (a < b) { + return -1; + } else if (a === b) { + return 0; + } else { + return 1; + } +} + function fromString(i) { var i$1 = parseFloat(i); if (isNaN(i$1)) { @@ -14,6 +28,8 @@ function fromString(i) { export { Constants , + equal , + compare , fromString , } /* No side effect */ diff --git a/src/Core__Float.res b/src/Core__Float.res index 8781324d..dd04ddf7 100644 --- a/src/Core__Float.res +++ b/src/Core__Float.res @@ -7,6 +7,17 @@ module Constants = { @val external maxValue: float = "Number.MAX_VALUE" } +let equal = (a: float, b: float) => a === b + +let compare = (a: float, b: float) => + if a < b { + -1 + } else if a === b { + 0 + } else { + 1 + } + @val external isNaN: float => bool = "isNaN" @val external isFinite: float => bool = "isFinite" @val external parseFloat: 'a => float = "parseFloat" diff --git a/src/Core__Float.resi b/src/Core__Float.resi index cb3e835b..5109f8f0 100644 --- a/src/Core__Float.resi +++ b/src/Core__Float.resi @@ -109,6 +109,10 @@ module Constants: { external maxValue: float = "Number.MAX_VALUE" } +let equal: (float, float) => bool + +let compare: (float, float) => int + /** `isNaN(v)` tests if the given `v` is `NaN`. See [`NaN`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN) on MDN. diff --git a/src/Core__Int.mjs b/src/Core__Int.mjs index 1ad4aea3..1b8538ba 100644 --- a/src/Core__Int.mjs +++ b/src/Core__Int.mjs @@ -3,6 +3,20 @@ import * as Pervasives from "rescript/lib/es6/pervasives.js"; import * as Core__Array from "./Core__Array.mjs"; +function equal(a, b) { + return a === b; +} + +function compare(a, b) { + if (a < b) { + return -1; + } else if (a === b) { + return 0; + } else { + return 1; + } +} + function fromString(radix, x) { var maybeInt = radix !== undefined ? parseInt(x, radix) : parseInt(x); if (isNaN(maybeInt) || maybeInt > 2147483647 || maybeInt < -2147483648) { @@ -54,6 +68,8 @@ var Constants = { export { Constants , + equal , + compare , fromString , range , rangeWithOptions , diff --git a/src/Core__Int.res b/src/Core__Int.res index a4f0e42e..e2162ba2 100644 --- a/src/Core__Int.res +++ b/src/Core__Int.res @@ -3,6 +3,17 @@ module Constants = { @inline let maxValue = 2147483647 } +let equal = (a: int, b: int) => a === b + +let compare = (a: int, b: int) => + if a < b { + -1 + } else if a === b { + 0 + } else { + 1 + } + @send external toExponential: int => string = "toExponential" @send external toExponentialWithPrecision: (int, ~digits: int) => string = "toExponential" diff --git a/src/Core__Int.resi b/src/Core__Int.resi index d1a8bcfb..e34dc9ac 100644 --- a/src/Core__Int.resi +++ b/src/Core__Int.resi @@ -56,6 +56,10 @@ module Constants: { let maxValue: int } +let equal: (int, int) => bool + +let compare: (int, int) => int + /** `toExponential(n)` return a `string` representing the given value in exponential notation. diff --git a/src/Core__List.mjs b/src/Core__List.mjs index 88b2cc9d..2f23f3d0 100644 --- a/src/Core__List.mjs +++ b/src/Core__List.mjs @@ -969,7 +969,7 @@ function every2(l1, l2, p) { }; } -function cmpByLength(_l1, _l2) { +function compareLength(_l1, _l2) { while(true) { var l2 = _l2; var l1 = _l1; @@ -989,55 +989,49 @@ function cmpByLength(_l1, _l2) { }; } -function cmp(l1, l2, f) { - var _l1 = l1; - var _l2 = l2; - var p = Curry.__2(f); +function compare(_l1, _l2, p) { while(true) { - var l2$1 = _l2; - var l1$1 = _l1; - if (!l1$1) { - if (l2$1) { + var l2 = _l2; + var l1 = _l1; + if (!l1) { + if (l2) { return -1; } else { return 0; } } - if (!l2$1) { + if (!l2) { return 1; } - var c = p(l1$1.hd, l2$1.hd); + var c = Curry._2(p, l1.hd, l2.hd); if (c !== 0) { return c; } - _l2 = l2$1.tl; - _l1 = l1$1.tl; + _l2 = l2.tl; + _l1 = l1.tl; continue ; }; } -function eq(l1, l2, f) { - var _l1 = l1; - var _l2 = l2; - var p = Curry.__2(f); +function equal(_l1, _l2, p) { while(true) { - var l2$1 = _l2; - var l1$1 = _l1; - if (!l1$1) { - if (l2$1) { + var l2 = _l2; + var l1 = _l1; + if (!l1) { + if (l2) { return false; } else { return true; } } - if (!l2$1) { + if (!l2) { return false; } - if (!p(l1$1.hd, l2$1.hd)) { + if (!Curry._2(p, l1.hd, l2.hd)) { return false; } - _l2 = l2$1.tl; - _l1 = l1$1.tl; + _l2 = l2.tl; + _l1 = l1.tl; continue ; }; } @@ -1386,9 +1380,9 @@ export { some , every2 , some2 , - cmpByLength , - cmp , - eq , + compareLength , + compare , + equal , has , getBy , filter , diff --git a/src/Core__List.res b/src/Core__List.res index 5506f64b..2e720bef 100644 --- a/src/Core__List.res +++ b/src/Core__List.res @@ -680,43 +680,40 @@ let rec every2U = (l1, l2, p) => let every2 = (l1, l2, p) => every2U(l1, l2, (. a, b) => p(a, b)) -let rec cmpByLength = (l1, l2) => +let rec compareLength = (l1, l2) => switch (l1, l2) { | (list{}, list{}) => 0 | (_, list{}) => 1 | (list{}, _) => -1 - | (list{_, ...l1s}, list{_, ...l2s}) => cmpByLength(l1s, l2s) + | (list{_, ...l1s}, list{_, ...l2s}) => compareLength(l1s, l2s) } -let rec cmpU = (l1, l2, p) => +let rec compare = (l1, l2, p) => switch (l1, l2) { | (list{}, list{}) => 0 | (_, list{}) => 1 | (list{}, _) => -1 | (list{a1, ...l1}, list{a2, ...l2}) => - let c = p(. a1, a2) + let c = p(a1, a2) if c == 0 { - cmpU(l1, l2, p) + compare(l1, l2, p) } else { c } } -let cmp = (l1, l2, f) => cmpU(l1, l2, (. x, y) => f(x, y)) - -let rec eqU = (l1, l2, p) => +let rec equal = (l1, l2, p) => switch (l1, l2) { | (list{}, list{}) => true | (_, list{}) | (list{}, _) => false | (list{a1, ...l1}, list{a2, ...l2}) => - if p(. a1, a2) { - eqU(l1, l2, p) + if p(a1, a2) { + equal(l1, l2, p) } else { false } } -let eq = (l1, l2, f) => eqU(l1, l2, (. x, y) => f(x, y)) let rec some2U = (l1, l2, p) => switch (l1, l2) { diff --git a/src/Core__List.resi b/src/Core__List.resi index 5c3a1718..d12659ed 100644 --- a/src/Core__List.resi +++ b/src/Core__List.resi @@ -615,24 +615,24 @@ List.some2(list{0, 1}, list{5, 0}, (a, b) => a > b) // true let some2: (t<'a>, t<'b>, ('a, 'b) => bool) => bool /** -`cmpByLength(list1, list2)` compare two lists solely by length. Returns `-1` if +`compareLength(list1, list2)` compare two lists solely by length. Returns `-1` if `length(list1)` is less than `length(list2)`, `0` if `length(list1)` equals `length(list2)`, and `1` if `length(list1)` is greater than `length(list2)`. ## Examples ```rescript -List.cmpByLength(list{1, 2}, list{3, 4, 5, 6}) // -1 +List.compareLength(list{1, 2}, list{3, 4, 5, 6}) // -1 -List.cmpByLength(list{1, 2, 3}, list{4, 5, 6}) // 0 +List.compareLength(list{1, 2, 3}, list{4, 5, 6}) // 0 -List.cmpByLength(list{1, 2, 3, 4}, list{5, 6}) // 1 +List.compareLength(list{1, 2, 3, 4}, list{5, 6}) // 1 ``` */ -let cmpByLength: (t<'a>, t<'a>) => int +let compareLength: (t<'a>, t<'a>) => int /** -`cmp(list1, list2, f)` compare elements one by one `f`. `f` returns a negative +`compare(list1, list2, f)` compare elements one by one `f`. `f` returns a negative number if `list1` is "less than" `list2`, zero if `list1` is "equal to" `list2`, a positive number if `list1` is "greater than" `list2`. @@ -645,40 +645,40 @@ zero for all `list1` and `list2`. ## Examples ```rescript -List.cmp(list{3}, list{3, 7}, (a, b) => compare(a, b)) /* (-1) */ +List.compare(list{3}, list{3, 7}, (a, b) => compare(a, b)) /* (-1) */ -List.cmp(list{5, 3}, list{5}, (a, b) => compare(a, b)) /* 1 */ +List.compare(list{5, 3}, list{5}, (a, b) => compare(a, b)) /* 1 */ -List.cmp(list{1, 3, 5}, list{1, 4, 2}, (a, b) => compare(a, b)) /* (-1) */ +List.compare(list{1, 3, 5}, list{1, 4, 2}, (a, b) => compare(a, b)) /* (-1) */ -List.cmp(list{1, 3, 5}, list{1, 2, 3}, (a, b) => compare(a, b)) /* 1 */ +List.compare(list{1, 3, 5}, list{1, 2, 3}, (a, b) => compare(a, b)) /* 1 */ -List.cmp(list{1, 3, 5}, list{1, 3, 5}, (a, b) => compare(a, b)) /* 0 */ +List.compare(list{1, 3, 5}, list{1, 3, 5}, (a, b) => compare(a, b)) /* 0 */ ``` **Please note:** The total ordering of List is different from Array, for Array, we compare the length first and, only if the lengths are equal, elements one by one. For lists, we just compare elements one by one. */ -let cmp: (t<'a>, t<'a>, ('a, 'a) => int) => int +let compare: (t<'a>, t<'a>, ('a, 'a) => int) => int /** -`eq(list1, list2, f)` check equality of `list2` and `list2` using `f` for +`equal(list1, list2, f)` check equality of `list2` and `list2` using `f` for equality on elements, where `f` is a function that returns `true` if items `x` and -`y` meet some criterion for equality, `false` otherwise. eq `false` if length +`y` meet some criterion for equality, `false` otherwise. equal `false` if length of `list1` and `list2` are not the same. ## Examples ```rescript -List.eq(list{1, 2, 3}, list{1, 2}, (a, b) => a == b) // false +List.equal(list{1, 2, 3}, list{1, 2}, (a, b) => a == b) // false -List.eq(list{1, 2}, list{1, 2}, (a, b) => a == b) // true +List.equal(list{1, 2}, list{1, 2}, (a, b) => a == b) // true -List.eq(list{1, 2, 3}, list{(-1), (-2), (-3)}, (a, b) => abs(a) == abs(b)) // true +List.equal(list{1, 2, 3}, list{(-1), (-2), (-3)}, (a, b) => abs(a) == abs(b)) // true ``` */ -let eq: (t<'a>, t<'a>, ('a, 'a) => bool) => bool +let equal: (t<'a>, t<'a>, ('a, 'a) => bool) => bool /** `has(list, element, f)` returns `true` if the list contains at least one diff --git a/src/Core__Null.mjs b/src/Core__Null.mjs index 18551f8f..d6c6e83b 100644 --- a/src/Core__Null.mjs +++ b/src/Core__Null.mjs @@ -2,6 +2,7 @@ import * as Curry from "rescript/lib/es6/curry.js"; import * as Caml_option from "rescript/lib/es6/caml_option.js"; +import * as Core__Option from "./Core__Option.mjs"; function fromOption(option) { if (option !== undefined) { @@ -11,6 +12,14 @@ function fromOption(option) { } } +function equal(a, b, eq) { + return Core__Option.equal(a === null ? undefined : Caml_option.some(a), b === null ? undefined : Caml_option.some(b), eq); +} + +function compare(a, b, cmp) { + return Core__Option.compare(a === null ? undefined : Caml_option.some(a), b === null ? undefined : Caml_option.some(b), cmp); +} + function getWithDefault(value, $$default) { if (value !== null) { return value; @@ -55,6 +64,8 @@ function flatMap(value, f) { } export { + equal , + compare , fromOption , getWithDefault , getExn , diff --git a/src/Core__Null.res b/src/Core__Null.res index 3fca7268..023ad78c 100644 --- a/src/Core__Null.res +++ b/src/Core__Null.res @@ -14,6 +14,10 @@ let fromOption: option<'a> => t<'a> = option => | None => null } +let equal = (a, b, eq) => Core__Option.equal(a->toOption, b->toOption, eq) + +let compare = (a, b, cmp) => Core__Option.compare(a->toOption, b->toOption, cmp) + let getWithDefault = (value, default) => switch value->toOption { | Some(x) => x diff --git a/src/Core__Null.resi b/src/Core__Null.resi index f6d2bd51..50b1e79f 100644 --- a/src/Core__Null.resi +++ b/src/Core__Null.resi @@ -44,6 +44,10 @@ let asNullValue = myStr->Null.make // The compiler now thinks this can be `strin */ external make: 'a => t<'a> = "%identity" +let equal: (t<'a>, t<'b>, ('a, 'b) => bool) => bool + +let compare: (t<'a>, t<'b>, ('a, 'b) => int) => int + /** Converts a nullable value into an option, so it can be pattern matched on. Will convert `null` to `None`, and a present value to `Some(value)`. diff --git a/src/Core__Nullable.mjs b/src/Core__Nullable.mjs index 1a7b1ff3..00606cb3 100644 --- a/src/Core__Nullable.mjs +++ b/src/Core__Nullable.mjs @@ -2,6 +2,7 @@ import * as Curry from "rescript/lib/es6/curry.js"; import * as Caml_option from "rescript/lib/es6/caml_option.js"; +import * as Core__Option from "./Core__Option.mjs"; function fromOption(option) { if (option !== undefined) { @@ -10,6 +11,14 @@ function fromOption(option) { } +function equal(a, b, eq) { + return Core__Option.equal((a == null) ? undefined : Caml_option.some(a), (b == null) ? undefined : Caml_option.some(b), eq); +} + +function compare(a, b, cmp) { + return Core__Option.compare((a == null) ? undefined : Caml_option.some(a), (b == null) ? undefined : Caml_option.some(b), cmp); +} + function getWithDefault(value, $$default) { if (value == null) { return $$default; @@ -54,6 +63,8 @@ function flatMap(value, f) { } export { + equal , + compare , fromOption , getWithDefault , getExn , diff --git a/src/Core__Nullable.res b/src/Core__Nullable.res index 0d74399a..51849685 100644 --- a/src/Core__Nullable.res +++ b/src/Core__Nullable.res @@ -14,6 +14,10 @@ let fromOption: option<'a> => t<'a> = option => | None => undefined } +let equal = (a, b, eq) => Core__Option.equal(a->toOption, b->toOption, eq) + +let compare = (a, b, cmp) => Core__Option.compare(a->toOption, b->toOption, cmp) + let getWithDefault = (value, default) => switch value->toOption { | Some(x) => x diff --git a/src/Core__Nullable.resi b/src/Core__Nullable.resi index 8df01762..821554d1 100644 --- a/src/Core__Nullable.resi +++ b/src/Core__Nullable.resi @@ -55,6 +55,10 @@ switch asNullable->Nullable.toOption { */ external make: 'a => t<'a> = "%identity" +let equal: (t<'a>, t<'b>, ('a, 'b) => bool) => bool + +let compare: (t<'a>, t<'b>, ('a, 'b) => int) => int + /** Converts a nullable value into an option, so it can be pattern matched on. Will convert both `null` and `undefined` to `None`, and a present value to `Some(value)`. diff --git a/src/Core__Option.mjs b/src/Core__Option.mjs index 29ad429b..455655f8 100644 --- a/src/Core__Option.mjs +++ b/src/Core__Option.mjs @@ -78,11 +78,10 @@ function isNone(x) { return x === undefined; } -function eq(a, b, f) { - var f$1 = Curry.__2(f); +function equal(a, b, eq) { if (a !== undefined) { if (b !== undefined) { - return f$1(Caml_option.valFromOption(a), Caml_option.valFromOption(b)); + return Curry._2(eq, Caml_option.valFromOption(a), Caml_option.valFromOption(b)); } else { return false; } @@ -91,11 +90,10 @@ function eq(a, b, f) { } } -function cmp(a, b, f) { - var f$1 = Curry.__2(f); +function compare(a, b, cmp) { if (a !== undefined) { if (b !== undefined) { - return f$1(Caml_option.valFromOption(a), Caml_option.valFromOption(b)); + return Curry._2(cmp, Caml_option.valFromOption(a), Caml_option.valFromOption(b)); } else { return 1; } @@ -117,7 +115,7 @@ export { orElse , isSome , isNone , - eq , - cmp , + equal , + compare , } /* No side effect */ diff --git a/src/Core__Option.res b/src/Core__Option.res index 9b688647..64171450 100644 --- a/src/Core__Option.res +++ b/src/Core__Option.res @@ -90,24 +90,17 @@ let isSome = x => let isNone = x => x == None -let eqU = (a, b, f) => - switch a { - | Some(a) => - switch b { - | None => false - | Some(b) => f(. a, b) - } - | None => b == None +let equal = (a, b, eq) => + switch (a, b) { + | (Some(a), Some(b)) => eq(a, b) + | (None, None) => true + | (None, Some(_)) | (Some(_), None) => false } -let eq = (a, b, f) => eqU(a, b, (. x, y) => f(x, y)) - -let cmpU = (a, b, f) => +let compare = (a, b, cmp) => switch (a, b) { - | (Some(a), Some(b)) => f(. a, b) + | (Some(a), Some(b)) => cmp(a, b) | (None, Some(_)) => -1 | (Some(_), None) => 1 | (None, None) => 0 } - -let cmp = (a, b, f) => cmpU(a, b, (. x, y) => f(x, y)) diff --git a/src/Core__Option.resi b/src/Core__Option.resi index fbcf89c1..28cabcfd 100644 --- a/src/Core__Option.resi +++ b/src/Core__Option.resi @@ -198,7 +198,7 @@ Option.isNone(Some(1)) // false let isNone: option<'a> => bool /** -`eq(opt1, opt2, f)` evaluates two optional values for equality with respect to a predicate function `f`. If both `opt1` and `opt2` are `None`, returns `true`. +`equal(opt1, opt2, f)` evaluates two optional values for equality with respect to a predicate function `f`. If both `opt1` and `opt2` are `None`, returns `true`. If one of the arguments is `Some(value)` and the other is `None`, returns `false`. If arguments are `Some(value1)` and `Some(value2)`, returns the result of @@ -211,16 +211,16 @@ let clockEqual = (a, b) => mod(a, 12) == mod(b, 12) open Option -eq(Some(3), Some(15), clockEqual) // true -eq(Some(3), None, clockEqual) // false -eq(None, Some(3), clockEqual) // false -eq(None, None, clockEqual) // true +equal(Some(3), Some(15), clockEqual) // true +equal(Some(3), None, clockEqual) // false +equal(None, Some(3), clockEqual) // false +equal(None, None, clockEqual) // true ``` */ -let eq: (option<'a>, option<'b>, ('a, 'b) => bool) => bool +let equal: (option<'a>, option<'b>, ('a, 'b) => bool) => bool /** -`cmp(opt1, opt2, f)` compares two optional values with respect to given `f`. +`compare(opt1, opt2, f)` compares two optional values with respect to given `f`. If both `opt1` and `opt2` are `None`, it returns `0`. If the first argument is `Some(value1)` and the second is `None`, returns `1` (something is greater than nothing). @@ -239,12 +239,12 @@ let clockCompare = (a, b) => compare(mod(a, 12), mod(b, 12)) open Option -cmp(Some(3), Some(15), clockCompare) // 0 -cmp(Some(3), Some(14), clockCompare) // 1 -cmp(Some(2), Some(15), clockCompare) // (-1) -cmp(None, Some(15), clockCompare) // (-1) -cmp(Some(14), None, clockCompare) // 1 -cmp(None, None, clockCompare) // 0 +compare(Some(3), Some(15), clockCompare) // 0 +compare(Some(3), Some(14), clockCompare) // 1 +compare(Some(2), Some(15), clockCompare) // (-1) +compare(None, Some(15), clockCompare) // (-1) +compare(Some(14), None, clockCompare) // 1 +compare(None, None, clockCompare) // 0 ``` */ -let cmp: (option<'a>, option<'b>, ('a, 'b) => int) => int +let compare: (option<'a>, option<'b>, ('a, 'b) => int) => int diff --git a/src/Core__Result.mjs b/src/Core__Result.mjs index fd9e5ffb..b54c7671 100644 --- a/src/Core__Result.mjs +++ b/src/Core__Result.mjs @@ -72,11 +72,10 @@ function isError(x) { } } -function eq(a, b, f) { - var f$1 = Curry.__2(f); +function equal(a, b, f) { if (a.TAG === /* Ok */0) { if (b.TAG === /* Ok */0) { - return f$1(a._0, b._0); + return Curry._2(f, a._0, b._0); } else { return false; } @@ -87,11 +86,10 @@ function eq(a, b, f) { } } -function cmp(a, b, f) { - var f$1 = Curry.__2(f); +function compare(a, b, f) { if (a.TAG === /* Ok */0) { if (b.TAG === /* Ok */0) { - return f$1(a._0, b._0); + return Curry._2(f, a._0, b._0); } else { return 1; } @@ -117,8 +115,8 @@ export { getWithDefault , isOk , isError , - eq , - cmp , + equal , + compare , forEach , } /* No side effect */ diff --git a/src/Core__Result.res b/src/Core__Result.res index 423ae5a1..681729c8 100644 --- a/src/Core__Result.res +++ b/src/Core__Result.res @@ -72,26 +72,22 @@ let isError = x => | Error(_) => true } -let eqU = (a, b, f) => +let equal = (a, b, f) => switch (a, b) { - | (Ok(a), Ok(b)) => f(. a, b) + | (Ok(a), Ok(b)) => f(a, b) | (Error(_), Ok(_)) | (Ok(_), Error(_)) => false | (Error(_), Error(_)) => true } -let eq = (a, b, f) => eqU(a, b, (. x, y) => f(x, y)) - -let cmpU = (a, b, f) => +let compare = (a, b, f) => switch (a, b) { - | (Ok(a), Ok(b)) => f(. a, b) + | (Ok(a), Ok(b)) => f(a, b) | (Error(_), Ok(_)) => -1 | (Ok(_), Error(_)) => 1 | (Error(_), Error(_)) => 0 } -let cmp = (a, b, f) => cmpU(a, b, (. x, y) => f(x, y)) - let forEach = (r, f) => switch r { | Ok(ok) => f(ok) diff --git a/src/Core__Result.resi b/src/Core__Result.resi index 3f03b7bd..0a6bc37c 100644 --- a/src/Core__Result.resi +++ b/src/Core__Result.resi @@ -134,7 +134,7 @@ let isOk: t<'a, 'b> => bool let isError: t<'a, 'b> => bool /** - `eq(res1, res2, f)`: Determine if two `Result` variables are equal with + `equal(res1, res2, f)`: Determine if two `Result` variables are equal with respect to an equality function. If `res1` and `res2` are of the form `Ok(n)` and `Ok(m)`, return the result of `f(n, m)`. If one of `res1` and `res2` are of the form `Error(e)`, return false If both `res1` and `res2` are of the form @@ -151,19 +151,19 @@ let isError: t<'a, 'b> => bool let mod10equal = (a, b) => mod(a, 10) === mod(b, 10) - Result.eq(good1, good2, mod10equal) == true + Result.equal(good1, good2, mod10equal) == true - Result.eq(good1, bad1, mod10equal) == false + Result.equal(good1, bad1, mod10equal) == false - Result.eq(bad2, good2, mod10equal) == false + Result.equal(bad2, good2, mod10equal) == false - Result.eq(bad1, bad2, mod10equal) == true + Result.equal(bad1, bad2, mod10equal) == true ``` */ -let eq: (t<'a, 'c>, t<'b, 'd>, ('a, 'b) => bool) => bool +let equal: (t<'a, 'c>, t<'b, 'd>, ('a, 'b) => bool) => bool /** - `cmp(res1, res2, f)`: Compare two `Result` variables with respect to a + `compare(res1, res2, f)`: Compare two `Result` variables with respect to a comparison function. The comparison function returns -1 if the first variable is "less than" the second, 0 if the two variables are equal, and 1 if the first is "greater than" the second. @@ -185,18 +185,18 @@ let eq: (t<'a, 'c>, t<'b, 'd>, ('a, 'b) => bool) => bool let mod10cmp = (a, b) => Pervasives.compare(mod(a, 10), mod(b, 10)) - Result.cmp(Ok(39), Ok(57), mod10cmp) == 1 + Result.compare(Ok(39), Ok(57), mod10cmp) == 1 - Result.cmp(Ok(57), Ok(39), mod10cmp) == (-1) + Result.compare(Ok(57), Ok(39), mod10cmp) == (-1) - Result.cmp(Ok(39), Error("y"), mod10cmp) == 1 + Result.compare(Ok(39), Error("y"), mod10cmp) == 1 - Result.cmp(Error("x"), Ok(57), mod10cmp) == (-1) + Result.compare(Error("x"), Ok(57), mod10cmp) == (-1) - Result.cmp(Error("x"), Error("y"), mod10cmp) == 0 + Result.compare(Error("x"), Error("y"), mod10cmp) == 0 ``` */ -let cmp: (t<'a, 'c>, t<'b, 'd>, ('a, 'b) => int) => int +let compare: (t<'a, 'c>, t<'b, 'd>, ('a, 'b) => int) => int /** `forEach(res, f)` runs the provided function `f` on the `Ok` value. If `res` is `Error`, nothing happens. diff --git a/src/Core__String.mjs b/src/Core__String.mjs index 1b8a0b9f..fb79fae4 100644 --- a/src/Core__String.mjs +++ b/src/Core__String.mjs @@ -1,6 +1,20 @@ // Generated by ReScript, PLEASE EDIT WITH CARE +function equal(a, b) { + return a === b; +} + +function compare(a, b) { + if (a === b) { + return 0; + } else if (a > b) { + return 1; + } else { + return -1; + } +} + function indexOfOpt(s, search) { var index = s.indexOf(search); if (index !== -1) { @@ -26,6 +40,8 @@ function searchOpt(s, re) { } export { + equal , + compare , indexOfOpt , lastIndexOfOpt , searchOpt , diff --git a/src/Core__String.res b/src/Core__String.res index dac72645..dee4498a 100644 --- a/src/Core__String.res +++ b/src/Core__String.res @@ -6,6 +6,17 @@ @val external fromCodePoint: int => string = "String.fromCodePoint" @variadic @val external fromCodePointMany: array => string = "String.fromCodePoint" +let equal = (a: string, b: string) => a === b + +let compare = (a: string, b: string) => + if a === b { + 0 + } else if a > b { + 1 + } else { + -1 + } + @get external length: string => int = "length" @get_index external get: (string, int) => option = "" @send external charAt: (string, int) => string = "charAt" diff --git a/src/Core__String.resi b/src/Core__String.resi index 56486294..cb0a5593 100644 --- a/src/Core__String.resi +++ b/src/Core__String.resi @@ -118,6 +118,10 @@ String.fromCodePointMany([0xd55c, 0xae00, 0x1f63a]) == `한글😺` @val external fromCodePointMany: array => string = "String.fromCodePoint" +let equal: (string, string) => bool + +let compare: (string, string) => int + /** `length(str)` returns the length of the given `string`. See [`String.length`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length) on MDN. @@ -961,7 +965,7 @@ external padEnd: (string, int, string) => string = "padEnd" /** `localeCompare(referenceStr, compareStr)` returns a float than indicatings -whether a reference string comes before or fater, or is the same as the given +whether a reference string comes before or after, or is the same as the given string in sort order. If `referenceStr` occurs before `compareStr` positive if the `referenceStr` occurs after `compareStr`, `0` if they are equivalent. Do not rely on exact return values of `-1` or `1`