Skip to content
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

compare and equal #84

Merged
merged 12 commits into from
Jun 11, 2023
Merged

compare and equal #84

merged 12 commits into from
Jun 11, 2023

Conversation

cknitt
Copy link
Member

@cknitt cknitt commented Mar 3, 2023

Add compare and equal functions, see #77.

Modules:

  • Array
  • Int
  • Float
  • String
  • Date
  • Null
  • Nullable

TODO:

  • Docs
  • Tests

@jmagaram
Copy link
Contributor

jmagaram commented Mar 7, 2023

I put some comments in the issue. I think we should discuss there, not here. I see problems using compare with options when sorting arrays - data first is awkward. Same with instantiating a Belt.Set or Map. I'm not sure where data first really works for option since you're going to end up with a number like -1, 0, or 1 and then what to do with it? Also does the subtraction implementation for int.compare work with NaN and other weird cases; it might.

let compareInt = (x, y) => x < y ? -1 : x > y ? 1 : 0
let x = [Some(1), None, Some(5), Some(2)]->Array.sortInPlace((a, b) => Option.cmp(a, b, compareInt))

@cknitt cknitt mentioned this pull request Mar 8, 2023
@zth zth added this to the 0.3.0 milestone Mar 9, 2023
@jmagaram
Copy link
Contributor

jmagaram commented Mar 19, 2023

The implementation fails with overflow like Int.Constants.minValue - Int.Constants.maxValue .

@jmagaram
Copy link
Contributor

Might want to explain in the docs which equality operator is used for floats/ints. if you use === then NaN != NaN and -0.0==+0.0 which seems fine to me. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is

@jmagaram
Copy link
Contributor

I'm confused about the string compare flavors. How does the version you've coded relate to String.localeCompare with or without options? Is there any advantage to it? The docs should explain when to use which ones. I'm wondering if these compare options should be grouped/unified together so the developer can easily see their choices together. Maybe String.compareLocale, String.compareLocaleWith, and String.compare, where the last is the version you've coded and the first two are the String.localeCompare versions with and without options.

https://262.ecma-international.org/7.0/#sec-abstract-relational-comparison

@cknitt cknitt marked this pull request as ready for review June 11, 2023 12:19
@cknitt cknitt merged commit 3a45914 into main Jun 11, 2023
@cknitt cknitt deleted the compare branch June 11, 2023 12:19
jmagaram pushed a commit to jmagaram/rescript-core that referenced this pull request Jul 18, 2023
* eq -> equal, cmp -> compare

* Fix typo

* Add String.equal, String.compare

* Add Int.equal, Int.compare

* Add Float.equal, Float.compare

* Add Date.equal, Date.compare

* Add Array.equal, Array.compare

* Simplify Option.equal, Option.compare

* Add Null.equal, Null.compare

* Add Nullable.equal, Nullable.compare

* Handle Infinity, NaN correctly for Int compare

* Remove xxxU functions
zth added a commit that referenced this pull request Jul 24, 2023
commit 2e24e598affad1f96938215166ab4a3616a74aba
Author: jmagaram <[email protected]>
Date:   Mon Mar 13 13:15:50 2023 -0700

    Result.mapError

    remove extra space before code comment

    shorter docs, add Examples header

    change log

commit 18f521a4698b33124abad3ad044a546730aac6d2
Author: jmagaram <[email protected]>
Date:   Sun Mar 26 22:41:04 2023 -0700

    formatting?

commit e2013e2658891d2c048d348dc1150ef425f51b36
Author: jmagaram <[email protected]>
Date:   Sun Mar 26 21:22:43 2023 -0700

    fixed data type to proper int or float

commit 0ce824e589a89037139f39b53ac552c3894e3a84
Author: jmagaram <[email protected]>
Date:   Sun Mar 26 21:20:14 2023 -0700

    better explanation of floats

commit f2c5039514184aee1572283d4ea9a10bd2db8cd4
Author: jmagaram <[email protected]>
Date:   Sun Mar 26 21:18:42 2023 -0700

    fix type summary text - was bigint

commit 8240c56eed86963a784e0632cfe6996b3fe4606b
Author: jmagaram <[email protected]>
Date:   Sun Mar 26 21:13:11 2023 -0700

    fix wrong type def at top - was bigint for every

commit 819bb5dc70ab0c3cffc5fe181fcfb2635bc208c4
Author: jmagaram <[email protected]>
Date:   Sun Mar 26 21:03:30 2023 -0700

    All specific typed arrays

commit ef7e2c20521128a0fe83b593545c0834be31865e
Author: jmagaram <[email protected]>
Date:   Thu Mar 23 15:13:15 2023 -0700

    Specific instance of typed array

commit 0bbef5b7c68bd22896e255b20d24f689e8004d06
Author: Gabriel Nordeborn <[email protected]>
Date:   Fri Jul 7 13:05:50 2023 +0200

    0.4.0

commit b401b45548b53831e63eb384dc9babb7cb976494
Author: Gabriel Nordeborn <[email protected]>
Date:   Fri Jul 7 12:59:29 2023 +0200

    remove

commit 0e2254cf54e031c5765508ea45529462b8a9214a
Author: glennsl <[email protected]>
Date:   Tue Jul 4 11:38:38 2023 +0200

    perf: compare directly to Ordering.equal instead of using isEqual

commit fc3c3d06a42fdbbdcf82d28a47e780a8406a5ecf
Author: glennsl <[email protected]>
Date:   Mon Jul 3 19:44:18 2023 +0200

    perf: @inline constants

commit f834abde7f243be77c6317f132031252f169cf9b
Author: glennsl <[email protected]>
Date:   Sun Jul 2 14:18:46 2023 +0200

    docs: update changelog

commit 7f2b0b5d7f019aebad1a8b216706fff8f23bf10e
Author: glennsl <[email protected]>
Date:   Sun Jul 2 14:12:30 2023 +0200

    refactor: use Ordering for compare and sort functions

commit 3cb033fcfc70433b373d5b67470671182e4c9d27
Author: glennsl <[email protected]>
Date:   Sun Jul 2 13:53:29 2023 +0200

    feat: add Ordering module

commit 2f0e0c4537a2c96f8a3b15ce3a44bb5f0bc6c38d
Author: glennsl <[email protected]>
Date:   Sun Jul 2 13:44:49 2023 +0200

    fix: use float for ordering to avoid overflow from idiomatic JS code

commit 0f03d9d824fa58017d4ac582aa6b2b24e7a30e3d
Author: Dmitry Zakharov <[email protected]>
Date:   Sat Jul 1 03:24:11 2023 +0400

    Improve Core__Type

commit 2ef0e0ec2c8786f20c8984d1a179004f53da6114
Author: Dmitry Zakharov <[email protected]>
Date:   Fri Jun 30 20:35:22 2023 +0400

    Update changelog

commit c0a3a54350170ca9a233a2ae628dedef4239e5b3
Author: Dmitry Zakharov <[email protected]>
Date:   Fri Jun 30 18:32:00 2023 +0400

    Use Js.Types.symbol for Core__Symbol.t

commit 9175708b3584334e59395e49107a654b6199fad0
Author: Bart Schuurmans <[email protected]>
Date:   Fri Jun 30 17:54:44 2023 +0200

    Fix Math.atan2 argument order

commit e91d3aefde2af857d54d708f70214539d959b288
Author: Christoph Knittel <[email protected]>
Date:   Sun Jun 11 14:19:44 2023 +0200

    compare and equal (#84)

    * eq -> equal, cmp -> compare

    * Fix typo

    * Add String.equal, String.compare

    * Add Int.equal, Int.compare

    * Add Float.equal, Float.compare

    * Add Date.equal, Date.compare

    * Add Array.equal, Array.compare

    * Simplify Option.equal, Option.compare

    * Add Null.equal, Null.compare

    * Add Nullable.equal, Nullable.compare

    * Handle Infinity, NaN correctly for Int compare

    * Remove xxxU functions

commit 9a05f2ec9f0b6f0441bc51f46fb40592eec787de
Author: Christoph Knittel <[email protected]>
Date:   Sun May 28 20:51:42 2023 +0200

    Update CHANGELOG

commit 29a3ff02b543b93f4e2c93eb922fabf08c36379f
Author: Christoph Knittel <[email protected]>
Date:   Sun May 28 20:38:27 2023 +0200

    Map, Set, WeakMap, WeakSet: use types from Js namespace

commit cf54d01ff4a35d781cb75a6f67f56bea096fe6df
Author: Cheng Lou <[email protected]>
Date:   Tue May 23 13:06:37 2023 -0700

    Change idiom from e.g. `shuffle` to `toShuffled` and `shuffleInPlace` to `shuffle` (#142)

    See #139 (comment)

commit 5d65450cd76914aa15c954675f4fa2770e524908
Author: Cheng Lou <[email protected]>
Date:   Tue May 23 02:59:48 2023 -0700

    Use idiomatic new JS array immutable helpers (#139)

    * Use idiomatic new JS array immutable helpers

    Fixes #138

    Added `toSorted`, `toReversed`, `toSpliced` and `with`, per spec.

    Which means moving `sortInPlace` to `sort`. Same for `reverseInPlace` -> `reverse` and `spliceInPlace` -> `splice`.

    This is a small breaking change, thankfully caught by the type system, and by Core typing these mutable helpers' return as `unit`.

    Did the same for `TypedArray`. Though its `reverse` and `sort` _did_ return the array itself, so this breaking change should be carefuly advertized.

    The `toBla-ed` naming convention seems to be the way to go in JS in the future. We should consider that for us as well. Otherwise we'd end up diverging more and more when JS releases more immutable helpers.

commit 49372fe09370120f44f1d6e1fa5e284029e6b2f6
Author: Cheng Lou <[email protected]>
Date:   Mon May 22 03:45:53 2023 -0700

    Upgrade Test stack trace cleaning function

    The stack format change slightly and we were no longer matching on the correct stuff to clean up: "(internal/..." became "(node:internal/..."

    Took the occasion to swap them out with regex and inline the one-off cleanup logic too (IIRC I kept it separate because not all repos wanted that cleanup. But nobody cares now)

commit b8396e33b4732cd96e589e604105d9bdc0e2a44e
Author: Cheng Lou <[email protected]>
Date:   Mon May 22 01:49:08 2023 -0700

    Explicitly list out our source dirs

    Afaik it was still faster due to not needing some kind of directory scan. Might be no longer true, but yeah nothing wrong with this just in case.

commit c1ad827505417ee72f1085633c456dad32c52810
Author: Gabriel Nordeborn <[email protected]>
Date:   Mon May 1 19:04:29 2023 +0200

    0.3.1

commit 81dcfee2f09ba039ea478505c4087f17dd9a87a8
Author: Hyeseong Kim <[email protected]>
Date:   Mon May 1 04:40:31 2023 +0900

    fix peerDeps range

commit d20116fa3e24aba8b998864f3bb1febc3171535e
Author: Gabriel Nordeborn <[email protected]>
Date:   Wed Apr 26 08:58:17 2023 +0200

    0.3.0

commit d656669fbc94cf07d8cf6412ac54018b501b7e8c
Author: Hyeseong Kim <[email protected]>
Date:   Wed Apr 26 15:53:51 2023 +0900

    specify peer's compiler version range (#133)

    * specify peer's compiler version range

    * fix version range

commit 5d982b797d197b487d2f90f5e7be4cf7bb3fae47
Author: Glenn Slotte <[email protected]>
Date:   Sat Apr 15 09:21:25 2023 +0200

    docs(option): getUnsafe does not raise an exception (#131)

    * docs(option): getUnsafe does not raise an exception

    * value -> opt

commit 2256de842cd90035634c3c591d44fef55aeab0d6
Author: Cristiano Calcagno <[email protected]>
Date:   Mon Apr 3 12:44:17 2023 +0200

    Use latest compiler.

commit 6a6685aab8d83c2130b797e0792a12a100f16f11
Author: Glenn Slotte <[email protected]>
Date:   Fri Mar 24 18:37:20 2023 +0100

    chore: add linguist hints for rescript (#121)

commit 40d20e6553e59b3e2a9aa5bc685bf9fb71082686
Author: Justin Magaram <[email protected]>
Date:   Tue Mar 21 14:27:37 2023 -0700

    Result.forEach with tests and docs (#116)

    * Result.forEach

    * Changelog - Result.forEach

commit 5d2f875
Author: jmagaram <[email protected]>
Date:   Sun Jul 16 16:09:13 2023 -0700

    add spacing between method names

commit edb1798
Author: jmagaram <[email protected]>
Date:   Thu Mar 23 11:22:41 2023 -0700

    Object.create docs

commit 1e2e296
Author: jmagaram <[email protected]>
Date:   Thu Mar 23 10:15:55 2023 -0700

    hasOwnProperty docs

commit 16bbe60
Author: jmagaram <[email protected]>
Date:   Thu Mar 23 10:05:37 2023 -0700

    warning on Object.assign

commit 6a59ec2
Author: jmagaram <[email protected]>
Date:   Thu Mar 23 09:42:49 2023 -0700

    safer type signatures {..} as 'a returns 'a

commit 6375876
Author: jmagaram <[email protected]>
Date:   Thu Mar 23 01:11:12 2023 -0700

    Object.set documentation

commit d7601b9
Author: jmagaram <[email protected]>
Date:   Thu Mar 23 00:50:15 2023 -0700

    docs for Object.empty

commit 6539e1b
Author: jmagaram <[email protected]>
Date:   Tue Mar 21 23:38:28 2023 -0700

    changelog

commit 90f2a34
Merge: 8b7f4cb b4b3fda
Author: jmagaram <[email protected]>
Date:   Tue Mar 21 23:28:17 2023 -0700

    Merge branch 'Object.get-documentation' into Object-docs

commit 8b7f4cb
Merge: 8aa1415 a251145
Author: jmagaram <[email protected]>
Date:   Tue Mar 21 23:20:52 2023 -0700

    Merge branch 'Object.assign-documentation' into Object-docs

commit 8aa1415
Merge: ffc0a8f 8856175
Author: jmagaram <[email protected]>
Date:   Tue Mar 21 23:06:46 2023 -0700

    Merge branch 'Object.keysToArray-documentation' into Object-docs

commit ffc0a8f
Merge: 23d7f0e a37c1b8
Author: jmagaram <[email protected]>
Date:   Tue Mar 21 23:06:17 2023 -0700

    Merge branch 'Object.preventExtensions-documentation' into Object-docs

commit 23d7f0e
Merge: c8a7ce5 b37ce6b
Author: jmagaram <[email protected]>
Date:   Tue Mar 21 22:57:05 2023 -0700

    Merge branch 'Object.seal-and-Object.isSealed' into Object-docs

commit c8a7ce5
Merge: 6490abd 319e3b5
Author: jmagaram <[email protected]>
Date:   Tue Mar 21 22:46:52 2023 -0700

    Merge branch 'Object.freeze/isFrozen-documentation' into Object-docs

commit 6490abd
Merge: 49537f2 08db9a8
Author: jmagaram <[email protected]>
Date:   Tue Mar 21 22:46:05 2023 -0700

    Merge branch 'Object.is' into Object-docs

commit 08db9a8
Author: jmagaram <[email protected]>
Date:   Tue Mar 21 13:50:52 2023 -0700

    stricter types on object.is, must be same
    test updates

commit 5dec67f
Author: jmagaram <[email protected]>
Date:   Tue Mar 21 13:46:21 2023 -0700

    stricted comparison types glennsl feedback

commit 3bca678
Author: jmagaram <[email protected]>
Date:   Tue Mar 21 12:45:04 2023 -0700

    remove some of the docs based on Glenn's feedback

commit 2d0fb2c
Author: jmagaram <[email protected]>
Date:   Tue Mar 21 11:11:54 2023 -0700

    remove Specifications section

commit 319e3b5
Author: jmagaram <[email protected]>
Date:   Tue Mar 21 11:06:32 2023 -0700

    remove Specifications section

commit b37ce6b
Author: jmagaram <[email protected]>
Date:   Tue Mar 21 11:02:19 2023 -0700

    remove Specifications section

commit a37c1b8
Author: jmagaram <[email protected]>
Date:   Tue Mar 21 10:56:46 2023 -0700

    remove Specifications section in docs

commit a251145
Author: jmagaram <[email protected]>
Date:   Tue Mar 21 10:35:50 2023 -0700

    remove Specifications section.
    docs for assignMany but no examples.

commit 8856175
Author: jmagaram <[email protected]>
Date:   Tue Mar 21 10:24:56 2023 -0700

    no Specifications header in docs

commit 2b52a04
Author: jmagaram <[email protected]>
Date:   Tue Mar 21 10:05:46 2023 -0700

    change undefined to None

commit b4b3fda
Author: jmagaram <[email protected]>
Date:   Mon Mar 20 12:41:59 2023 -0700

    getSymbol docs and tests

commit 9a67b0c
Author: jmagaram <[email protected]>
Date:   Mon Mar 20 00:11:29 2023 -0700

    Object.assign documentation and tests

commit 14ea9ed
Author: jmagaram <[email protected]>
Date:   Sun Mar 19 20:17:12 2023 -0700

    Object.keysToArray documentation

commit a34df2a
Author: jmagaram <[email protected]>
Date:   Fri Mar 17 12:25:10 2023 -0700

    Object.get documentation and tests

commit bfa541f
Author: jmagaram <[email protected]>
Date:   Fri Mar 17 00:18:56 2023 -0700

    better code examples

commit 258abe3
Author: jmagaram <[email protected]>
Date:   Fri Mar 17 00:11:49 2023 -0700

    better docs and sample code

commit 6d36dbf
Author: jmagaram <[email protected]>
Date:   Thu Mar 16 23:49:53 2023 -0700

    documentation for preventExtensions

commit 8f6db7c
Author: jmagaram <[email protected]>
Date:   Thu Mar 16 22:45:34 2023 -0700

    MDN not Mozilla

commit d60469f
Author: jmagaram <[email protected]>
Date:   Thu Mar 16 22:44:46 2023 -0700

    MDN not Mozilla

commit 5b95915
Author: jmagaram <[email protected]>
Date:   Thu Mar 16 22:42:13 2023 -0700

    seal and isSealed documentation

commit 033d0a6
Author: jmagaram <[email protected]>
Date:   Thu Mar 16 12:11:30 2023 -0700

    docs for object.freeze and object.isfrozen

commit 2741d73
Author: jmagaram <[email protected]>
Date:   Wed Mar 15 12:27:37 2023 -0700

    change recommendation text

commit 5db75f0
Author: jmagaram <[email protected]>
Date:   Tue Mar 14 10:33:31 2023 -0700

    cleaned up a bit

commit 8f1d753
Author: jmagaram <[email protected]>
Date:   Mon Mar 13 22:03:22 2023 -0700

    unnecessary paragraph mark

commit da6197a
Author: jmagaram <[email protected]>
Date:   Mon Mar 13 22:00:07 2023 -0700

    reference to rescript docs, +0/-0, small tweaks

commit 7da0d96
Author: jmagaram <[email protected]>
Date:   Mon Mar 13 20:57:04 2023 -0700

    Object.is documentation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants