Skip to content

Commit 38b691e

Browse files
srghmapaluh
authored andcommitted
fix: Fix Foldable1 NonEmptyArray instance (requires purescript/purescript-nonempty#39)
1 parent 8ab23b7 commit 38b691e

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
@@ -305,12 +305,22 @@ testNonEmptyArray = do
305305
log "Unfoldable instance"
306306
assert $ U1.range 0 9 == NEA.range 0 9
307307

308-
log "foldl should work"
308+
log "foldMap1 should work"
309+
assert $ foldMap1 Additive (fromArray [1, 2, 3, 4]) == Additive 10
310+
311+
log "fold1 should work"
309312
-- test through sum
310313
assert $ sum (fromArray [1, 2, 3, 4]) == 10
311314

312-
log "foldMap1 should work"
313-
assert $ foldMap1 Additive (fromArray [1, 2, 3, 4]) == Additive 10
315+
log "foldr1 should work"
316+
assert $ foldr1 (\l r -> "(" <> l <> r <> ")") (fromArray ["a", "b", "c", "d"]) == "(((ab)c)d)"
317+
assert $ foldr1 (\l r -> "(" <> l <> r <> ")") (fromArray ["a", "b"]) == "(ab)"
318+
assert $ foldr1 (\l r -> "(" <> l <> r <> ")") (fromArray ["a"]) == "a"
319+
320+
log "foldl1 should work"
321+
assert $ foldl1 (\l r -> "(" <> l <> r <> ")") (fromArray ["a", "b", "c", "d"]) == "(a(b(cd)))"
322+
assert $ foldl1 (\l r -> "(" <> l <> r <> ")") (fromArray ["a", "b"]) == "(ab)"
323+
assert $ foldl1 (\l r -> "(" <> l <> r <> ")") (fromArray ["a"]) == "a"
314324

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

0 commit comments

Comments
 (0)