git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Array is unfunny now. Add some foldable functions. Removed duplicates.
commit905ec78da5650ae5e998b81049edbbf9f23ac302 authorjoneshf <jones3.hardy@gmail.com> dateMon 10 Mar 2014, 11:21 (13 years ago) parent813d985
3 files changed+44−18
examples/passing/Foldable.purs+13−3 examples/passing/Traversable.purs+2−2 prelude/prelude.purs+29−13
examples/passing/Foldable.purs+13−3
@@ -2,8 +2,18 @@ module Main where
22
33 import Prelude
44 import Data.Foldable
5+ import Data.Monoid
56
6− sum :: [Number] -> Number
7− sum = foldr (+) 0
7+ data Tree a = Node a | Branch (Tree a) (Tree a)
88
9− main = Debug.Trace.print <<< sum $ [1,2,3,4,5]
9+ instance foldableTree :: Foldable Tree where
10+ foldr f z (Node x) = x `f` z
11+ foldr f z (Branch l r) = foldr f (foldr f z r) l
12+
13+ foldl f z (Node x) = z `f` x
14+ foldl f z (Branch l r) = foldl f (foldl f z l) r
15+
16+ foldMap f (Node x) = f x
17+ foldMap f (Branch l r) = foldMap f l <> foldMap f r
18+
19+ main = Debug.Trace.print <<< sum $ Branch (Node 1) (Branch (Node 2) (Node 3))
examples/passing/Traversable.purs+2−2
@@ -1,8 +1,8 @@
11module Main where
22
33 import Prelude
4+ import Data.Array
45 import Data.Maybe
56 import Data.Traversable
6− import Data.Tuple
77
8− main = Debug.Trace.print (sequence (Tuple 3 (Just 4)))
8+ main = Debug.Trace.print (sequence (Just [1,2,3]))
prelude/prelude.purs+29−13
@@ -599,11 +599,6 @@ module Data.Array where
599599 filter p (x:xs) | p x = x : filter p xs
600600 filter p (_:xs) = filter p xs
601601
602− find :: forall a. (a -> Boolean) -> [a] -> Maybe a
603− find _ [] = Nothing
604− find p (x:xs) | p x = Just x
605− find p (_:xs) = find p xs
606−
607602 isEmpty :: forall a. [a] -> Boolean
608603 isEmpty [] = true
609604 isEmpty _ = false
@@ -616,14 +611,6 @@ module Data.Array where
616611 zipWith f (a:as) (b:bs) = f a b : zipWith f as bs
617612 zipWith _ _ _ = []
618613
619− any :: forall a. (a -> Boolean) -> [a] -> Boolean
620− any _ [] = false
621− any p (a:as) = p a || any p as
622−
623− all :: forall a. (a -> Boolean) -> [a] -> Boolean
624− all _ [] = true
625− all p (a:as) = p a && all p as
626−
627614 drop :: forall a. Number -> [a] -> [a]
628615 drop 0 xs = xs
629616 drop _ [] = []
@@ -1295,6 +1282,35 @@ module Data.Foldable where
12951282 mconcat :: forall f m. (Foldable f, Monoid m) => f m -> m
12961283 mconcat = foldl (<>) mempty
12971284
1285+ and :: forall f. (Foldable f) => f Boolean -> Boolean
1286+ and = foldl (&&) true
1287+
1288+ or :: forall f. (Foldable f) => f Boolean -> Boolean
1289+ or = foldl (||) false
1290+
1291+ any :: forall a f. (Foldable f) => (a -> Boolean) -> f a -> Boolean
1292+ any p = or <<< foldMap (\x -> [p x])
1293+
1294+ all :: forall a f. (Foldable f) => (a -> Boolean) -> f a -> Boolean
1295+ all p = and <<< foldMap (\x -> [p x])
1296+
1297+ sum :: forall f. (Foldable f) => f Number -> Number
1298+ sum = foldl (+) 0
1299+
1300+ product :: forall f. (Foldable f) => f Number -> Number
1301+ product = foldl (*) 1
1302+
1303+ elem :: forall a f. (Eq a, Foldable f) => a -> f a -> Boolean
1304+ elem = any <<< (==)
1305+
1306+ notElem :: forall a f. (Eq a, Foldable f) => a -> f a -> Boolean
1307+ notElem x = not <<< elem x
1308+
1309+ find :: forall a f. (Foldable f) => (a -> Boolean) -> f a -> Maybe a
1310+ find p f = case foldMap (\x -> if p x then [x] else []) f of
1311+ (x:_) -> Just x
1312+ [] -> Nothing
1313+
12981314module Data.Traversable where
12991315
13001316 import Prelude