question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

This will be my third stab at implementing this functionality. The basic premise is that my equality comparison methods like startsWith, includes and equal aught to support specifying how to compare the values. Currently they use ===, and they really aught to use Object.is. In the main that is straightforward. It’s really equal that’s the nasty bit.

The problem is this: equal aught to be able to compare things that aren’t iterables. This library already has some functionality that isn’t exactly related to iterables, e.g. the new isObject method. It seems silly to defined equal in such a way that equal(3, 3) should return false or throw an error. There is a correct result, and that is true. But now we have a problem. If equal can compare anything, there will be no foolproof way of knowing whether an argument passed to equal is intended to be a comparator or a value to be compared. What are our options to go about fixing this? As I see them the two basic ways to go are:

  • Keep the comparator as the (potential) first argument but do something clever to distinguish it.
  • Make a factory that takes a comparator and returns an equal method that does nothing magical with its arguments at all.

Each has advantages and disadvantages. The disadvantage of the factory is relatively obvious I think. It won’t look or work quite like our other methods. Either it will be forced currying equal(comparator)(...values) which looks weird when you want the default comparator equal()(...values), or we’ll have to pick a factory name for the factory, e.g. equalFactory. None of it rolls off the tongue.

The disadvantage of currying is that it risks being confusingly magical. We could assume that any call to equal which is given only a single argument is configuring the comparator, e.g. equal(comparator). I think this risks weirdness since our equal method is variadic. It may be used with a spread, and then dynamic changes in the length of the array could cause the shape of the output to change from a boolean to a function. Better I think would be to require the comparator to be tagged, e.g. a function with a Symbol.for('@iter-tools/comparator') property. The consumer wouldn’t have to know about the symbol, they would just write equal(comparator((a, b) => a - b === 0), ...values). I think this is the best solution. I’m going to go ahead and try implementing it.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
conartist6commented, Dec 10, 2020

Oh I should have used the new discussions feature for this. Next time. Good discussing this with you, self!

0reactions
conartist6commented, Dec 10, 2020

I guess for includesSeq there’s no need to go to that trouble as the current curry definition can handle a comparator. That just leaves includes. I don’t think that’s ambiguous now that I think about it. If you get one function before your iterable it’s value. If you get two functions before your iterable they’re comparator, value.

Wow I’m done!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Comparators - Tutorialspoint
A comparator is an electronic circuit, which compares the two inputs that are applied to it and produces an output. The output value...
Read more >
Comparator - Wikipedia
A comparator consists of a specialized high-gain differential amplifier. They are commonly used in devices that measure and digitize analog signals, such as ......
Read more >
Comparator Definition & Meaning - Merriam-Webster
The meaning of COMPARATOR is a device for comparing something with a similar thing or with a standard measure.
Read more >
What Are Comparators? - Circuit Basics
Comparators are devices that compare two voltages or currents and output a digital signal indicating which is larger.
Read more >
Definition of Comparator - Analog Devices
A comparator compares two input voltages and outputs a binary signal indicating which is larger. If the non-inverting (+) input is greater than...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found