Wed Jun 3 12:14:47 CDT 2009 wasserman.louis@gmail.com
* Second round of methods for Data.Sequence
This patch includes the following methods: inits, tails, span, break, iterate, unfoldr, partition, and one or two more.
New patches:
[Second round of methods for Data.Sequence
wasserman.louis@gmail.com**20090603171447
Ignore-this: 5c14e067dd93e64db1794bac21b53295
This patch includes the following methods: inits, tails, span, break, iterate, unfoldr, partition, and one or two more.
] {
hunk ./Data/Sequence.hs 44
(|>), -- :: Seq a -> a -> Seq a
(><), -- :: Seq a -> Seq a -> Seq a
fromList, -- :: [a] -> Seq a
+ -- ** Sequential construction
+ iterate, -- :: Int -> (a -> a) -> a -> Seq a
+ unfoldr, -- :: (b -> Maybe (a, b)) -> b -> Seq a
-- * Deconstruction
-- | Additional functions for deconstructing sequences are available
-- via the 'Foldable' instance of 'Seq'.
hunk ./Data/Sequence.hs 64
scanl1, -- :: (a -> a -> a) -> Seq a -> Seq a
scanr, -- :: (a -> b -> b) -> b -> Seq a -> Seq b
scanr1, -- :: (a -> a -> a) -> Seq a -> Seq a
+ -- ** Sublists
+ tails, -- :: Seq a -> Seq (Seq a)
+ inits, -- :: Seq a -> Seq (Seq a)
+ takeWhile, -- :: (a -> Bool) -> Seq a -> Seq a
+ dropWhile, -- :: (a -> Bool) -> Seq a -> Seq a
+ span, -- :: (a -> Bool) -> Seq a -> (Seq a, Seq a)
+ break, -- :: (a -> Bool) -> Seq a -> (Seq a, Seq a)
+ partition, -- :: (a -> Bool) -> Seq a -> (Seq a, Seq a)
-- ** Indexing
index, -- :: Seq a -> Int -> a
adjust, -- :: (a -> a) -> Int -> Seq a -> Seq a
hunk ./Data/Sequence.hs 94
) where
import Prelude hiding (
- null, length, take, drop, splitAt, foldl, foldl1, foldr, foldr1,
- scanl, scanl1, scanr, scanr1,
- replicate,
- zip, zipWith, zip3, zipWith3,
- reverse)
+ null, length, take, drop, splitAt, foldl, foldl1, foldr, foldr1, span,
+ scanl, scanl1, scanr, scanr1, replicate, zip, zipWith, zip3, zipWith3,
+ takeWhile, dropWhile, break, iterate, reverse)
import qualified Data.List (foldl')
import Control.Applicative (Applicative(..), (<$>))
import Control.Monad (MonadPlus(..))
hunk ./Data/Sequence.hs 801
snocDigitToTree (Deep n pr m (Four a b c d)) (Four x y z w)
= Deep (n + 4) pr (m `snocDigitToTree` Two (node3 a b c) (node3 d x y)) (Two z w)
+-- | Builds a sequence from a seed value. Takes time linear in the number of generated elements. /WARNING: If the number of generated elements is infinite, this method will not terminate./
+unfoldr :: (b -> Maybe (a, b)) -> b -> Seq a
+unfoldr f b = unfoldr' empty b where
+ -- uses tail recursion rather than, for instance, the List implementation.
+ unfoldr' as b = case f b of
+ Nothing -> as
+ Just (a, b') -> unfoldr' (as |> a) b'
+
+-- | /O(n)/. Constructs a sequence by repeated application of a function to a seed value.
+--
+-- > iterate n f x = fromList (Prelude.take n (Prelude.iterate f x))
+iterate :: Int -> (a -> a) -> a -> Seq a
+-- borrows the structure of the sequence from replicate and preserves it with mapAccumL
+iterate n f x = snd (mapAccumL iterate' x (replicate n ())) where
+ iterate' y _ = let y' = f y in (y', y')
+
------------------------------------------------------------------------
-- Deconstruction
------------------------------------------------------------------------
hunk ./Data/Sequence.hs 1143
splitAt i (Seq xs) = (Seq l, Seq r)
where (l, r) = split i xs
+-- | /O(n)/. Returns a sequence of all suffixes of this sequence, longest first. For example,
+--
+-- > tails (fromList "abc") = fromList [fromList "abc", fromList "bc", fromList "c", fromList ""]
+--
+-- The suffixes are computed lazily from left to right.
+tails :: Seq a -> Seq (Seq a)
+tails xs = xs <| snd (mapAccumL tails' xs xs) where
+ -- By using mapAccumL, we keep the structure of xs for free, without having to do any cons operations.
+ -- We simply ignore the values coming from xs.
+ tails' ys _ = case viewl ys of
+ _ :< ys' -> (ys', ys')
+ _ -> error "Invariant failure in Data.Sequence.tails" -- should never happen
+
+-- | /O(n)/. Returns a sequence of all prefixes of this sequence, shortest first. For example,
+--
+-- > inits (fromList "abc") = fromList [fromList "", fromList "a", fromList "ab", fromList "abc"]
+--
+-- The prefixes are computed lazily from left to right.
+inits :: Seq a -> Seq (Seq a)
+inits xs = empty <| snd (mapAccumL inits' empty xs) where
+ inits' ys y = let ys' = ys |> y in (ys', ys')
+
split :: Int -> FingerTree (Elem a) ->
(FingerTree (Elem a), FingerTree (Elem a))
split i Empty = i `seq` (Empty, Empty)
hunk ./Data/Sequence.hs 1261
sab = sa + size b
sabc = sab + size c
+-- | /O(i)/ where /i/ is the breakpoint index. 'takeWhile', applied to a predicate @p@ and a sequence @xs@, returns the longest prefix (possibly empty) of @xs@ of elements that satisfy @p@.
+takeWhile :: (a -> Bool) -> Seq a -> Seq a
+takeWhile p xs = fst (span p xs)
+
+-- | /O(i)/ where /i/ is the breakpoint index. @'dropWhile' p xs@ returns the suffix remaining after @takeWhile p xs@.
+dropWhile :: (a -> Bool) -> Seq a -> Seq a
+dropWhile p xs = snd (span p xs)
+
+-- | /O(i)/ where /i/ is the breakpoint index. 'span', applied to a predicate @p@ and a sequence @xs@, returns a tuple whose first element is the longest prefix (possibly empty) of @xs@ of elements that satisfy @p@ and the second element is the remainder of the sequence.
+span :: (a -> Bool) -> Seq a -> (Seq a, Seq a)
+span p xs = splitAt ix xs
+ where indexed = snd (mapAccumL (\ i x -> (i + 1, (x, i))) 0 xs)
+ ix = foldr (\ (x, i) i' -> if p x then i' else i) (length xs) indexed
+
+-- | /O(i)/ where /i/ is the breakpoint index. 'break', applied to a predicate @p@ and a sequence @xs@, returns a tuple whose first element is the longest prefix (possibly empty) of @xs@ of elements that /do not satisfy/ @p@ and the second element is the remainder of the sequence.
+break :: (a -> Bool) -> Seq a -> (Seq a, Seq a)
+break p xs = span (not . p) xs
+
+-- | /O(n)/. The 'partition' function takes a predicate @p@ and a sequence @xs@ and returns sequences of those elements which do and do not satisfy the predicate.
+partition :: (a -> Bool) -> Seq a -> (Seq a, Seq a)
+partition p (Seq xs) = case partitionTree (\ (Elem x) -> p x) xs of
+ (xsT, xsF) -> (Seq xsT, Seq xsF)
+
+partitionTree :: (Elem a -> Bool) -> FingerTree (Elem a) -> (FingerTree (Elem a), FingerTree (Elem a))
+partitionTree _ Empty = (Empty, Empty)
+partitionTree p (Single x)
+ | p x = (Single x, Empty)
+ | otherwise = (Empty, Single x)
+partitionTree p (Deep _ pr m sf) =
+ let (prT, prF) = partitionDigit p pr
+ (sfT, sfF) = partitionDigit p sf in case partitionTree p (pull m) of
+ (mT, mF) -> (combine prT mT sfT, combine prF mF sfF)
+ where pull :: FingerTree (Node (Elem a)) -> FingerTree (Elem a)
+ pull t = case viewLTree t of
+ Nothing2 -> Empty
+ Just2 l t' -> case viewRTree t' of
+ Nothing2 -> digitToTree (nodeToDigit l)
+ Just2 t'' r -> Deep (size t) (nodeToDigit l) t'' (nodeToDigit r)
+ combine :: Sized a => Maybe (Digit a) -> FingerTree a -> Maybe (Digit a) -> FingerTree a
+ combine pr m sf = maybe id consDigitToTree pr $ (maybe m (m `snocDigitToTree`) sf)
+
+partitionDigit :: (a -> Bool) -> Digit a -> (Maybe (Digit a), Maybe (Digit a))
+partitionDigit p (One a) = case (p a) of
+ (False) -> (Nothing, Just (One a))
+ (True) -> (Just (One a), Nothing)
+partitionDigit p (Two a b) = case (p a, p b) of
+ (False, False) -> (Nothing, Just (Two a b))
+ (False, True) -> (Just (One b), Just (One a))
+ (True, False) -> (Just (One a), Just (One b))
+ (True, True) -> (Just (Two a b), Nothing)
+partitionDigit p (Three a b c) = case (p a, p b, p c) of
+ (False, False, False) -> (Nothing, Just (Three a b c))
+ (False, False, True) -> (Just (One c), Just (Two a b))
+ (False, True, False) -> (Just (One b), Just (Two a c))
+ (False, True, True) -> (Just (Two b c), Just (One a))
+ (True, False, False) -> (Just (One a), Just (Two b c))
+ (True, False, True) -> (Just (Two a c), Just (One b))
+ (True, True, False) -> (Just (Two a b), Just (One c))
+ (True, True, True) -> (Just (Three a b c), Nothing)
+partitionDigit p (Four a b c d) = case (p a, p b, p c, p d) of
+ (False, False, False, False) -> (Nothing, Just (Four a b c d))
+ (False, False, False, True) -> (Just (One d), Just (Three a b c))
+ (False, False, True, False) -> (Just (One c), Just (Three a b d))
+ (False, False, True, True) -> (Just (Two c d), Just (Two a b))
+ (False, True, False, False) -> (Just (One b), Just (Three a c d))
+ (False, True, False, True) -> (Just (Two b d), Just (Two a c))
+ (False, True, True, False) -> (Just (Two b c), Just (Two a d))
+ (False, True, True, True) -> (Just (Three b c d), Just (One a))
+ (True, False, False, False) -> (Just (One a), Just (Three b c d))
+ (True, False, False, True) -> (Just (Two a d), Just (Two b c))
+ (True, False, True, False) -> (Just (Two a c), Just (Two b d))
+ (True, False, True, True) -> (Just (Three a c d), Just (One b))
+ (True, True, False, False) -> (Just (Two a b), Just (Two c d))
+ (True, True, False, True) -> (Just (Three a b d), Just (One c))
+ (True, True, True, False) -> (Just (Three a b c), Just (One d))
+ (True, True, True, True) -> (Just (Four a b c d), Nothing)
+
------------------------------------------------------------------------
-- Lists
------------------------------------------------------------------------
}
Context:
[New methods for Data.Sequence
wasserman.louis@gmail.com**20090601192920
Ignore-this: b975d876022f0788997319dce83dbc93
This patch includes several new methods for Data.Sequence. Scan and replicate methods analogous to their List versions have been included, and most importantly there are fast zip functions, which run considerably faster than the workaround of converting to lists and zipping those.
]
[Use left/right rather than old/new to describe the arguments to unionWithKey
Ian Lynagh **20090208192132
Fixes trac #3002.
]
[help nhc98 by making import decl more explicit
Malcolm.Wallace@cs.york.ac.uk**20090203142144]
[Add instance Data.Traversable for IntMap
Matti Niemenmaa **20090116190353
Ignore-this: df88a286935926aecec3f8a5dd291699
]
[Require Cabal version >= 1.6
Ian Lynagh **20090122011256]
[Add "bug-reports" and "source-repository" info to the Cabal file
Ian Lynagh **20090121182106]
[Fix warnings in containers
Ian Lynagh **20090116200251]
[optimize IntMap/IntSet findMin/findMax
sedillard@gmail.com**20081002152055]
[O(n) fromAscList IntSet / IntMap
sedillard@gmail.com**20080521195941
Added algorithm by Scott Dillard and Bertram Felgenhauer to build IntSets and
IntMaps from sorted input in linear time. Also changed quickcheck prop_Ordered
(no longer a tautology!) to include negative and duplicate keys.
]
[correct type for IntMap.intersectionWith[Key]
sedillard@gmail.com**20081002144828]
[Export mapAccumRWithKey from Map and IntMap (Trac #2769)
matti.niemenmaa+darcs@iki.fi**20081210160205]
[Bump the version number to 0.2.0.1, to work-around cabal-install problems
Ian Lynagh **20081212201829]
[Fix #2760: change mkNorepType to mkNoRepType
'Jose Pedro Magalhaes '**20081202083424]
[Doc fix, from hackage trac #378
Ian Lynagh **20081024143949]
[import Data.Data instead of Data.Generics.*, eliminating the dependency on syb
Ross Paterson **20081005002559]
[fixed typo in highestBitMask
sedillard@gmail.com**20081002215438]
[export Data.Map.toDescList, foldlWithKey, and foldrWithKey (trac ticket 2580)
qdunkan@gmail.com**20080922213200
toDescList was previously implemented, but not exported.
foldlWithKey was previously implemented, but not exported. It can be used to
implement toDescList.
foldrWithKey is already exported as foldWithKey, but foldrWithKey is explicitly
the mirror of foldlWithKey, and foldWithKey kept for compatibility.
]
[Bump version number to 0.2.0.0
Ian Lynagh **20080920160016]
[TAG 6.10 branch has been forked
Ian Lynagh **20080919123438]
Patch bundle hash:
e1955d8836e9a7b3ac4b5d743aa240252ab56f47