Skip to content

Commit b867e53

Browse files
Reimplement Foldable1 in terms of NonEmptyArray, reducing JS code footprint
1 parent 7a8775e commit b867e53

File tree

2 files changed

+6
-30
lines changed

2 files changed

+6
-30
lines changed

src/Data/Array/AtLeast.js

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,3 @@ export const offsetWithImpl = function (offset) {
1717
}
1818
}
1919
}
20-
21-
export const foldr1Impl = function (f) {
22-
return function (xs) {
23-
let len = xs.length;
24-
let acc = xs[len - 1];
25-
for (let i = len - 2; i >= 0; i--) {
26-
acc = f(xs[i])(acc);
27-
}
28-
return acc;
29-
}
30-
}
31-
32-
export const foldl1Impl = function (f) {
33-
return function (xs) {
34-
let acc = xs[0];
35-
let len = xs.length;
36-
for (let i = 1; i < len; i++) {
37-
acc = f(acc)(xs[i]);
38-
}
39-
return acc;
40-
}
41-
}

src/Data/Array/AtLeast.purs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,12 @@ import Data.FastVect.FastVect as FV
7171
import Data.Foldable (class Foldable)
7272
import Data.FoldableWithIndex (class FoldableWithIndex)
7373
import Data.FunctorWithIndex (class FunctorWithIndex)
74-
import Data.Int.AtLeast (IntAL, fromInt', toInt)
7574
import Data.Int.AtLeast (fromLength) as IntAL
75+
import Data.Int.AtLeast (IntAL, fromInt', toInt)
7676
import Data.Maybe (Maybe(Nothing, Just), fromJust)
7777
import Data.Reflectable (class Reflectable, reflectType)
78-
import Data.Semigroup.Foldable (class Foldable1, foldMap1DefaultL)
78+
import Data.Semigroup.Foldable (class Foldable1)
79+
import Data.Semigroup.Foldable as Foldable1
7980
import Data.Traversable (class Traversable)
8081
import Data.TraversableWithIndex (class TraversableWithIndex)
8182
import Partial.Unsafe (unsafePartial)
@@ -107,12 +108,9 @@ derive newtype instance Traversable (ArrayAL n)
107108
derive newtype instance TraversableWithIndex Int (ArrayAL n)
108109

109110
instance Compare n 0 GT => Foldable1 (ArrayAL n) where
110-
foldMap1 = foldMap1DefaultL
111-
foldr1 = foldr1Impl
112-
foldl1 = foldl1Impl
113-
114-
foreign import foldr1Impl :: forall n a. (a -> a -> a) -> ArrayAL n a -> a
115-
foreign import foldl1Impl :: forall n a. (a -> a -> a) -> ArrayAL n a -> a
111+
foldMap1 = Foldable1.foldMap1DefaultL
112+
foldr1 f xs = Foldable1.foldr1 f $ toNonEmptyArray xs
113+
foldl1 f xs = Foldable1.foldl1 f $ toNonEmptyArray xs
116114

117115
instance Apply (ArrayAL n) where
118116
apply (ArrayAL fab) (ArrayAL a) = ArrayAL (Array.zipWith ($) fab a)

0 commit comments

Comments
 (0)