Skip to content

Commit 05beff0

Browse files
authored
Merge pull request #183 from andyarvanitis/master
Make Eq and Ord foreign functions less JS-specific
2 parents 0ea05f1 + d84c89f commit 05beff0

File tree

5 files changed

+86
-14
lines changed

5 files changed

+86
-14
lines changed

src/Data/Eq.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
"use strict";
22

3-
exports.refEq = function (r1) {
3+
var refEq = function (r1) {
44
return function (r2) {
55
return r1 === r2;
66
};
77
};
88

9+
exports.eqBooleanImpl = refEq;
10+
exports.eqIntImpl = refEq;
11+
exports.eqNumberImpl = refEq;
12+
exports.eqCharImpl = refEq;
13+
exports.eqStringImpl = refEq;
14+
915
exports.eqArrayImpl = function (f) {
1016
return function (xs) {
1117
return function (ys) {

src/Data/Eq.purs

+11-6
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ notEq x y = (x == y) == false
3838
infix 4 notEq as /=
3939

4040
instance eqBoolean :: Eq Boolean where
41-
eq = refEq
41+
eq = eqBooleanImpl
4242

4343
instance eqInt :: Eq Int where
44-
eq = refEq
44+
eq = eqIntImpl
4545

4646
instance eqNumber :: Eq Number where
47-
eq = refEq
47+
eq = eqNumberImpl
4848

4949
instance eqChar :: Eq Char where
50-
eq = refEq
50+
eq = eqCharImpl
5151

5252
instance eqString :: Eq String where
53-
eq = refEq
53+
eq = eqStringImpl
5454

5555
instance eqUnit :: Eq Unit where
5656
eq _ _ = true
@@ -64,7 +64,12 @@ instance eqArray :: Eq a => Eq (Array a) where
6464
instance eqRec :: (RL.RowToList row list, EqRecord list row) => Eq (Record row) where
6565
eq = eqRecord (RLProxy :: RLProxy list)
6666

67-
foreign import refEq :: forall a. a -> a -> Boolean
67+
foreign import eqBooleanImpl :: Boolean -> Boolean -> Boolean
68+
foreign import eqIntImpl :: Int -> Int -> Boolean
69+
foreign import eqNumberImpl :: Number -> Number -> Boolean
70+
foreign import eqCharImpl :: Char -> Char -> Boolean
71+
foreign import eqStringImpl :: String -> String -> Boolean
72+
6873
foreign import eqArrayImpl :: forall a. (a -> a -> Boolean) -> Array a -> Array a -> Boolean
6974

7075
-- | The `Eq1` type class represents type constructors with decidable equality.

src/Data/Ord.js

+18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
"use strict";
22

3+
var unsafeCompareImpl = function (lt) {
4+
return function (eq) {
5+
return function (gt) {
6+
return function (x) {
7+
return function (y) {
8+
return x < y ? lt : x === y ? eq : gt;
9+
};
10+
};
11+
};
12+
};
13+
};
14+
15+
exports.ordBooleanImpl = unsafeCompareImpl;
16+
exports.ordIntImpl = unsafeCompareImpl;
17+
exports.ordNumberImpl = unsafeCompareImpl;
18+
exports.ordStringImpl = unsafeCompareImpl;
19+
exports.ordCharImpl = unsafeCompareImpl;
20+
321
exports.ordArrayImpl = function (f) {
422
return function (xs) {
523
return function (ys) {

src/Data/Ord.purs

+45-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ module Data.Ord
1717

1818
import Data.Eq (class Eq, class Eq1, class EqRecord, (/=))
1919
import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol)
20-
import Data.Ord.Unsafe (unsafeCompare)
2120
import Data.Ordering (Ordering(..))
2221
import Data.Ring (class Ring, zero, one, negate)
2322
import Data.Unit (Unit)
@@ -39,19 +38,19 @@ class Eq a <= Ord a where
3938
compare :: a -> a -> Ordering
4039

4140
instance ordBoolean :: Ord Boolean where
42-
compare = unsafeCompare
41+
compare = ordBooleanImpl LT EQ GT
4342

4443
instance ordInt :: Ord Int where
45-
compare = unsafeCompare
44+
compare = ordIntImpl LT EQ GT
4645

4746
instance ordNumber :: Ord Number where
48-
compare = unsafeCompare
47+
compare = ordNumberImpl LT EQ GT
4948

5049
instance ordString :: Ord String where
51-
compare = unsafeCompare
50+
compare = ordStringImpl LT EQ GT
5251

5352
instance ordChar :: Ord Char where
54-
compare = unsafeCompare
53+
compare = ordCharImpl LT EQ GT
5554

5655
instance ordUnit :: Ord Unit where
5756
compare _ _ = EQ
@@ -68,6 +67,46 @@ instance ordArray :: Ord a => Ord (Array a) where
6867
LT -> 1
6968
GT -> -1
7069

70+
foreign import ordBooleanImpl
71+
:: Ordering
72+
-> Ordering
73+
-> Ordering
74+
-> Boolean
75+
-> Boolean
76+
-> Ordering
77+
78+
foreign import ordIntImpl
79+
:: Ordering
80+
-> Ordering
81+
-> Ordering
82+
-> Int
83+
-> Int
84+
-> Ordering
85+
86+
foreign import ordNumberImpl
87+
:: Ordering
88+
-> Ordering
89+
-> Ordering
90+
-> Number
91+
-> Number
92+
-> Ordering
93+
94+
foreign import ordStringImpl
95+
:: Ordering
96+
-> Ordering
97+
-> Ordering
98+
-> String
99+
-> String
100+
-> Ordering
101+
102+
foreign import ordCharImpl
103+
:: Ordering
104+
-> Ordering
105+
-> Ordering
106+
-> Char
107+
-> Char
108+
-> Ordering
109+
71110
foreign import ordArrayImpl :: forall a. (a -> a -> Int) -> Array a -> Array a -> Int
72111

73112
instance ordOrdering :: Ord Ordering where

src/Data/Ord/Unsafe.purs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
module Data.Ord.Unsafe (unsafeCompare) where
22

3+
import Prim.TypeError (class Warn, Text)
34
import Data.Ordering (Ordering(..))
45

5-
unsafeCompare :: forall a. a -> a -> Ordering
6+
unsafeCompare
7+
:: forall a.
8+
Warn (Text "'unsafeCompare' is deprecated.")
9+
=> a -> a -> Ordering
610
unsafeCompare = unsafeCompareImpl LT EQ GT
711

812
foreign import unsafeCompareImpl

0 commit comments

Comments
 (0)