Skip to content

Commit db552ac

Browse files
committed
Merge pull request #1 from joneshf/master
Arrays tail recurisve
2 parents 35497a4 + d44c275 commit db552ac

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/Data/Foldable.purs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@ class Foldable f where
1414
foldMap :: forall a m. (Monoid m) => (a -> m) -> f a -> m
1515

1616
instance foldableArray :: Foldable [] where
17-
foldr _ z [] = z
18-
foldr f z (x:xs) = x `f` (foldr f z xs)
17+
foldr f z xs = foldrArray f z xs
1918

20-
foldl _ z [] = z
21-
foldl f z (x:xs) = foldl f (z `f` x) xs
19+
foldl f z xs = foldlArray f z xs
2220

23-
foldMap _ [] = mempty
24-
foldMap f (x:xs) = f x <> foldMap f xs
21+
foldMap f xs = foldr (\x acc -> f x <> acc) mempty xs
2522

2623
instance foldableEither :: Foldable (Either a) where
2724
foldr _ z (Left _) = z
@@ -100,3 +97,29 @@ find :: forall a f. (Foldable f) => (a -> Boolean) -> f a -> Maybe a
10097
find p f = case foldMap (\x -> if p x then [x] else []) f of
10198
(x:_) -> Just x
10299
[] -> Nothing
100+
101+
foreign import foldrArray
102+
"function foldrArray(f) {\
103+
\ return function(z) {\
104+
\ return function(xs) {\
105+
\ var acc = z;\
106+
\ for (var i = xs.length - 1; i >= 0; --i) {\
107+
\ acc = f(xs[i])(acc);\
108+
\ }\
109+
\ return acc;\
110+
\ }\
111+
\ }\
112+
\}" :: forall a b. (a -> b -> b) -> b -> [a] -> b
113+
114+
foreign import foldlArray
115+
"function foldlArray(f) {\
116+
\ return function(z) {\
117+
\ return function(xs) {\
118+
\ var acc = z;\
119+
\ for (var i = 0, len = xs.length; i < len; ++i) {\
120+
\ acc = f(acc)(xs[i]);\
121+
\ }\
122+
\ return acc;\
123+
\ }\
124+
\ }\
125+
\}" :: forall a b. (b -> a -> b) -> b -> [a] -> b

0 commit comments

Comments
 (0)