Skip to content

Commit 62660c4

Browse files
committed
Merge pull request #6 from hdgarrood/master
Updates for psc 0.7
2 parents e0005bd + 3440a62 commit 62660c4

File tree

5 files changed

+87
-80
lines changed

5 files changed

+87
-80
lines changed

docs/Prelude.Unsafe.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#### `unsafeIndex`
66

77
``` purescript
8-
unsafeIndex :: forall a. [a] -> Number -> a
8+
unsafeIndex :: forall a. Array a -> Number -> a
99
```
1010

1111
Find the element of an array at the specified index.

docs/Prelude.md

+9-33
Original file line numberDiff line numberDiff line change
@@ -119,30 +119,6 @@ max x y | x >= y = x
119119
| otherwise = y
120120
```
121121

122-
#### `cons`
123-
124-
``` purescript
125-
cons :: forall a. a -> [a] -> [a]
126-
```
127-
128-
Attaches an element to the front of an array, creating a new array.
129-
130-
```purescript
131-
cons 1 [2, 3, 4] = [1, 2, 3, 4]
132-
```
133-
134-
Note, the running time of this function is `O(n)`.
135-
136-
#### `(:)`
137-
138-
``` purescript
139-
(:) :: forall a. a -> [a] -> [a]
140-
```
141-
142-
An infix alias for `cons`.
143-
144-
Note, the running time of this function is `O(n)`.
145-
146122
#### `Semigroupoid`
147123

148124
``` purescript
@@ -234,7 +210,7 @@ instance functorFn :: Functor (Prim.Function r)
234210
#### `functorArray`
235211

236212
``` purescript
237-
instance functorArray :: Functor Prim.Array
213+
instance functorArray :: Functor Array
238214
```
239215

240216

@@ -316,7 +292,7 @@ instance applyFn :: Apply (Prim.Function r)
316292
#### `applyArray`
317293

318294
``` purescript
319-
instance applyArray :: Apply Prim.Array
295+
instance applyArray :: Apply Array
320296
```
321297

322298

@@ -363,7 +339,7 @@ instance applicativeFn :: Applicative (Prim.Function r)
363339
#### `applicativeArray`
364340

365341
``` purescript
366-
instance applicativeArray :: Applicative Prim.Array
342+
instance applicativeArray :: Applicative Array
367343
```
368344

369345

@@ -438,7 +414,7 @@ instance bindFn :: Bind (Prim.Function r)
438414
#### `bindArray`
439415

440416
``` purescript
441-
instance bindArray :: Bind Prim.Array
417+
instance bindArray :: Bind Array
442418
```
443419

444420

@@ -476,7 +452,7 @@ instance monadFn :: Monad (Prim.Function r)
476452
#### `monadArray`
477453

478454
``` purescript
479-
instance monadArray :: Monad Prim.Array
455+
instance monadArray :: Monad Array
480456
```
481457

482458

@@ -579,7 +555,7 @@ instance semigroupOrdering :: Semigroup Ordering
579555
#### `semigroupArray`
580556

581557
``` purescript
582-
instance semigroupArray :: Semigroup [a]
558+
instance semigroupArray :: Semigroup (Array a)
583559
```
584560

585561

@@ -871,7 +847,7 @@ instance eqUnit :: Eq Unit
871847
#### `eqArray`
872848

873849
``` purescript
874-
instance eqArray :: (Eq a) => Eq [a]
850+
instance eqArray :: (Eq a) => Eq (Array a)
875851
```
876852

877853

@@ -958,7 +934,7 @@ instance ordUnit :: Ord Unit
958934
#### `ordArray`
959935

960936
``` purescript
961-
instance ordArray :: (Ord a) => Ord [a]
937+
instance ordArray :: (Ord a) => Ord (Array a)
962938
```
963939

964940

@@ -1273,7 +1249,7 @@ instance showUnit :: Show Unit
12731249
#### `showArray`
12741250

12751251
``` purescript
1276-
instance showArray :: (Show a) => Show [a]
1252+
instance showArray :: (Show a) => Show (Array a)
12771253
```
12781254

12791255

src/Prelude.purs

+46-45
Original file line numberDiff line numberDiff line change
@@ -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
116115
otherwise :: Boolean
117116
otherwise = 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-
143118
infixr 9 >>>
144119
infixr 9 <<<
145120

@@ -199,7 +174,7 @@ class Functor f where
199174
instance functorFn :: Functor ((->) r) where
200175
map = compose
201176

202-
instance functorArray :: Functor [] where
177+
instance functorArray :: Functor Array where
203178
map = arrayMap
204179

205180
foreign 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
272247
instance 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
302277
instance 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
358333
instance 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

364339
foreign 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

392367
instance 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

468443
foreign 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

486461
infixl 6 +
487462
infixl 7 *
@@ -733,7 +708,7 @@ instance eqString :: Eq String where
733708
instance 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

739714
instance 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
811786
instance 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

822823
instance ordOrdering :: Ord Ordering where
823824
compare LT LT = EQ
@@ -1053,7 +1054,7 @@ instance showString :: Show String where
10531054
instance 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

10591060
instance 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

src/Prelude/Unsafe.purs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ foreign import unsafeIndex
1010
return xs[n];
1111
};
1212
}
13-
""" :: forall a. [a] -> Number -> a
13+
""" :: forall a. Array a -> Number -> a

test/Main.purs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
module Main where
3+
4+
import Console (log)
5+
import Control.Monad.Eff
6+
7+
foreign import throwError
8+
"""
9+
function throwError(msg) {
10+
throw new Error(msg)
11+
}
12+
""" :: forall e. String -> Eff e Unit
13+
14+
check :: forall e a. (Show a, Ord a) => Ordering -> a -> a -> Eff e Unit
15+
check expected a b =
16+
let actual = compare a b
17+
in if actual == expected
18+
then return unit
19+
else throwError (show a <> " `compare` " <> show b <> ": " <>
20+
"expected " <> show expected <> ", got " <> show actual)
21+
22+
main = do
23+
check EQ [] ([] :: Array Number)
24+
check LT [] [0]
25+
check GT [0] []
26+
check EQ [1,2,3] [1,2,3]
27+
check LT [1,1,3] [1,2,3]
28+
check GT [1,3,3] [1,2,3]
29+
30+
log "All good!"

0 commit comments

Comments
 (0)