separated 0.2.3 → 0.3.0
raw patch · 7 files changed
+247/−20 lines, 7 filesdep +deriving-compatdep ~basedep ~bifunctorsdep ~lens
Dependencies added: deriving-compat
Dependency ranges changed: base, bifunctors, lens, semigroupoids, semigroups
Files
- changelog.md +4/−0
- separated.cabal +19/−13
- src/Data/Separated.hs +45/−7
- src/Data/Separated/After.hs +9/−0
- src/Data/Separated/Before.hs +9/−0
- src/Data/Separated/Between.hs +72/−0
- src/Data/Separated/Internal.hs +89/−0
changelog.md view
@@ -1,3 +1,7 @@+0.3.0++* Add `Foldable1` and `Traversable1` instances for `Pesarated1`.+ 0.2.3 * Add `Foldable` and `Traversable` instances for `Separated`, `Separated1`, `Pesarated` and `Pesarated1`.
separated.cabal view
@@ -1,7 +1,7 @@ name: separated-version: 0.2.3+version: 0.3.0 license: BSD3-license-File: LICENCE+license-file: LICENCE author: Queensland Functional Programming Lab <oᴉ˙ldɟb@llǝʞsɐɥ> maintainer: Queensland Functional Programming Lab <oᴉ˙ldɟb@llǝʞsɐɥ> copyright: Copyright (c) 2017, Commonwealth Scientific and Industrial Research Organisation (CSIRO) ABN 41 687 119 230.@@ -30,11 +30,12 @@ Haskell2010 build-depends:- base < 5 && >= 3- , lens >= 4.0- , semigroups >= 0.9- , semigroupoids >= 4.0- , bifunctors >= 4.0+ base >= 4.7 && < 5+ , lens >= 4.0 && < 5+ , semigroups >= 0.9 && < 1+ , semigroupoids >= 4.0 && < 6+ , bifunctors >= 4.0 && < 6+ , deriving-compat >= 0.3 && < 0.4 ghc-options: -Wall@@ -45,7 +46,12 @@ hs-source-dirs: src + other-modules:+ Data.Separated.Internal exposed-modules:+ Data.Separated.After+ Data.Separated.Between+ Data.Separated.Before Data.Separated test-suite doctests@@ -59,13 +65,13 @@ Haskell2010 build-depends:- base < 5 && >= 3- , doctest >= 0.9.7- , filepath >= 1.3- , directory >= 1.1- , QuickCheck >= 2.0+ base >= 4.7 && < 5+ , doctest >= 0.9.7+ , filepath >= 1.3+ , directory >= 1.1+ , QuickCheck >= 2.0 , template-haskell >= 2.8- , parsec >= 3.1+ , parsec >= 3.1 ghc-options: -Wall
src/Data/Separated.hs view
@@ -6,6 +6,7 @@ {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FunctionalDependencies #-} {-# LANGUAGE RankNTypes #-}+{-# LANGUAGE TupleSections #-} module Data.Separated( -- * Data types@@ -25,6 +26,9 @@ , Separated1Single(..) , Pesarated1Single(..) , Construct(..)+, Sprinkle(..)+, skrinple+, skrinpleMay , SeparatedCons(..) , PesaratedCons(..) -- * Appending@@ -47,9 +51,13 @@ import Data.Functor(Functor(fmap), (<$>)) import Data.Functor.Apply as Apply(Apply((<.>))) import Data.List(intercalate, zipWith, repeat)+import Data.List.NonEmpty (NonEmpty ((:|)), nonEmpty)+import Data.Maybe (Maybe) import Data.Monoid(Monoid(mappend, mempty)) import Data.Ord(Ord) import Data.Semigroup as Semigroup(Semigroup((<>)))+import Data.Semigroup.Foldable (Foldable1, foldMap1)+import Data.Semigroup.Traversable (Traversable1, traverse1) import Data.String(String) import Data.Traversable(Traversable, traverse) import Data.Tuple(fst, snd, uncurry)@@ -252,10 +260,6 @@ -> f a s empty :: f s a- sprinkle ::- s- -> [a]- -> f s a infixl 9 +- @@ -273,9 +277,6 @@ swapped # (s +- a) empty = Separated mempty- sprinkle s as =- Separated- ((,) s <$> as) -- | One element and one separator. --@@ -291,10 +292,36 @@ _Wrapped # (s +- a) empty = Pesarated empty++-- | Generalised interspersion+class Sprinkle f where+ sprinkle ::+ s+ -> [a]+ -> f s a++instance Sprinkle Separated where sprinkle s as =+ Separated+ ((,) s <$> as)++instance Sprinkle Pesarated where+ sprinkle s as = Pesarated (swapped # sprinkle s as) +instance Sprinkle Separated1 where+ sprinkle s as =+ Separated1 s (swapped # sprinkle s as)++skrinple :: s -> NonEmpty a -> Pesarated1 s a+skrinple s (a:|as) =+ Pesarated1 (Separated1 a (sprinkle s as))++skrinpleMay :: s -> [a] -> Maybe (Pesarated1 s a)+skrinpleMay s as =+ skrinple s <$> nonEmpty as+ -- | Prepend a value to a separated-like structure. class (f ~ SeparatedConsF g, g ~ SeparatedConsG f) => SeparatedCons f g where type SeparatedConsF g :: * -> * -> *@@ -785,9 +812,20 @@ foldr = bifoldr (flip const) +instance Foldable1 (Pesarated1 a) where+ foldMap1 f (Pesarated1 (Separated1 b (Separated abs))) =+ foldMap1 f (b :| fmap snd abs)+ instance Traversable (Pesarated1 a) where traverse = bitraverse pure++instance Traversable1 (Pesarated1 a) where+ traverse1 f (Pesarated1 (Separated1 b (Separated abs))) =+ let consPair (y,x) s = y -: x -: s+ in case abs of+ [] -> singlePesarated <$> f b+ ((a,b'):xs) -> consPair <$> fmap (,a) (f b) <.> traverse1 f (b' -: xs ^. pesarated) -- | Applies functions with separator values, using a zipping operation, -- appending elements.
+ src/Data/Separated/After.hs view
@@ -0,0 +1,9 @@+module Data.Separated.After+ ( After(..)+ -- * Isos+ , after+ , afterBefore+ )+ where++import Data.Separated.Internal
+ src/Data/Separated/Before.hs view
@@ -0,0 +1,9 @@+module Data.Separated.Before+ ( Before(..)+ -- * Isos+ , before+ , beforeAfter+ )+ where++import Data.Separated.Internal
+ src/Data/Separated/Between.hs view
@@ -0,0 +1,72 @@+{-# language DeriveFunctor #-}+{-# language DeriveFoldable #-}+{-# language DeriveTraversable #-}+{-# language TemplateHaskell #-}+module Data.Separated.Between+ ( -- * Datatypes+ Between(..)+ , Between'(..)+ -- * Isos+ , between+ , between'+ , betweens+ )+ where++import Prelude (Show)+import Control.Applicative+import Control.Lens+import Data.Bifoldable+import Data.Bitraversable+import Data.Deriving+import Data.Eq+import Data.Functor+import Data.Foldable+import Data.Monoid+import Data.Ord++-- | An @a@ with an @s@ on the left and a @t@ on the right+data Between s t a = Between s a t+ deriving (Eq, Foldable, Functor, Ord, Traversable, Show)++deriveEq1 ''Between+deriveShow1 ''Between++-- | @'Between' s t a@ is isomorphic to @(s, a, t)@+between :: Iso (s, a, s') (t, b, t') (Between s s' a) (Between t t' b) +between =+ iso+ (\(t, b, t') -> Between t b t')+ (\(Between s a s') -> (s, a, s'))++-- | An @a@ with an @s@ on each side+data Between' s a = Between' s a s+ deriving (Eq, Foldable, Functor, Traversable, Show)++deriveEq1 ''Between'+deriveShow1 ''Between'++instance Bifunctor Between' where+ bimap f g (Between' s a s') = Between' (f s) (g a) (f s')++-- | @'bifoldMap' f g ('Between'' s a s') = f s '<>' g a '<>' f s'@+instance Bifoldable Between' where+ bifoldMap f g (Between' s a s') = f s <> g a <> f s'+ +-- | @'bitraverse' f g ('Between'' s a s') = 'Between'' '<$>' f s '<*>' g a '<*>' f s'@+instance Bitraversable Between' where+ bitraverse f g (Between' s a s') = Between' <$> f s <*> g a <*> f s'++-- | @'Between'' s a@ is isomorphic to @(s, a, s)@+between' :: Iso (s, a, s) (t, b, t) (Between' s a) (Between' t b) +between' =+ iso+ (\(t, b, t') -> Between' t b t')+ (\(Between' s a s') -> (s, a, s'))++-- | @'Between'' s a@ is isomorphic to @'Between' s s a@+betweens :: Iso (Between s s a) (Between t t b) (Between' s a) (Between' t b)+betweens =+ iso+ (\(Between t b t') -> Between' t b t')+ (\(Between' s a s') -> Between s a s')
+ src/Data/Separated/Internal.hs view
@@ -0,0 +1,89 @@+{-# language DeriveFunctor #-}+{-# language DeriveFoldable #-}+{-# language DeriveTraversable #-}+{-# language TemplateHaskell #-}++module Data.Separated.Internal where++import Prelude (Show, uncurry)+import Control.Applicative+import Control.Lens+import Data.Bifoldable+import Data.Bitraversable+import Data.Deriving+import Data.Eq+import Data.Foldable+import Data.Functor+import Data.Monoid+import Data.Ord++-- | An @s@ that comes before an @a@+data Before s a = Before s a+ deriving (Eq, Foldable, Functor, Ord, Traversable, Show)++deriveEq1 ''Before+deriveShow1 ''Before++instance Bifunctor Before where+ bimap f g (Before s a) = Before (f s) (g a)++-- | @'bifoldMap' f g ('Before' s a) = f s '<>' g a@+instance Bifoldable Before where+ bifoldMap f g (Before s a) = f s <> g a++-- | @'bitraverse' f g ('Before' s a) = 'Before' '<$>' f s '<*>' g a@+instance Bitraversable Before where+ bitraverse f g (Before s a) = Before <$> f s <*> g a++instance Swapped Before where+ swapped =+ iso+ (\(Before a b) -> Before b a)+ (\(Before b a) -> Before a b)++-- | @'Before' s a@ is isomorphic to @(s, a)@+before :: Iso (s, a) (t, b) (Before s a) (Before t b) +before = iso (uncurry Before) (\(Before s a) -> (s, a))++-- | @'Before' s a@ is isomorphic to @'After' a s@+--+-- @'beforeAfter' == 'from' 'afterBefore'@+beforeAfter :: Iso (After a s) (After b t) (Before s a) (Before t b)+beforeAfter =+ iso+ (\(After a s) -> Before a s)+ (\(Before t b) -> After t b)++-- | An @s@ that comes after an @a@+data After s a = After a s+ deriving (Eq, Foldable, Functor, Ord, Traversable, Show)++deriveEq1 ''After+deriveShow1 ''After++instance Bifunctor After where+ bimap f g (After a s) = After (g a) (f s)++-- | @'bifoldMap' f g ('After' a s) = g a '<>' f s@+instance Bifoldable After where+ bifoldMap f g (After a s) = g a <> f s++-- | @'bitraverse' f g ('After' a s) = 'After' '<$>' g a '<*>' f s@+instance Bitraversable After where+ bitraverse f g (After a s) = After <$> g a <*> f s+ +instance Swapped After where+ swapped =+ iso+ (\(After a b) -> After b a)+ (\(After b a) -> After a b)++-- | @'After' s a@ is isomorphic to @(a, s)@+after :: Iso (a, s) (b, t) (After s a) (After t b)+after = iso (uncurry After) (\(After a s) -> (a, s))++-- | @'After' s a@ is isomorphic to @'Before' a s@+--+-- @'afterBefore' == 'from' 'beforeAfter'@+afterBefore :: Iso (Before s a) (Before t b) (After a s) (After b t) +afterBefore = from beforeAfter