ALGO: generalize the QuadTree folds

Now we traverse over all nodes (TNode, TLeaf, TNil),
not just TLeaf.
This commit is contained in:
hasufell 2014-11-18 00:56:24 +01:00
parent 8d7ed598e2
commit 7527e0bec3
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020

View File

@ -121,15 +121,16 @@ getSquareByZipper sq z = go sq (reverse . snd $ z)
go sq' (SECrumb {}:zs) = go (seSq sq') zs go sq' (SECrumb {}:zs) = go (seSq sq') zs
-- |Left fold over the tree leafs. -- |Left fold over the tree.
qtFoldl :: (a -> b -> a) -> a -> QuadTree b -> a qtFoldl :: (a -> QuadTree b -> a) -> a -> QuadTree b -> a
qtFoldl _ sv (TNil) = sv qtFoldl f sv qt@(TNode nw ne sw se) = foldl (qtFoldl f)
qtFoldl f sv (TLeaf a) = f sv a (f sv qt)
qtFoldl f sv (TNode nw ne sw se) = foldl (qtFoldl f) sv [nw, ne, sw, se] [nw, ne, sw, se]
qtFoldl f sv qt = f sv qt
-- |Right fold over the tree leafs. -- |Right fold over the tree.
qtFoldr :: (b -> a -> a) -> a -> QuadTree b -> a qtFoldr :: (QuadTree b -> a -> a) -> a -> QuadTree b -> a
qtFoldr f sv qt = qtFoldl (\g b x -> g (f b x)) id qt sv qtFoldr f sv qt = qtFoldl (\g b x -> g (f b x)) id qt sv