unfoldable 0.0.0 → 0.1.0
raw patch · 3 files changed
+32/−4 lines, 3 filesdep ~transformers
Dependency ranges changed: transformers
Files
- src/Data/Splittable.hs +10/−2
- src/Data/Unfoldable.hs +20/−0
- unfoldable.cabal +2/−2
src/Data/Splittable.hs view
@@ -11,6 +11,7 @@ import qualified System.Random as R import Data.List (mapAccumR)+import Data.Monoid (Dual(..)) -- | Splittable datatypes are datatypes that can be used as seeds for unfolds. class Splittable s where@@ -24,10 +25,12 @@ -- | If a datatype is bounded and enumerable, we can use 'getInt' to produce a value from a seed. boundedEnum :: forall s a. (Splittable s, Bounded a, Enum a) => s -> a-boundedEnum s = toEnum $ (getInt s `mod` (1 + ub - lb)) + lb+boundedEnum s = toEnum $ (getInt s `mod'` (1 + ub - lb)) + lb where lb = fromEnum (minBound :: a) ub = fromEnum (maxBound :: a)+ n `mod'` 0 = n - lb+ n `mod'` m = n `mod` m data Left = L instance Splittable Left where@@ -39,7 +42,7 @@ instance Splittable Right where split = replicate choose fs = last fs- getInt R = maxBound+ getInt R = 0 instance Splittable R.StdGen where split 0 _ = []@@ -72,3 +75,8 @@ choose fs = either (choose (map (. Left) fs)) (choose (map (. Right) fs)) getInt (Left a) = getInt a * 2 getInt (Right a) = getInt a * 2 + 1++instance Splittable s => Splittable (Dual s) where+ split n = map Dual . reverse . split n . getDual+ choose fs = choose (map (. Dual) $ reverse fs) . getDual+ getInt = negate . getInt . getDual
src/Data/Unfoldable.hs view
@@ -16,6 +16,12 @@ import Control.Applicative import Control.Monad.Trans.State import Data.Splittable+import Data.Monoid (Dual(..))+import Data.Functor.Compose+import Data.Functor.Constant+import Data.Functor.Identity+import Data.Functor.Product+import Data.Functor.Reverse -- | Data structures that can be unfolded. --@@ -76,3 +82,17 @@ instance (Bounded a, Enum a) => Unfoldable ((,) a) where unfoldMap f = spread $ (,) <$> to boundedEnum <*> to f +instance Unfoldable Identity where+ unfoldMap f = Identity . f++instance (Bounded a, Enum a) => Unfoldable (Constant a) where+ unfoldMap _ = Constant . boundedEnum+ +instance (Unfoldable p, Unfoldable q) => Unfoldable (Product p q) where+ unfoldMap f = spread $ Pair <$> to (unfoldMap f) <*> to (unfoldMap f)++instance (Unfoldable p, Unfoldable q) => Unfoldable (Compose p q) where+ unfoldMap f = Compose . unfoldMap (unfoldMap f)++instance Unfoldable f => Unfoldable (Reverse f) where+ unfoldMap f = Reverse . unfoldMap (f . getDual) . Dual
unfoldable.cabal view
@@ -1,5 +1,5 @@ Name: unfoldable-Version: 0.0.0+Version: 0.1.0 Synopsis: Class of data structures that can be unfolded from a seed value. Homepage: https://github.com/sjoerdvisscher/unfoldable License: BSD3@@ -19,7 +19,7 @@ Build-depends: base >= 4 && < 5 - , transformers >= 0.2 && < 0.4+ , transformers >= 0.3 && < 0.4 , random >= 1.0 && < 1.1 source-repository head