From dbb8c57f0b55a4290edca4bbf92eef36e57044f2 Mon Sep 17 00:00:00 2001 From: Frank Staals Date: Wed, 25 Dec 2024 17:13:15 +0100 Subject: [PATCH] fixed the order --- hgeometry-combinatorial/src/HGeometry/Trie.hs | 16 +++++++++++++--- hgeometry-combinatorial/test/TrieSpec.hs | 10 +++++----- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/hgeometry-combinatorial/src/HGeometry/Trie.hs b/hgeometry-combinatorial/src/HGeometry/Trie.hs index 02cd8b481..e8e06e503 100644 --- a/hgeometry-combinatorial/src/HGeometry/Trie.hs +++ b/hgeometry-combinatorial/src/HGeometry/Trie.hs @@ -19,7 +19,6 @@ import Data.Bitraversable import Data.Foldable1 import Data.Functor.Apply ((<.*>)) import Data.Functor.Classes -import Data.Monoid (Endo(..)) import Data.Semigroup.Traversable -------------------------------------------------------------------------------- @@ -35,9 +34,20 @@ deriving instance (Functor (f e)) => Functor (TrieF f e) deriving instance (Foldable (f e)) => Foldable (TrieF f e) deriving instance (Traversable (f e)) => Traversable (TrieF f e) +-- instance Foldable (f e) => Foldable1 (TrieF f e) where +-- foldMap1 f (Node v chs) = let Endo g = foldMap (\x -> Endo $ \x0 -> x0 <> foldMap1 f x) chs +-- in g (f v) +-- somehow the order is wrong here... + instance Foldable (f e) => Foldable1 (TrieF f e) where - foldMap1 f (Node v chs) = let Endo g = foldMap (\x -> Endo $ \x0 -> x0 <> foldMap1 f x) chs - in g (f v) + foldMap1 f (Node v chs) = case foldMap (Just . foldMap1 f) chs of + Nothing -> f v + Just s -> f v <> s + -- foldMap1 = foldMap1Default + + -- f (Node v chs) = let Endo g = foldMap (\x -> Endo $ \x0 -> x0 <> foldMap1 f x) chs + -- in g (f v) + instance Traversable (f e) => Traversable1 (TrieF f e) where traverse1 f (Node v chs) = diff --git a/hgeometry-combinatorial/test/TrieSpec.hs b/hgeometry-combinatorial/test/TrieSpec.hs index c4281c7ef..693408595 100644 --- a/hgeometry-combinatorial/test/TrieSpec.hs +++ b/hgeometry-combinatorial/test/TrieSpec.hs @@ -1,6 +1,6 @@ module TrieSpec(spec) where -import Control.Lens +-- import Control.Lens import Data.Foldable1 import qualified Data.List.NonEmpty as NonEmpty import qualified Data.Map as Map @@ -13,14 +13,14 @@ import Test.Hspec spec :: Spec spec = describe "Trie tests" $ do it "fold1 Map" $ - fold1 myTrie `shouldBe` "rootbarfoograndchild" + fold1 myTrie `shouldBe` "rootfoograndchildbar" it "foldable1 to list Map" $ - toNonEmpty myTrie `shouldBe` NonEmpty.fromList ["root","bar","foo","grandchild"] + toNonEmpty myTrie `shouldBe` NonEmpty.fromList ["root","foo","grandchild","bar"] it "fold1 KV" $ - fold1 myTrie `shouldBe` "rootbarfoograndchild" + fold1 myTrie2 `shouldBe` "rootfoograndchildbar" it "foldable1 to list KV" $ - toNonEmpty myTrie `shouldBe` NonEmpty.fromList ["root","bar","foo","grandchild"] + toNonEmpty myTrie2 `shouldBe` NonEmpty.fromList ["root","foo","grandchild","bar"]