Skip to content

Commit 478fb5b

Browse files
committed
fix: Fix Foldable1 NonEmptyArray instance (requires purescript/purescript-nonempty#39)
1 parent e46daf7 commit 478fb5b

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

src/Data/Array/NonEmpty/Internal.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
exports.fold1Impl = function (f) {
3+
exports.foldr1Impl = function (f) {
44
return function (xs) {
55
var acc = xs[0];
66
var len = xs.length;
@@ -11,6 +11,16 @@ exports.fold1Impl = function (f) {
1111
};
1212
};
1313

14+
exports.foldl1Impl = function (f) {
15+
return function (xs) {
16+
var acc = xs[xs.length - 1];
17+
for (var i = xs.length - 2; i >= 0; i--) {
18+
acc = f(xs[i])(acc);
19+
}
20+
return acc;
21+
};
22+
};
23+
1424
exports.traverse1Impl = function () {
1525
function Cont(fn) {
1626
this.fn = fn;

src/Data/Array/NonEmpty/Internal.purs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ derive newtype instance foldableWithIndexNonEmptyArray :: FoldableWithIndex Int
3535

3636
instance foldable1NonEmptyArray :: Foldable1 NonEmptyArray where
3737
foldMap1 = foldMap1Default
38-
fold1 = fold1Impl (<>)
38+
fold1 = foldr1Impl (<>)
39+
foldr1 = foldr1Impl
40+
foldl1 = foldl1Impl
3941

4042
derive newtype instance unfoldable1NonEmptyArray :: Unfoldable1 NonEmptyArray
4143
derive newtype instance traversableNonEmptyArray :: Traversable NonEmptyArray
@@ -56,7 +58,8 @@ derive newtype instance monadNonEmptyArray :: Monad NonEmptyArray
5658
derive newtype instance altNonEmptyArray :: Alt NonEmptyArray
5759

5860
-- we use FFI here to avoid the unncessary copy created by `tail`
59-
foreign import fold1Impl :: forall a. (a -> a -> a) -> NonEmptyArray a -> a
61+
foreign import foldr1Impl :: forall a. (a -> a -> a) -> NonEmptyArray a -> a
62+
foreign import foldl1Impl :: forall a. (a -> a -> a) -> NonEmptyArray a -> a
6063

6164
foreign import traverse1Impl
6265
:: forall m a b

test/Test/Data/Array/NonEmpty.purs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Data.FunctorWithIndex (mapWithIndex)
1010
import Data.Maybe (Maybe(..), fromJust)
1111
import Data.Monoid.Additive (Additive(..))
1212
import Data.NonEmpty ((:|))
13-
import Data.Semigroup.Foldable (foldMap1)
13+
import Data.Semigroup.Foldable (foldMap1, foldr1, foldl1)
1414
import Data.Semigroup.Traversable (traverse1)
1515
import Data.Tuple (Tuple(..))
1616
import Data.Unfoldable1 as U1
@@ -297,12 +297,22 @@ testNonEmptyArray = do
297297
log "Unfoldable instance"
298298
assert $ U1.range 0 9 == NEA.range 0 9
299299

300-
log "foldl should work"
300+
log "foldMap1 should work"
301+
assert $ foldMap1 Additive (fromArray [1, 2, 3, 4]) == Additive 10
302+
303+
log "fold1 should work"
301304
-- test through sum
302305
assert $ sum (fromArray [1, 2, 3, 4]) == 10
303306

304-
log "foldMap1 should work"
305-
assert $ foldMap1 Additive (fromArray [1, 2, 3, 4]) == Additive 10
307+
log "foldr1 should work"
308+
assert $ foldr1 (\l r -> "(" <> l <> r <> ")") (fromArray ["a", "b", "c", "d"]) == "(((ab)c)d)"
309+
assert $ foldr1 (\l r -> "(" <> l <> r <> ")") (fromArray ["a", "b"]) == "(ab)"
310+
assert $ foldr1 (\l r -> "(" <> l <> r <> ")") (fromArray ["a"]) == "a"
311+
312+
log "foldl1 should work"
313+
assert $ foldl1 (\l r -> "(" <> l <> r <> ")") (fromArray ["a", "b", "c", "d"]) == "(a(b(cd)))"
314+
assert $ foldl1 (\l r -> "(" <> l <> r <> ")") (fromArray ["a", "b"]) == "(ab)"
315+
assert $ foldl1 (\l r -> "(" <> l <> r <> ")") (fromArray ["a"]) == "a"
306316

307317
log "traverse1 should work"
308318
assert $ traverse1 Just (fromArray [1, 2, 3, 4]) == NEA.fromArray [1, 2, 3, 4]

0 commit comments

Comments
 (0)