Skip to content

Commit

Permalink
Added Set
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackCapCoder committed Oct 24, 2024
1 parent b1502ee commit 748fe29
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions monoid-extras.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ library
Data.Monoid.Inf,
Data.Monoid.MList,
Data.Monoid.Recommend,
Data.Monoid.Set,
Data.Monoid.Split,
Data.Monoid.WithSemigroup

Expand Down
53 changes: 53 additions & 0 deletions src/Data/Monoid/Set.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE MagicHash #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}

module Data.Monoid.Set where

import Data.Data
import Data.Semigroup
import Data.Foldable
import Data.Traversable

import GHC.Exts (isTrue#, dataToTag#)
import Unsafe.Coerce (unsafeCoerce)

----

-- | @Set@ is like @Maybe@, but the value can either be
-- unspecified with @Unset@, or explicitly cleared with @Clear@
data Set a
= Unset
| Set a
| Clear
deriving (Data, Typeable, Show, Read, Functor, Foldable, Traversable)


-- | The right-hand-side or "newer" value is prefered, unless it
-- is Unset, in which case the old value is left unchanged
instance Semigroup (Set a) where

l <> Unset = l
_ <> r = r

stimes n s
| n < 1 = Unset
| let = s

instance Monoid (Set a) where
mempty = Unset


isSet :: Set a -> Bool
isSet s = isTrue# (dataToTag# s)

maybeToSet :: Maybe a -> Set a
maybeToSet = unsafeCoerce

setToMaybe :: Set a -> Maybe a
setToMaybe (Set a) = Just a
setToMaybe _ = Nothing

0 comments on commit 748fe29

Please sign in to comment.