@@ -5,7 +5,6 @@ module Prelude
55 , const
66 , asTypeOf
77 , otherwise
8- , (:), cons
98 , Semigroupoid , compose , (<<<), (>>>)
109 , Category , id
1110 , Functor , map , (<$>), (<#>), void
@@ -116,30 +115,6 @@ asTypeOf x _ = x
116115otherwise :: Boolean
117116otherwise = true
118117
119- -- | Attaches an element to the front of an array, creating a new array.
120- -- |
121- -- | ```purescript
122- -- | cons 1 [2, 3, 4] = [1, 2, 3, 4]
123- -- | ```
124- -- |
125- -- | Note, the running time of this function is `O(n)`.
126- foreign import cons
127- " " "
128- function cons(e) {
129- return function(l) {
130- return [e].concat(l);
131- };
132- }
133- " " " :: forall a . a -> [a ] -> [a ]
134-
135- infixr 6 :
136-
137- -- | An infix alias for `cons`.
138- -- |
139- -- | Note, the running time of this function is `O(n)`.
140- (:) :: forall a . a -> [a ] -> [a ]
141- (:) = cons
142-
143118infixr 9 >>>
144119infixr 9 <<<
145120
@@ -199,7 +174,7 @@ class Functor f where
199174instance functorFn :: Functor ((-> ) r ) where
200175 map = compose
201176
202- instance functorArray :: Functor [] where
177+ instance functorArray :: Functor Array where
203178 map = arrayMap
204179
205180foreign import arrayMap
@@ -214,7 +189,7 @@ foreign import arrayMap
214189 return result;
215190 };
216191 }
217- " " " :: forall a b . (a -> b ) -> [ a ] -> [ b ]
192+ " " " :: forall a b . (a -> b ) -> Array a -> Array b
218193
219194(<$>) :: forall f a b . (Functor f ) => (a -> b ) -> f a -> f b
220195(<$>) = map
@@ -272,7 +247,7 @@ class (Functor f) <= Apply f where
272247instance applyFn :: Apply ((-> ) r ) where
273248 apply f g x = f x (g x)
274249
275- instance applyArray :: Apply [] where
250+ instance applyArray :: Apply Array where
276251 apply = ap
277252
278253(<*>) :: forall f a b . (Apply f ) => f (a -> b ) -> f a -> f b
@@ -302,7 +277,7 @@ class (Apply f) <= Applicative f where
302277instance applicativeFn :: Applicative ((-> ) r ) where
303278 pure = const
304279
305- instance applicativeArray :: Applicative [] where
280+ instance applicativeArray :: Applicative Array where
306281 pure x = [x]
307282
308283-- | `return` is an alias for `pure`.
@@ -358,7 +333,7 @@ class (Apply m) <= Bind m where
358333instance bindFn :: Bind ((-> ) r ) where
359334 bind m f x = f (m x) x
360335
361- instance bindArray :: Bind [] where
336+ instance bindArray :: Bind Array where
362337 bind = arrayBind
363338
364339foreign import arrayBind
@@ -372,7 +347,7 @@ foreign import arrayBind
372347 return result;
373348 };
374349 }
375- " " " :: forall a b . [ a ] -> (a -> [ b ] ) -> [ b ]
350+ " " " :: forall a b . Array a -> (a -> Array b ) -> Array b
376351
377352(>>=) :: forall m a b . (Monad m ) => m a -> (a -> m b ) -> m b
378353(>>=) = bind
@@ -391,7 +366,7 @@ class (Applicative m, Bind m) <= Monad m
391366
392367instance monadFn :: Monad ((-> ) r )
393368
394- instance monadArray :: Monad []
369+ instance monadArray :: Monad Array
395370
396371-- | `liftM1` provides a default implementation of `(<$>)` for any
397372-- | [`Monad`](#monad), without using `(<$>)` as provided by the
@@ -462,7 +437,7 @@ instance semigroupOrdering :: Semigroup Ordering where
462437 append GT _ = GT
463438 append EQ y = y
464439
465- instance semigroupArray :: Semigroup [ a ] where
440+ instance semigroupArray :: Semigroup ( Array a ) where
466441 append = concatArray
467442
468443foreign import concatString
@@ -481,7 +456,7 @@ foreign import concatArray
481456 return xs.concat(ys);
482457 };
483458 }
484- " " " :: forall a . [ a ] -> [ a ] -> [ a ]
459+ " " " :: forall a . Array a -> Array a -> Array a
485460
486461infixl 6 +
487462infixl 7 *
@@ -733,7 +708,7 @@ instance eqString :: Eq String where
733708instance eqUnit :: Eq Unit where
734709 eq _ _ = true
735710
736- instance eqArray :: (Eq a ) => Eq [ a ] where
711+ instance eqArray :: (Eq a ) => Eq ( Array a ) where
737712 eq = eqArrayImpl (==)
738713
739714instance eqOrdering :: Eq Ordering where
@@ -773,7 +748,7 @@ foreign import eqArrayImpl
773748 };
774749 };
775750 }
776- " " " :: forall a . (a -> a -> Boolean ) -> [ a ] -> [ a ] -> Boolean
751+ " " " :: forall a . (a -> a -> Boolean ) -> Array a -> Array a -> Boolean
777752
778753-- | The `Ordering` data type represents the three possible outcomes of
779754-- | comparing two values:
@@ -811,13 +786,39 @@ instance ordChar :: Ord Char where
811786instance ordUnit :: Ord Unit where
812787 compare _ _ = EQ
813788
814- instance ordArray :: (Ord a ) => Ord [a ] where
815- compare [] [] = EQ
816- compare [] _ = LT
817- compare _ [] = GT
818- compare (x:xs) (y:ys) = case compare x y of
819- EQ -> compare xs ys
820- other -> other
789+ instance ordArray :: (Ord a ) => Ord (Array a ) where
790+ compare xs ys = compare 0 $ ordArrayImpl (\x y -> case compare x y of
791+ EQ -> 0
792+ LT -> 1
793+ GT -> -1 ) xs ys
794+
795+ foreign import ordArrayImpl " " "
796+ function ordArrayImpl(f) {
797+ return function (xs) {
798+ return function (ys) {
799+ var i = 0;
800+ var xlen = xs.length;
801+ var ylen = ys.length;
802+ while (i < xlen && i < ylen) {
803+ var x = xs[i];
804+ var y = ys[i];
805+ var o = f(x)(y);
806+ if (o !== 0) {
807+ return o;
808+ }
809+ i++;
810+ }
811+ if (xlen == ylen) {
812+ return 0;
813+ } else if (xlen > ylen) {
814+ return -1;
815+ } else {
816+ return 1;
817+ }
818+ };
819+ };
820+ }
821+ " " " :: forall a . (a -> a -> Int ) -> Array a -> Array a -> Int
821822
822823instance ordOrdering :: Ord Ordering where
823824 compare LT LT = EQ
@@ -1053,7 +1054,7 @@ instance showString :: Show String where
10531054instance showUnit :: Show Unit where
10541055 show _ = " unit"
10551056
1056- instance showArray :: (Show a ) => Show [ a ] where
1057+ instance showArray :: (Show a ) => Show ( Array a ) where
10571058 show = showArrayImpl show
10581059
10591060instance showOrdering :: Show Ordering where
@@ -1100,4 +1101,4 @@ foreign import showArrayImpl
11001101 return '[' + ss.join(',') + ']';
11011102 };
11021103 }
1103- " " " :: forall a . (a -> String ) -> [ a ] -> String
1104+ " " " :: forall a . (a -> String ) -> Array a -> String
0 commit comments