Give proper foldl' and foldr methods for LTrees

This commit is contained in:
2021-07-22 02:00:18 +02:00
parent 9f37ecb204
commit 315cf780bf
4 changed files with 89 additions and 45 deletions
+10 -2
View File
@@ -4,8 +4,8 @@ module Picture.Data
where
import Geometry.Data
--import Data.Foldable
import Data.Foldable
--import qualified Data.List as L
--import Data.Monoid
--import Data.Traversable
--import qualified Data.Foldable as F
@@ -54,6 +54,14 @@ instance Foldable LTree where
foldMap g (LBranches ts) = mconcat $ map (foldMap g) ts
foldMap g (LLeaf a) = g a
{-# INLINE foldMap #-}
foldr _ x (LBranches []) = x
foldr g x (LBranches (t:ts)) = foldr g (foldr g x (LBranches ts)) t
foldr g x (LLeaf y) = g y x
{-# INLINE foldr #-}
foldl' _ x (LBranches []) = x
foldl' g x (LBranches (t:ts)) = foldl' g x t `seq` foldl' g (foldl' g x t) (LBranches ts)
foldl' g x (LLeaf y) = g x y
{-# INLINE foldl' #-}
instance Functor LTree where
fmap f (LBranches ts) = LBranches $ fmap (fmap f) ts
fmap f (LLeaf x) = LLeaf (f x)