fantasyland / static-land

Specification for common algebraic structures in JavaScript based on Fantasy Land
MIT License
772 stars 41 forks source link

Ord compare #49

Closed goodmind closed 6 years ago

goodmind commented 6 years ago

what about Ord's compare function?

type Ordering = -1 | 0 | 1
interface Ord<A> extends Setoid<A> {
  compare(a: A, b: A): Ordering
}

In Haskell's hackage about Data.Ord

Minimal complete definition: either compare or <=. Using compare can be more efficient for complex types.

davidchambers commented 6 years ago

We can have sort without the -1/0/1 ugliness required by Array#sort. ;)

goodmind commented 6 years ago

@davidchambers but you can derive compare from lte

davidchambers commented 6 years ago

What's the advantage of compare over lte?

rpominov commented 6 years ago

I think most of the discussion about this happened at https://github.com/fantasyland/fantasy-land/pull/235 . And seems like the summary as David pointed out is that boolean is better as return type than number.

Also, we try to avoid breaking changes as much as possible, so this change is unlikely to happen, and I'm going to close this issue for now.

goodmind commented 6 years ago

Sanctuary also doesn't seem to support static-land

davidchambers commented 6 years ago

Sanctuary also doesn't seem to support static-land

I'm not intimately familiar with Static Land, so please correct me if I am wrong, but my understanding is that the Sanctuary module is a polymorphic Static Land module.

We can create values of type Maybe a or Either a b by using the of function of the S module:

> S.of (S.Maybe) (64)
Just (64)

> S.of (S.Either) (64)
Right (64)

We can interact with these values by using the map function of the S module:

> S.map (Math.sqrt) (S.of (S.Maybe) (64))
Just (8)

> S.map (Math.sqrt) (S.of (S.Either) (64))
Right (8)

If this does not count as Static Land support, I'd love to know what more Sanctuary could provide.

goodmind commented 6 years ago

@davidchambers if I have static land Ord module, how do I use sanctuary sort?

davidchambers commented 6 years ago

Good question, @goodmind! I see the problem now. @rpominov, perhaps you could help with this. How would one change sort :: (Ord a, Applicative m, Foldable m, Monoid (m a)) => m a -> m a to work with Static Land? Must it take TypeRep m and TypeRep a as additional arguments?

-S.sort (['foo', 'bar', 'baz'])
+S.sort (Array) (String) (['foo', 'bar', 'baz'])
rpominov commented 6 years ago

Right, since #45 isn't resolved yet, the only way to implement sort is to pass modules/TypeReps for both types explicitly.

davidchambers commented 6 years ago

Thanks, Roman. #45 will be terrific, in that case. :D