diff options
author | Tom Smeding <tom@tomsmeding.com> | 2024-02-29 21:13:14 +0100 |
---|---|---|
committer | Tom Smeding <tom@tomsmeding.com> | 2024-02-29 21:13:14 +0100 |
commit | e094e3294e9c93fd1123b008a4b0e5f53915f5be (patch) | |
tree | 673ec0e38870cef6c7a7fee9e2ce57a248668d0a /src/Data/Bag.hs | |
parent | fc942fb8dfaad7614567f2dcbd9a911ffd474a06 (diff) |
Destroy fancy typing, and some work
Diffstat (limited to 'src/Data/Bag.hs')
-rw-r--r-- | src/Data/Bag.hs | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/src/Data/Bag.hs b/src/Data/Bag.hs index 9cdce89..f1173e4 100644 --- a/src/Data/Bag.hs +++ b/src/Data/Bag.hs @@ -3,12 +3,16 @@ module Data.Bag where data Bag a - = BTwo (Bag a) (Bag a) + = BZero | BOne a - | BZero + | BTwo (Bag a) (Bag a) deriving (Functor, Foldable, Traversable) -instance Semigroup (Bag a) where (<>) = BTwo +instance Semigroup (Bag a) where + BZero <> b = b + b <> BZero = b + b1 <> b2 = BTwo b1 b2 + instance Monoid (Bag a) where mempty = BZero instance Applicative Bag where @@ -18,3 +22,10 @@ instance Applicative Bag where _ <*> BZero = BZero BOne f <*> b = f <$> b BTwo b1 b2 <*> b = BTwo (b1 <*> b) (b2 <*> b) + +bagPartition :: (a -> Maybe b) -> Bag a -> (Bag b, Bag a) +bagPartition _ BZero = (BZero, BZero) +bagPartition f (BOne x) + | Just y <- f x = (BOne y, BZero) + | otherwise = (BZero, BOne x) +bagPartition f (BTwo b1 b2) = bagPartition f b1 <> bagPartition f b2 |