git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #274 from joneshf/master

Added Data.Foldable and Data.Traversable to Prelude.

commitf04ca5449cc9d7a5c2eee9998cf222e63d54c57a authorPhil Freeman <paf31@cantab.net> dateMon 10 Mar 2014, 13:51 (13 years ago) parent5a1cbdc905ec78
3 files changed+206−44
examples/passing/Foldable.purs+19 examples/passing/Traversable.purs+8 prelude/prelude.purs+179−44
examples/passing/Foldable.pursnew file+19
@@ -0,0 +1,19 @@
1+module Main where
2+
3+ import Prelude
4+ import Data.Foldable
5+ import Data.Monoid
6+
7+ data Tree a = Node a | Branch (Tree a) (Tree a)
8+
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.pursnew file+8
@@ -0,0 +1,8 @@
1+module Main where
2+
3+ import Prelude
4+ import Data.Array
5+ import Data.Maybe
6+ import Data.Traversable
7+
8+ main = Debug.Trace.print (sequence (Just [1,2,3]))
prelude/prelude.purs+179−44
@@ -323,7 +323,6 @@ module Prelude where
323323module Data.Monoid where
324324
325325 import Prelude
326− import Data.Array (foldl)
327326
328327 infixr 6 <>
329328
@@ -339,13 +338,30 @@ module Data.Monoid where
339338 mempty = []
340339 (<>) = Data.Array.concat
341340
342− mconcat :: forall m. (Monoid m) => [m] -> m
343− mconcat = foldl (<>) mempty
341+module Control.Applicative where
342+
343+ import Prelude
344+
345+ infixl 4 <*
346+ infixl 4 *>
347+
348+ (<*) :: forall a b f. (Applicative f) => f a -> f b -> f a
349+ (<*) x y = const <$> x <*> y
350+
351+ (*>) :: forall a b f. (Applicative f) => f a -> f b -> f b
352+ (*>) x y = const id <$> x <*> y
353+
354+ lift2 :: forall a b c f. (Applicative f) => (a -> b -> c) -> f a -> f b -> f c
355+ lift2 f x y = f <$> x <*> y
356+
357+ lift3 :: forall a b c d f. (Applicative f) => (a -> b -> c -> d) -> f a -> f b -> f c -> f d
358+ lift3 f x y z = f <$> x <*> y <*> z
344359
345360module Control.Monad where
346361
347362 import Prelude
348363 import Data.Array
364+ import Data.Traversable
349365
350366 replicateM :: forall m a. (Monad m) => Number -> m a -> m [a]
351367 replicateM 0 _ = return []
@@ -354,13 +370,6 @@ module Control.Monad where
354370 as <- replicateM (n - 1) m
355371 return (a : as)
356372
357− mapM :: forall m a b. (Monad m) => (a -> m b) -> [a] -> m [b]
358− mapM _ [] = return []
359− mapM f (a:as) = do
360− b <- f a
361− bs <- mapM f as
362− return (b : bs)
363−
364373 infixr 1 >=>
365374 infixr 1 <=<
366375
@@ -372,13 +381,6 @@ module Control.Monad where
372381 (<=<) :: forall m a b c. (Monad m) => (b -> m c) -> (a -> m b) -> a -> m c
373382 (<=<) = flip (>=>)
374383
375− sequence :: forall m a. (Monad m) => [m a] -> m [a]
376− sequence [] = return []
377− sequence (m:ms) = do
378− a <- m
379− as <- sequence ms
380− return (a : as)
381−
382384 join :: forall m a. (Monad m) => m (m a) -> m a
383385 join mm = do
384386 m <- mm
@@ -392,8 +394,8 @@ module Control.Monad where
392394 when true m = m
393395 when false _ = return {}
394396
395− zipWithM :: forall m a b c. (Monad m) => (a -> b -> m c) -> [a] -> [b] -> m [c]
396− zipWithM f xs ys = sequence $ zipWith f xs ys
397+ zipWithA :: forall m a b c. (Applicative m) => (a -> b -> m c) -> [a] -> [b] -> m [c]
398+ zipWithA f xs ys = sequence (zipWith f xs ys)
397399
398400module Data.Maybe where
399401
@@ -481,14 +483,6 @@ module Data.Array where
481483 map _ [] = []
482484 map f (x:xs) = f x : map f xs
483485
484− foldr :: forall a b. (a -> b -> a) -> a -> [b] -> a
485− foldr f a (b : bs) = f (foldr f a bs) b
486− foldr _ a [] = a
487−
488− foldl :: forall a b. (b -> a -> b) -> b -> [a] -> b
489− foldl _ b [] = b
490− foldl f b (a:as) = foldl f (f b a) as
491−
492486 foreign import length "function length(xs) {\
493487 \ return xs.length;\
494488 \}" :: forall a. [a] -> Number
@@ -605,11 +599,6 @@ module Data.Array where
605599 filter p (x:xs) | p x = x : filter p xs
606600 filter p (_:xs) = filter p xs
607601
608− find :: forall a. (a -> Boolean) -> [a] -> Maybe a
609− find _ [] = Nothing
610− find p (x:xs) | p x = Just x
611− find p (_:xs) = find p xs
612−
613602 isEmpty :: forall a. [a] -> Boolean
614603 isEmpty [] = true
615604 isEmpty _ = false
@@ -622,14 +611,6 @@ module Data.Array where
622611 zipWith f (a:as) (b:bs) = f a b : zipWith f as bs
623612 zipWith _ _ _ = []
624613
625− any :: forall a. (a -> Boolean) -> [a] -> Boolean
626− any _ [] = false
627− any p (a:as) = p a || any p as
628−
629− all :: forall a. (a -> Boolean) -> [a] -> Boolean
630− all _ [] = true
631− all p (a:as) = p a && all p as
632−
633614 drop :: forall a. Number -> [a] -> [a]
634615 drop 0 xs = xs
635616 drop _ [] = []
@@ -710,6 +691,9 @@ module Data.Tuple where
710691 (==) (Tuple a1 b1) (Tuple a2 b2) = a1 == a2 && b1 == b2
711692 (/=) t1 t2 = not (t1 == t2)
712693
694+ instance functorTuple :: Functor (Tuple a) where
695+ (<$>) f (Tuple x y) = Tuple x (f y)
696+
713697module Data.String where
714698
715699 foreign import lengthS "function lengthS(s) {\
@@ -909,19 +893,19 @@ module Math where
909893 \}" :: Number -> Number
910894
911895 foreign import max "function max(n1){\
912− \ return function(n2) {\
896+ \ return function(n2) {\
913897 \ return Math.max(n1, n2);\
914898 \ }\
915899 \}" :: Number -> Number -> Number
916900
917901 foreign import min "function min(n1){\
918− \ return function(n2) {\
902+ \ return function(n2) {\
919903 \ return Math.min(n1, n2);\
920904 \ }\
921905 \}" :: Number -> Number -> Number
922906
923907 foreign import pow "function pow(n){\
924− \ return function(p) {\
908+ \ return function(p) {\
925909 \ return Math.pow(n, p);\
926910 \ }\
927911 \}" :: Number -> Number -> Number
@@ -1206,7 +1190,7 @@ module Data.Enum where
12061190 class Enum a where
12071191 toEnum :: Number -> Maybe a
12081192 fromEnum :: a -> Number
1209−
1193+
12101194module Text.Parsing.Read where
12111195
12121196 class Read a where
@@ -1223,3 +1207,154 @@ module Text.Parsing.Read where
12231207
12241208 instance readNumber :: Read Number where
12251209 read = readNumberImpl
1210+
1211+module Data.Foldable where
1212+
1213+ import Prelude
1214+ import Control.Applicative
1215+ import Data.Either
1216+ import Data.Eq
1217+ import Data.Maybe
1218+ import Data.Monoid
1219+ import Data.Tuple
1220+
1221+ class Foldable f where
1222+ foldr :: forall a b. (a -> b -> b) -> b -> f a -> b
1223+ foldl :: forall a b. (b -> a -> b) -> b -> f a -> b
1224+ foldMap :: forall a m. (Monoid m) => (a -> m) -> f a -> m
1225+
1226+ instance foldableArray :: Foldable [] where
1227+ foldr _ z [] = z
1228+ foldr f z (x:xs) = x `f` (foldr f z xs)
1229+
1230+ foldl _ z [] = z
1231+ foldl f z (x:xs) = foldl f (z `f` x) xs
1232+
1233+ foldMap _ [] = mempty
1234+ foldMap f (x:xs) = f x <> foldMap f xs
1235+
1236+ instance foldableEither :: Foldable (Either a) where
1237+ foldr _ z (Left _) = z
1238+ foldr f z (Right x) = x `f` z
1239+
1240+ foldl _ z (Left _) = z
1241+ foldl f z (Right x) = z `f` x
1242+
1243+ foldMap f (Left _) = mempty
1244+ foldMap f (Right x) = f x
1245+
1246+ instance foldableMaybe :: Foldable Maybe where
1247+ foldr _ z Nothing = z
1248+ foldr f z (Just x) = x `f` z
1249+
1250+ foldl _ z Nothing = z
1251+ foldl f z (Just x) = z `f` x
1252+
1253+ foldMap f Nothing = mempty
1254+ foldMap f (Just x) = f x
1255+
1256+ instance foldableRef :: Foldable Ref where
1257+ foldr f z (Ref x) = x `f` z
1258+
1259+ foldl f z (Ref x) = z `f` x
1260+
1261+ foldMap f (Ref x) = f x
1262+
1263+ instance foldableTuple :: Foldable (Tuple a) where
1264+ foldr f z (Tuple _ x) = x `f` z
1265+
1266+ foldl f z (Tuple _ x) = z `f` x
1267+
1268+ foldMap f (Tuple _ x) = f x
1269+
1270+ fold :: forall f m. (Foldable f, Monoid m) => f m -> m
1271+ fold = foldMap id
1272+
1273+ traverse_ :: forall a b f m. (Applicative m, Foldable f) => (a -> m b) -> f a -> m {}
1274+ traverse_ f = foldr ((*>) <<< f) (pure {})
1275+
1276+ for_ :: forall a b f m. (Applicative m, Foldable f) => f a -> (a -> m b) -> m {}
1277+ for_ = flip traverse_
1278+
1279+ sequence_ :: forall a f m. (Applicative m, Foldable f) => f (m a) -> m {}
1280+ sequence_ = traverse_ id
1281+
1282+ mconcat :: forall f m. (Foldable f, Monoid m) => f m -> m
1283+ mconcat = foldl (<>) mempty
1284+
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+
1314+module Data.Traversable where
1315+
1316+ import Prelude
1317+ import Data.Array ((:))
1318+ import Data.Either
1319+ import Data.Eq
1320+ import Data.Foldable
1321+ import Data.Maybe
1322+ import Data.Tuple
1323+
1324+ class Traversable t where
1325+ traverse :: forall a b m. (Applicative m) => (a -> m b) -> t a -> m (t b)
1326+ sequence :: forall a m. (Applicative m) => t (m a) -> m (t a)
1327+
1328+ instance traversableArray :: Traversable [] where
1329+ traverse _ [] = pure []
1330+ traverse f (x:xs) = (:) <$> (f x) <*> traverse f xs
1331+
1332+ sequence [] = pure []
1333+ sequence (x:xs) = (:) <$> x <*> sequence xs
1334+
1335+ instance traversableEither :: Traversable (Either a) where
1336+ traverse _ (Left x) = pure (Left x)
1337+ traverse f (Right x) = Right <$> f x
1338+
1339+ sequence (Left x) = pure (Left x)
1340+ sequence (Right x) = Right <$> x
1341+
1342+ instance traversableRef :: Traversable Ref where
1343+ traverse f (Ref x) = Ref <$> f x
1344+
1345+ sequence (Ref x) = Ref <$> x
1346+
1347+ instance traversableMaybe :: Traversable Maybe where
1348+ traverse _ Nothing = pure Nothing
1349+ traverse f (Just x) = Just <$> f x
1350+
1351+ sequence Nothing = pure Nothing
1352+ sequence (Just x) = Just <$> x
1353+
1354+ instance traversableTuple :: Traversable (Tuple a) where
1355+ traverse f (Tuple x y) = Tuple x <$> f y
1356+
1357+ sequence (Tuple x y) = Tuple x <$> y
1358+
1359+ for :: forall a b m t. (Applicative m, Traversable t) => t a -> (a -> m b) -> m (t b)
1360+ for x f = traverse f x