Skip to content

Commit c9d1e69

Browse files
committed
Implement stimes for sequences
Implement `stimes` for sequences using `cycleNTimes`. This makes it work when the argument is 0 (unlike the default). `cycleNTimes` is faster and lazier than `stimesMonoid` because it takes advantage of the finger tree structure of sequences. Closes #618
1 parent 8f42a47 commit c9d1e69

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

Data/Sequence/Internal.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,7 @@ instance Monoid (Seq a) where
891891
-- | @since 0.5.7
892892
instance Semigroup.Semigroup (Seq a) where
893893
(<>) = (><)
894+
stimes = cycleNTimes . fromIntegral
894895
#endif
895896

896897
INSTANCE_TYPEABLE1(Seq)

changelog.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
# Changelog for [`containers` package](http://github.com/haskell/containers)
22

3-
## 0.6.0.2
3+
## 0.6.0.2?
44

55
* Fix Foldable instance for IntMap, which previously placed positively
66
keyed entries before negatively keyed ones for `fold`, `foldMap`, and
77
`traverse`.
88

9+
* Make `stimes` for sequences work with 0 arguments, and make it more
10+
efficient.
11+
912
## 0.6.0.1
1013

1114
* Released with GHC 8.6

tests/seq-properties.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import Data.Functor ((<$>), (<$))
2525
import Data.Maybe
2626
import Data.Function (on)
2727
import Data.Monoid (Monoid(..), All(..), Endo(..), Dual(..))
28+
#if MIN_VERSION_base(4,9,0)
29+
import Data.Semigroup (stimes, stimesMonoid)
30+
#endif
2831
import Data.Traversable (Traversable(traverse), sequenceA)
2932
import Prelude hiding (
3033
lookup, null, length, take, drop, splitAt,
@@ -151,6 +154,9 @@ main = defaultMain
151154
, testProperty "Left view constructor" prop_viewl_con
152155
, testProperty "Right view pattern" prop_viewr_pat
153156
, testProperty "Right view constructor" prop_viewr_con
157+
#endif
158+
#if MIN_VERSION_base(4,9,0)
159+
, testProperty "stimes" prop_stimes
154160
#endif
155161
]
156162

@@ -878,6 +884,14 @@ prop_bind :: Seq A -> Fun A (Seq B) -> Bool
878884
prop_bind xs (Fun _ f) =
879885
toList' (xs >>= f) ~= (toList xs >>= toList . f)
880886

887+
-- Semigroup operations
888+
889+
#if MIN_VERSION_base(4,9,0)
890+
prop_stimes :: NonNegative Int -> Seq A -> Property
891+
prop_stimes (NonNegative n) s =
892+
stimes n s === stimesMonoid n s
893+
#endif
894+
881895
-- MonadFix operation
882896

883897
-- It's exceedingly difficult to construct a proper QuickCheck

0 commit comments

Comments
 (0)