pointedlist 0.3.5 → 0.4.0
raw patch · 3 files changed
+69/−50 lines, 3 filesdep +fclabelsdep −data-accessordep ~basePVP ok
version bump matches the API change (PVP)
Dependencies added: fclabels
Dependencies removed: data-accessor
Dependency ranges changed: base
API changes (from Hackage documentation)
- Data.List.PointedList: focusA :: Accessor (PointedList a) a
- Data.List.PointedList: instance Binary t1 => Binary (PointedList t1)
- Data.List.PointedList: instance Eq a => Eq (PointedList a)
- Data.List.PointedList: instance Ord a => Ord (PointedList a)
- Data.List.PointedList: instance Show a => Show (PointedList a)
- Data.List.PointedList: move :: Int -> PointedList a -> Maybe (PointedList a)
+ Data.List.PointedList: _focus :: PointedList a -> a
+ Data.List.PointedList: instance (Eq a) => Eq (PointedList a)
+ Data.List.PointedList: instance (Show a) => Show (PointedList a)
+ Data.List.PointedList: moveN :: Int -> PointedList a -> Maybe (PointedList a)
+ Data.List.PointedList: moveTo :: Int -> PointedList a -> Maybe (PointedList a)
+ Data.List.PointedList: replace :: a -> PointedList a -> PointedList a
+ Data.List.PointedList: reversedPrefix :: PointedList a -> [a]
+ Data.List.PointedList: suffix :: PointedList a -> [a]
+ Data.List.PointedList.Circular: moveN :: Int -> PointedList a -> PointedList a
- Data.List.PointedList: find :: Eq a => a -> PointedList a -> Maybe (PointedList a)
+ Data.List.PointedList: find :: (Eq a) => a -> PointedList a -> Maybe (PointedList a)
- Data.List.PointedList: focus :: PointedList a -> a
+ Data.List.PointedList: focus :: PointedList a :-> a
Files
- Data/List/PointedList.hs +56/−43
- Data/List/PointedList/Circular.hs +9/−3
- pointedlist.cabal +4/−4
Data/List/PointedList.hs view
@@ -1,3 +1,5 @@+{-# LANGUAGE TemplateHaskell, TypeOperators #-}+ -- | An implementation of a zipper-like non-empty list structure that tracks -- an index position in the list (the 'focus'). module Data.List.PointedList where@@ -6,19 +8,27 @@ import Control.Applicative import Control.Monad-import Data.Accessor-import Data.Binary+import Data.Binary () import Data.Foldable hiding (find) import Data.List hiding (length, foldl, foldr, find, elem) import qualified Data.List as List import Data.Maybe+import Data.Record.Label import Data.Traversable -- | The implementation of the pointed list structure which tracks the current -- position in the list structure.-data PointedList a = PointedList [a] a [a]- deriving (Eq, Ord {-! Binary !-})+data PointedList a = PointedList+ { reversedPrefix :: [a]+ , _focus :: a+ , suffix :: [a]+ } deriving ({-! Binary !-}) +$(mkLabels [''PointedList])++-- | Focus element accessor label.+focus :: PointedList a :-> a+ instance (Show a) => Show (PointedList a) where show (PointedList ls x rs) = show (reverse ls) ++ " " ++ show x ++ " " ++ show rs @@ -29,14 +39,15 @@ foldr f z (PointedList ls x rs) = foldl (flip f) (foldr f z (x:rs)) ls instance Traversable PointedList where- traverse f (PointedList ls x rs) = PointedList <$> (reverse <$> traverse f (reverse ls)) <*> f x <*> traverse f rs+ traverse f (PointedList ls x rs) = PointedList <$>+ (reverse <$> traverse f (reverse ls)) <*> f x <*> traverse f rs --- | Create a @PointedList@ with a single element.+-- | Create a 'PointedList' with a single element. singleton :: a -> PointedList a singleton x = PointedList [] x [] --- | Possibly create a @Just PointedList@ if the provided list has at least one--- element; otherwise, return Nothing.+-- | Possibly create a @'Just' 'PointedList'@ if the provided list has at least+-- one element; otherwise, return Nothing. -- -- The provided list's head will be the focus of the list, and the rest of -- list will follow on the right side.@@ -44,8 +55,8 @@ fromList [] = Nothing fromList (x:xs) = Just $ PointedList [] x xs --- | Possibly create a @Just PointedList@ if the provided list has at least one--- element; otherwise, return Nothing.+-- | Possibly create a @'Just' 'PointedList'@ if the provided list has at least+-- one element; otherwise, return Nothing. -- -- The provided list's last element will be the focus of the list, following -- the rest of the list in order, to the left.@@ -54,13 +65,9 @@ fromListEnd xs = Just $ PointedList xs' x [] where (x:xs') = reverse xs --- | The focus element of the pointed list.-focus :: PointedList a -> a-focus (PointedList _ x _) = x---- | Accessor to read and edit the focus.-focusA :: Accessor (PointedList a) a-focusA = accessor focus $ \b (PointedList a _ c) -> PointedList a b c+-- | Replace the focus of the list, retaining the prefix and suffix.+replace :: a -> PointedList a -> PointedList a+replace = set focus -- | Possibly move the focus to the next element in the list. next :: PointedList a -> Maybe (PointedList a)@@ -106,7 +113,7 @@ -- | Possibly delete the element at the focus, then move the element on the -- left to the focus. If no element is on the left, focus on the element to -- the right. If the deletion will cause the list to be empty, return--- @Nothing@.+-- 'Nothing'. deleteLeft :: PointedList a -> Maybe (PointedList a) deleteLeft (PointedList [] _ [] ) = Nothing deleteLeft (PointedList (l:ls) _ rs) = Just $ PointedList ls l rs@@ -115,7 +122,7 @@ -- | Possibly delete the element at the focus, then move the element on the -- right to the focus. If no element is on the right, focus on the element to -- the left. If the deletion will cause the list to be empty, return--- @Nothing@.+-- 'Nothing'. deleteRight :: PointedList a -> Maybe (PointedList a) deleteRight (PointedList [] _ [] ) = Nothing deleteRight (PointedList ls _ (r:rs)) = Just $ PointedList ls r rs@@ -139,43 +146,53 @@ atEnd (PointedList _ _ []) = True atEnd _ = False --- | Create a @PointedList@ of variations of the provided @PointedList@, in--- which each element is focused, with the provided @PointedList@ as the+-- | Create a 'PointedList' of variations of the provided 'PointedList', in+-- which each element is focused, with the provided 'PointedList' as the -- focus of the sets. positions :: PointedList a -> PointedList (PointedList a) positions p@(PointedList ls x rs) = PointedList left p right where left = unfoldr (\p -> fmap (join (,)) $ previous p) p right = unfoldr (\p -> fmap (join (,)) $ next p) p --- | Map over the @PointedList@s created via 'positions', such that @f@ is+-- | Map over the 'PointedList's created via 'positions', such that @f@ is -- called with each element of the list focused in the provided--- @PointedList@. An example makes this easier to understand:+-- 'PointedList'. An example makes this easier to understand: -- -- > contextMap atStart (fromJust $ fromList [1..5]) contextMap :: (PointedList a -> b) -> PointedList a -> PointedList b contextMap f z = fmap f $ positions z --- | Create a @PointedList a@ of @(a, Bool)@, in which the boolean values+-- | Create a @'PointedList' a@ of @(a, 'Bool')@, in which the boolean values -- specify whether the current element has the focus. That is, all of the--- booleans will be @False@, except the focused element.+-- booleans will be 'False', except the focused element. withFocus :: PointedList a -> PointedList (a, Bool)-withFocus (PointedList a b c) = PointedList (zip a (repeat False)) (b, True) (zip c (repeat False))+withFocus (PointedList a b c) =+ PointedList (zip a (repeat False)) (b, True) (zip c (repeat False)) -- | Move the focus to the specified index. -move :: Int -> PointedList a -> Maybe (PointedList a)-move n pl@(PointedList a _ _) | n < 0 || n >= Data.List.PointedList.length pl- = Nothing - | Prelude.length a == n = Just pl- | Prelude.length a >= n = move n (tryPrevious pl)- | otherwise = move n (tryNext pl)+moveTo :: Int -> PointedList a -> Maybe (PointedList a)+moveTo n pl@(PointedList a _ _)+ | n < 0 = Nothing+ | atEnd pl = Nothing+ | i == n = Just pl+ | i > n = moveTo n $ tryPrevious pl+ | otherwise = moveTo n $ tryNext pl+ where i = index pl -- Cache length of the prefix +-- | Move the focus by @n@, relative to the current index. Negative values move+-- the focus backwards, positive values more forwards through the list.+moveN :: Int -> PointedList a -> Maybe (PointedList a)+moveN 0 pl = Just pl+moveN n pl | n < 0 = maybe Nothing (moveN (n+1)) $ previous pl+ | n > 0 = maybe Nothing (moveN (n-1)) $ next pl+ -- | Move the focus to the specified element, if it is present. -- -- Patch with much faster algorithm provided by Runar Bjarnason for version -- 0.3.2. Improved again by Runar Bjarnason for version 0.3.3 to support -- infinite lists on both sides of the focus. find :: Eq a => a -> PointedList a -> Maybe (PointedList a)-find x pl = find' ((x ==) . focus) $ positions pl+find x pl = find' ((x ==) . (get focus)) $ positions pl where find' pred (PointedList a b c) = if pred b then Just b else List.find pred (merge a c)@@ -186,14 +203,10 @@ index :: PointedList a -> Int index (PointedList a _ _) = Prelude.length a ------------------------------------------------------------ DERIVES GENERATED CODE--- DO NOT MODIFY BELOW THIS LINE--- CHECKSUM: 1323105363+-- GENERATED START -instance Binary t1 => Binary (PointedList t1)- where put (PointedList x1- x2- x3) = return () >> (put x1 >> (put x2 >> put x3))- get = case 0 of- 0 -> ap (ap (ap (return PointedList) get) get) get+ +instance (Eq a) => Eq (PointedList a) where+ PointedList x1 x2 x3 == PointedList y1 y2 y3+ = x1 == y1 && x2 == y2 && x3 == y3+-- GENERATED STOP
Data/List/PointedList/Circular.hs view
@@ -8,15 +8,16 @@ , delete , deleteLeft , deleteRight+ , moveN ) where import Data.List.PointedList ( PointedList(..)+ , focus , singleton , fromList , fromListEnd- , focus- , focusA+ , replace , insert , insertLeft , insertRight@@ -25,7 +26,6 @@ , positions , contextMap , withFocus- , move , find , index )@@ -70,3 +70,9 @@ deleteRight (PointedList ls _ (r:rs)) = Just $ PointedList ls r rs deleteRight (PointedList ls _ [] ) = let (x:xs) = reverse ls in Just $ PointedList [] x xs++-- | Move+moveN :: Int -> PointedList a -> PointedList a+moveN 0 pl = pl+moveN n pl | n > 0 = moveN (n-1) $ next pl+ | otherwise = moveN (n+1) $ previous pl
pointedlist.cabal view
@@ -1,5 +1,5 @@ Name: pointedlist-Version: 0.3.5+Version: 0.4.0 Synopsis: A zipper-like comonad which works as a list, tracking a position. Category: Data Description:@@ -13,9 +13,9 @@ License-file: LICENSE Author: Jeff Wheeler Maintainer: jeffwheeler@gmail.com-Build-type: Custom-Build-depends: base>=4+Build-type: Simple+Build-depends: base >= 4 && < 5 Build-depends: binary-Build-depends: data-accessor+Build-depends: fclabels Exposed-modules: Data.List.PointedList Data.List.PointedList.Circular