git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Added Data.Foldable and instances.
4c16a28joneshf
examples/passing/Foldable.pursnew file+9
@@ -0,0 +1,9 @@
1+module Main where
2+
3+ import Prelude
4+ import Data.Foldable
5+
6+ sum :: [Number] -> Number
7+ sum = foldr (+) 0
8+
9+ main = Debug.Trace.print <<< sum $ [1,2,3,4,5]
prelude/prelude.purs+62−4
@@ -909,19 +909,19 @@ module Math where
909 \}" :: Number -> Number
909 \}" :: Number -> Number
910
910
911 foreign import max "function max(n1){\
911 foreign import max "function max(n1){\
912− \ return function(n2) {\
912+ \ return function(n2) {\
913 \ return Math.max(n1, n2);\
913 \ return Math.max(n1, n2);\
914 \ }\
914 \ }\
915 \}" :: Number -> Number -> Number
915 \}" :: Number -> Number -> Number
916
916
917 foreign import min "function min(n1){\
917 foreign import min "function min(n1){\
918− \ return function(n2) {\
918+ \ return function(n2) {\
919 \ return Math.min(n1, n2);\
919 \ return Math.min(n1, n2);\
920 \ }\
920 \ }\
921 \}" :: Number -> Number -> Number
921 \}" :: Number -> Number -> Number
922
922
923 foreign import pow "function pow(n){\
923 foreign import pow "function pow(n){\
924− \ return function(p) {\
924+ \ return function(p) {\
925 \ return Math.pow(n, p);\
925 \ return Math.pow(n, p);\
926 \ }\
926 \ }\
927 \}" :: Number -> Number -> Number
927 \}" :: Number -> Number -> Number
@@ -1206,7 +1206,7 @@ module Data.Enum where
1206 class Enum a where
1206 class Enum a where
1207 toEnum :: Number -> Maybe a
1207 toEnum :: Number -> Maybe a
1208 fromEnum :: a -> Number
1208 fromEnum :: a -> Number
1209−
1209+
1210module Text.Parsing.Read where
1210module Text.Parsing.Read where
1211
1211
1212 class Read a where
1212 class Read a where
@@ -1223,3 +1223,61 @@ module Text.Parsing.Read where
1223
1223
1224 instance readNumber :: Read Number where
1224 instance readNumber :: Read Number where
1225 read = readNumberImpl
1225 read = readNumberImpl
1226+
1227+module Data.Foldable where
1228+
1229+ import Prelude
1230+ import Data.Either
1231+ import Data.Eq
1232+ import Data.Maybe
1233+ import Data.Monoid
1234+ import Data.Tuple
1235+
1236+ class Foldable f where
1237+ foldr :: forall a b. (a -> b -> b) -> b -> f a -> b
1238+ foldl :: forall a b. (b -> a -> b) -> b -> f a -> b
1239+ foldMap :: forall a m. (Monoid m) => (a -> m) -> f a -> m
1240+
1241+ instance foldableArray :: Foldable [] where
1242+ foldr _ z [] = z
1243+ foldr f z (x:xs) = x `f` (foldr f z xs)
1244+
1245+ foldl _ z [] = z
1246+ foldl f z (x:xs) = foldl f (z `f` x) xs
1247+
1248+ foldMap _ [] = mempty
1249+ foldMap f (x:xs) = f x <> foldMap f xs
1250+
1251+ instance foldableEither :: Foldable (Either a) where
1252+ foldr _ z (Left _) = z
1253+ foldr f z (Right x) = x `f` z
1254+
1255+ foldl _ z (Left _) = z
1256+ foldl f z (Right x) = z `f` x
1257+
1258+ foldMap f (Left _) = mempty
1259+ foldMap f (Right x) = f x
1260+
1261+ instance foldableMaybe :: Foldable Maybe where
1262+ foldr _ z Nothing = z
1263+ foldr f z (Just x) = x `f` z
1264+
1265+ foldl _ z Nothing = z
1266+ foldl f z (Just x) = z `f` x
1267+
1268+ foldMap f Nothing = mempty
1269+ foldMap f (Just x) = f x
1270+
1271+ instance foldableRef :: Foldable Ref where
1272+ foldr f z (Ref x) = x `f` z
1273+
1274+ foldl f z (Ref x) = z `f` x
1275+
1276+ foldMap f (Ref x) = f x
1277+
1278+ instance foldableTuple :: Foldable (Tuple a) where
1279+ foldr f z (Tuple _ x) = x `f` z
1280+
1281+ foldl f z (Tuple _ x) = z `f` x
1282+
1283+ foldMap f (Tuple _ x) = f x