Skip to content

Commit 98bb0cf

Browse files
committed
tests and fixes for Foldable1 Traversable1 impls
1 parent 3015dbc commit 98bb0cf

File tree

4 files changed

+63
-14
lines changed

4 files changed

+63
-14
lines changed

src/Data/Array/NonEmpty.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ exports.traverse1Impl = function () {
1616
this.fn = fn;
1717
}
1818

19+
var emptyList = {};
20+
1921
var ConsCell = function (head, tail) {
2022
this.head = head;
2123
this.tail = tail;
2224
};
2325

24-
function FinalCell(x) {
25-
this.val = x;
26-
}
26+
function finalCell(head) {
27+
return new ConsCell(head, emptyList);
28+
};
2729

2830
function consList(x) {
2931
return function (xs) {
@@ -34,11 +36,10 @@ exports.traverse1Impl = function () {
3436
function listToArray(list) {
3537
var arr = [];
3638
var xs = list;
37-
while (xs instanceof ConsCell) {
39+
while (xs !== emptyList) {
3840
arr.push(xs.head);
3941
xs = xs.tail;
4042
}
41-
arr.push(xs.val);
4243
return arr;
4344
}
4445

@@ -55,13 +56,14 @@ exports.traverse1Impl = function () {
5556
} else {
5657
var last = xs[currentLen - 1];
5758
return new Cont(function () {
58-
return go(buildFrom(last, acc), currentLen - 1, xs);
59+
var built = go(buildFrom(last, acc), currentLen - 1, xs);
60+
return built;
5961
});
6062
}
6163
};
6264

6365
return function (array) {
64-
var acc = new FinalCell(array[array.length - 1]);
66+
var acc = map(finalCell)(f(array[array.length - 1]));
6567
var result = go(acc, array.length - 1, array);
6668
while (result instanceof Cont) {
6769
result = result.fn();

src/Data/Array/NonEmpty.purs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ import Control.Alt (class Alt)
9797
import Control.Alternative (class Alternative)
9898
import Control.Lazy (class Lazy)
9999
import Control.Monad.Rec.Class (class MonadRec)
100-
import Data.Array (foldl)
101100
import Data.Array as A
102101
import Data.Bifunctor (bimap)
103102
import Data.Eq (class Eq1)
@@ -231,10 +230,9 @@ range x y = fromArray $ A.range x y
231230

232231
infix 8 range as ..
233232

234-
replicate :: forall a. Int -> a -> Maybe (NonEmptyArray a)
235-
replicate i x
236-
| i > 0 = Just $ NonEmptyArray $ A.replicate i x
237-
| otherwise = Nothing
233+
-- | Replicate an item at least once
234+
replicate :: forall a. Int -> a -> NonEmptyArray a
235+
replicate i x = NonEmptyArray $ A.replicate (max 1 i) x
238236

239237
some
240238
:: forall f a
@@ -497,8 +495,8 @@ foreign import fold1Impl :: forall a. (a -> a -> a) -> NonEmptyArray a -> a
497495

498496
foreign import traverse1Impl
499497
:: forall m a b
500-
. (m (a -> b) -> m a -> m b)
501-
-> ((a -> b) -> m a -> m b)
498+
. (forall a' b'. (m (a -> b) -> m a -> m b))
499+
-> (forall a' b'. (a -> b) -> m a -> m b)
502500
-> (a -> m b)
503501
-> NonEmptyArray a
504502
-> m (NonEmptyArray b)

test/Test/Data/Array/NonEmpty.purs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module Test.Data.Array.NonEmpty (testNonEmptyArray) where
2+
3+
import Prelude
4+
5+
import Control.Monad.Eff (Eff)
6+
import Control.Monad.Eff.Console (CONSOLE, log)
7+
import Data.Array.NonEmpty as NEA
8+
import Data.Foldable (sum)
9+
import Data.Maybe (Maybe(..), fromJust)
10+
import Data.Monoid.Additive (Additive(..))
11+
import Data.Semigroup.Foldable (foldMap1)
12+
import Data.Semigroup.Traversable (traverse1)
13+
import Partial.Unsafe (unsafePartial)
14+
import Test.Assert (ASSERT, assert)
15+
16+
testNonEmptyArray :: forall eff. Eff (console :: CONSOLE, assert :: ASSERT | eff) Unit
17+
testNonEmptyArray = do
18+
log "singleton should construct an array with a single value"
19+
assert $ NEA.toArray (NEA.singleton 1) == [1]
20+
assert $ NEA.toArray (NEA.singleton "foo") == ["foo"]
21+
22+
log "range should create an inclusive array of integers for the specified start and end"
23+
assert $ NEA.toArray (unsafePartial fromJust (NEA.range 0 5))
24+
== [0, 1, 2, 3, 4, 5]
25+
assert $ NEA.toArray (unsafePartial fromJust (NEA.range 2 (-3)))
26+
== [2, 1, 0, -1, -2, -3]
27+
28+
log "replicate should produce an array containg an item a specified number of times"
29+
assert $ NEA.toArray (NEA.replicate 3 true) == [true, true, true]
30+
assert $ NEA.toArray (NEA.replicate 1 "foo") == ["foo"]
31+
assert $ NEA.toArray (NEA.replicate 0 "foo") == ["foo"]
32+
assert $ NEA.toArray (NEA.replicate (-1) "foo") == ["foo"]
33+
34+
log "length should return the number of items in an array"
35+
assert $ NEA.length (NEA.singleton 1) == 1
36+
assert $ NEA.length (unsafePartial fromJust (NEA.fromArray [1, 2, 3, 4, 5]))
37+
== 5
38+
39+
log "foldl should work"
40+
-- test through sum
41+
assert $ sum (unsafePartial fromJust (NEA.fromArray [1, 2, 3, 4])) == 10
42+
43+
log "foldMap1 should work"
44+
assert $ foldMap1 Additive (unsafePartial fromJust (NEA.fromArray [1, 2, 3, 4])) == Additive 10
45+
46+
log "traverse1 should work"
47+
assert $ traverse1 Just (unsafePartial fromJust (NEA.fromArray [1, 2, 3, 4])) == NEA.fromArray [1, 2, 3, 4]

test/Test/Main.purs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import Test.Data.Array (testArray)
1010
import Test.Data.Array.Partial (testArrayPartial)
1111
import Test.Data.Array.ST (testArrayST)
1212
import Test.Data.Array.ST.Partial (testArraySTPartial)
13+
import Test.Data.Array.NonEmpty (testNonEmptyArray)
1314

1415
main :: forall eff. Eff (console :: CONSOLE, assert :: ASSERT | eff) Unit
1516
main = do
1617
testArray
1718
testArrayST
1819
testArrayPartial
1920
testArraySTPartial
21+
testNonEmptyArray

0 commit comments

Comments
 (0)