git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Array is unfunny now. Add some foldable functions. Removed duplicates.
905ec78joneshf
examples/passing/Foldable.purs+13−3
@@ -2,8 +2,18 @@ module Main where
2
2
3 import Prelude
3 import Prelude
4 import Data.Foldable
4 import Data.Foldable
5+ import Data.Monoid
5
6
6− sum :: [Number] -> Number
7+ data Tree a = Node a | Branch (Tree a) (Tree a)
7− sum = foldr (+) 0
8
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+
9− main = Debug.Trace.print <<< sum $ [1,2,3,4,5]
19+ main = Debug.Trace.print <<< sum $ Branch (Node 1) (Branch (Node 2) (Node 3))
examples/passing/Traversable.purs+2−2
@@ -1,8 +1,8 @@
1module Main where
1module Main where
2
2
3 import Prelude
3 import Prelude
4+ import Data.Array
4 import Data.Maybe
5 import Data.Maybe
5 import Data.Traversable
6 import Data.Traversable
6− import Data.Tuple
7
7
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
599 filter p (x:xs) | p x = x : filter p xs
599 filter p (x:xs) | p x = x : filter p xs
600 filter p (_:xs) = filter p xs
600 filter p (_:xs) = filter p xs
601
601
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−
607 isEmpty :: forall a. [a] -> Boolean
602 isEmpty :: forall a. [a] -> Boolean
608 isEmpty [] = true
603 isEmpty [] = true
609 isEmpty _ = false
604 isEmpty _ = false
@@ -616,14 +611,6 @@ module Data.Array where
616 zipWith f (a:as) (b:bs) = f a b : zipWith f as bs
611 zipWith f (a:as) (b:bs) = f a b : zipWith f as bs
617 zipWith _ _ _ = []
612 zipWith _ _ _ = []
618
613
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−
627 drop :: forall a. Number -> [a] -> [a]
614 drop :: forall a. Number -> [a] -> [a]
628 drop 0 xs = xs
615 drop 0 xs = xs
629 drop _ [] = []
616 drop _ [] = []
@@ -1295,6 +1282,35 @@ module Data.Foldable where
1295 mconcat :: forall f m. (Foldable f, Monoid m) => f m -> m
1282 mconcat :: forall f m. (Foldable f, Monoid m) => f m -> m
1296 mconcat = foldl (<>) mempty
1283 mconcat = foldl (<>) mempty
1297
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+
1298module Data.Traversable where
1314module Data.Traversable where
1299
1315
1300 import Prelude
1316 import Prelude