Skip to content

Commit afa99d2

Browse files
committed
Merge pull request #64 from purescript/unsafe-partial
Rename Unsafe to Partial, test on build
2 parents b8e09e8 + f58537e commit afa99d2

File tree

8 files changed

+92
-27
lines changed

8 files changed

+92
-27
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ install:
1313
- npm install
1414
- bower install
1515
script:
16-
- npm run build
16+
- npm test
1717
after_success:
1818
- >-
1919
test $TRAVIS_TAG &&

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"private": true,
33
"scripts": {
44
"clean": "rimraf output && rimraf .pulp-cache",
5-
"build": "jshint src && jscs src && pulp build"
5+
"build": "jshint src && jscs src && pulp build",
6+
"test": "jshint src && jscs src && pulp test"
67
},
78
"devDependencies": {
89
"jscs": "^2.8.0",

src/Data/Array/Partial.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* global exports */
2+
"use strict";
3+
4+
// module Data.Array.Partial
5+
6+
exports.unsafeIndexImpl = function (xs) {
7+
return function (n) {
8+
return xs[n];
9+
};
10+
};

src/Data/Array/Partial.purs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
-- | Partial helper functions for working with immutable arrays.
2+
module Data.Array.Partial
3+
( unsafeIndex
4+
, head
5+
, tail
6+
, last
7+
, init
8+
) where
9+
10+
import Prelude
11+
12+
import Data.Array (length, slice)
13+
14+
-- | Find the element of an array at the specified index.
15+
unsafeIndex :: forall a. Partial => Array a -> Int -> a
16+
unsafeIndex = unsafeIndexImpl
17+
18+
foreign import unsafeIndexImpl :: forall a. Array a -> Int -> a
19+
20+
-- | Get the first element of a non-empty array.
21+
-- |
22+
-- | Running time: `O(1)`.
23+
head :: forall a. Partial => Array a -> a
24+
head xs = unsafeIndex xs 0
25+
26+
-- | Get all but the first element of a non-empty array.
27+
-- |
28+
-- | Running time: `O(n)`, where `n` is the length of the array.
29+
tail :: forall a. Partial => Array a -> Array a
30+
tail xs = slice 1 (length xs) xs
31+
32+
-- | Get the last element of a non-empty array.
33+
-- |
34+
-- | Running time: `O(1)`.
35+
last :: forall a. Partial => Array a -> a
36+
last xs = unsafeIndex xs (length xs - 1)
37+
38+
-- | Get all but the last element of a non-empty array.
39+
-- |
40+
-- | Running time: `O(n)`, where `n` is the length of the array.
41+
init :: forall a. Partial => Array a -> Array a
42+
init xs = slice 0 (length xs - 1) xs

test/Test/Data/Array.purs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,17 @@ import Prelude
44

55
import Control.Monad.Eff (Eff)
66
import Control.Monad.Eff.Console (log, CONSOLE)
7-
import Data.Foldable (for_, foldMapDefaultR, class Foldable, all)
8-
import Test.Assert (assert)
97

108
import Data.Array (range, foldM, unzip, zip, zipWithA, zipWith, intersectBy, intersect, (\\), deleteBy, delete, unionBy, union, nubBy, nub, groupBy, group', group, span, dropWhile, drop, takeWhile, take, sortBy, sort, catMaybes, mapMaybe, filterM, filter, concat, concatMap, reverse, alterAt, modifyAt, updateAt, deleteAt, insertAt, findLastIndex, findIndex, elemLastIndex, elemIndex, (!!), uncons, init, tail, last, head, insertBy, insert, snoc, (:), length, null, replicate, replicateM, singleton, fromFoldable)
9+
import Data.Foldable (for_, foldMapDefaultR, class Foldable, all)
1110
import Data.Maybe (Maybe(..), isNothing, fromJust)
1211
import Data.Tuple (Tuple(..))
1312

1413
import Partial.Unsafe (unsafePartial)
1514

1615
import Test.Assert (assert, ASSERT)
1716

18-
testArray :: forall t.
19-
Eff
20-
( console :: CONSOLE
21-
, assert :: ASSERT
22-
| t
23-
)
24-
Unit
17+
testArray :: forall eff. Eff (console :: CONSOLE, assert :: ASSERT | eff) Unit
2518
testArray = do
2619

2720
log "singleton should construct an array with a single value"

test/Test/Data/Array/Partial.purs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module Test.Data.Array.Partial (testArrayPartial) where
2+
3+
import Prelude
4+
5+
import Control.Monad.Eff (Eff)
6+
import Control.Monad.Eff.Console (log, CONSOLE)
7+
8+
import Data.Array.Partial (init, last, tail, head)
9+
10+
import Partial.Unsafe (unsafePartial)
11+
12+
import Test.Assert (assert, ASSERT)
13+
14+
testArrayPartial :: forall eff. Eff (console :: CONSOLE, assert :: ASSERT | eff) Unit
15+
testArrayPartial = do
16+
17+
log "head should return the first item in an array"
18+
assert $ unsafePartial $ head [1, 2, 3] == 1
19+
assert $ unsafePartial $ head [1] == 1
20+
21+
log "tail should return all but the first item in an array"
22+
assert $ unsafePartial $ tail [1, 2, 3] == [2, 3]
23+
24+
log "last should return the last item of an array"
25+
assert $ unsafePartial $ last [1, 2, 3] == 3
26+
assert $ unsafePartial $ last [1] == 1
27+
28+
log "init should return all but the last item of an array"
29+
assert $ unsafePartial $ init [1, 2, 3] == [1, 2]

test/Test/Data/Array/ST.purs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,7 @@ import Data.Maybe (Maybe(..), isNothing)
1212

1313
import Test.Assert (assert, ASSERT)
1414

15-
testArrayST :: forall t.
16-
Eff
17-
( console :: CONSOLE
18-
, assert :: ASSERT
19-
| t
20-
)
21-
Unit
15+
testArrayST :: forall eff. Eff (console :: CONSOLE, assert :: ASSERT | eff) Unit
2216
testArrayST = do
2317

2418
log "emptySTArray should produce an empty array"

test/Test/Main.purs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
module Test.Main where
22

33
import Prelude (bind, Unit)
4+
45
import Control.Monad.Eff (Eff)
56
import Control.Monad.Eff.Console (CONSOLE)
7+
68
import Test.Assert (ASSERT)
79
import Test.Data.Array (testArray)
10+
import Test.Data.Array.Partial (testArrayPartial)
811
import Test.Data.Array.ST (testArrayST)
9-
import Test.Data.Array.Unsafe (testArrayUnsafe)
1012

11-
main :: forall t.
12-
Eff
13-
( console :: CONSOLE
14-
, assert :: ASSERT
15-
| t
16-
)
17-
Unit
13+
main :: forall eff. Eff (console :: CONSOLE, assert :: ASSERT | eff) Unit
1814
main = do
1915
testArray
2016
testArrayST
21-
testArrayUnsafe
17+
testArrayPartial

0 commit comments

Comments
 (0)