on master help sign in
Refactor, rename, reorg code, update docs
d5a8ebeHarendra Kumar
dev/container-api.md+13
@@ -85,6 +85,19 @@ the following reasons:
85 correctly. Ideally, when we know the elements then former is more
85 correctly. Ideally, when we know the elements then former is more
86 suitable while if we know the count then the latter is more suitable.
86 suitable while if we know the count then the latter is more suitable.
87
87
88+Note that in case of flaoting point numbers `FromStep` style may have an
89+advantage over `FromThen` style. Here is a quote from the documentation of
90+`enumerateFromThenNum`:
91+
92+```
93+Note that in the strange world of floating point numbers, using
94+@enumerateFromThenNum (from, from + 1)@ is almost exactly the same as
95+@enumerateFromStepNum (from, 1) but not precisely the same. Because @(from
96++ 1) - from@ is not exactly 1, it may lose some precision, the loss may
97+also be aggregated in each step, if you want that precision then use
98+'enumerateFromStepNum' instead.
99+```
100+
88### Appending
101### Appending
89
102
90* append (for mutable arrays)
103* append (for mutable arrays)
src/Streamly/Internal/Data/Unfold.hs+2−2
@@ -160,8 +160,8 @@ module Streamly.Internal.Data.Unfold
160 , enumerateFromThenToIntegral
160 , enumerateFromThenToIntegral
161
161
162 -- ** Enumerating 'Small Integral' Types
162 -- ** Enumerating 'Small Integral' Types
163− , enumerateFromSmall
163+ , enumerateFromSmallBounded
164− , enumerateFromThenSmall
164+ , enumerateFromThenSmallBounded
165 , enumerateFromToSmall
165 , enumerateFromToSmall
166 , enumerateFromThenToSmall
166 , enumerateFromThenToSmall
167
167
src/Streamly/Internal/Data/Unfold/Enumeration.hs+233−194
@@ -23,29 +23,38 @@ module Streamly.Internal.Data.Unfold.Enumeration
23 (
23 (
24 Enumerable (..)
24 Enumerable (..)
25
25
26− -- ** Enumerate 'Num' Types
26+ -- ** Enumerating 'Num' Types
27+ , enumerateFromStepNum
27 , enumerateFromNum
28 , enumerateFromNum
28 , enumerateFromThenNum
29 , enumerateFromThenNum
30+ , enumerateFromToNum
31+ , enumerateFromThenToNum
29
32
30− -- ** Enumerating 'Bounded Integral' Types
33+ -- ** Enumerating unbounded 'Integral' Types
31− , enumerateFromIntegralBounded
34+ , enumerateFromStepIntegral
32− , enumerateFromThenIntegralBounded
33− , enumerateFromToIntegralBounded
34− , enumerateFromThenToIntegralBounded
35−
36− -- ** Enumerating 'Unbounded Integral' Types
37 , enumerateFromIntegral
35 , enumerateFromIntegral
38 , enumerateFromThenIntegral
36 , enumerateFromThenIntegral
37+
38+ , enumerateFromToStepIntegral
39 , enumerateFromToIntegral
39 , enumerateFromToIntegral
40 , enumerateFromThenToIntegral
40 , enumerateFromThenToIntegral
41
41
42− -- ** Enumerating 'Small Integral' Types
42+ -- ** Enumerating 'Bounded' 'Integral' Types
43− , enumerateFromSmall
43+ , enumerateFromStepIntegralBounded
44− , enumerateFromThenSmall
44+ , enumerateFromIntegralBounded
45+ , enumerateFromThenIntegralBounded
46+ , enumerateFromToIntegralBounded
47+ , enumerateFromThenToIntegralBounded
48+
49+ -- ** Enumerating small 'Integral' Types
50+ -- | Small types are always bounded.
51+ , enumerateFromSmallBounded
52+ , enumerateFromThenSmallBounded
45 , enumerateFromToSmall
53 , enumerateFromToSmall
46 , enumerateFromThenToSmall
54 , enumerateFromThenToSmall
47
55
48 -- ** Enumerating 'Fractional' Types
56 -- ** Enumerating 'Fractional' Types
57+ -- | Enumeration of 'Num' specialized to 'Fractional' types.
49 , enumerateFromFractional
58 , enumerateFromFractional
50 , enumerateFromThenFractional
59 , enumerateFromThenFractional
51 , enumerateFromToFractional
60 , enumerateFromToFractional
@@ -67,22 +76,22 @@ import Prelude
67 hiding (map, mapM, takeWhile, take, filter, const, zipWith
76 hiding (map, mapM, takeWhile, take, filter, const, zipWith
68 , drop, dropWhile)
77 , drop, dropWhile)
69
78
70−------------------------------------------------------------------------------
71−-- Enumeration of Num
72−------------------------------------------------------------------------------
73−
74−-- | Unfolds @(from, next)@ generating an infinite stream starting from @from@
75−-- and @next@ values with a stride of @(next - from)@. After the value
76−-- overflows it keeps enumerating in a cycle:
77−--
78-- $setup
79-- $setup
79-- >>> :m
80-- >>> :m
80-- >>> import qualified Streamly.Prelude as Stream
81-- >>> import qualified Streamly.Prelude as Stream
81-- >>> import qualified Streamly.Internal.Data.Unfold as Unfold
82-- >>> import qualified Streamly.Internal.Data.Unfold as Unfold
82-- >>> import Streamly.Internal.Data.Unfold.Type
83-- >>> import Streamly.Internal.Data.Unfold.Type
83-- >>> import Data.Word
84-- >>> import Data.Word
85+
86+------------------------------------------------------------------------------
87+-- Enumeration of Num
88+------------------------------------------------------------------------------
89+
90+-- | Unfolds @(from, stride)@ generating an infinite stream starting from
91+-- @from@ and incrementing every time by @stride@. For 'Bounded' types, after
92+-- the value overflows it keeps enumerating in a cycle:
84--
93--
85−-- >>> Stream.toList $ Stream.take 10 $ Stream.unfold Unfold.enumerateFromThenNum (255::Word8,0)
94+-- >>> Stream.toList $ Stream.take 10 $ Stream.unfold Unfold.enumerateFromStepNum (255::Word8,1)
86-- [255,0,1,2,3,4,5,6,7,8]
95-- [255,0,1,2,3,4,5,6,7,8]
87--
96--
88-- The implementation is numerically stable for floating point values.
97-- The implementation is numerically stable for floating point values.
@@ -91,13 +100,13 @@ import Prelude
91--
100--
92-- /Internal/
101-- /Internal/
93--
102--
94−{-# INLINE enumerateFromThenNum #-}
103+{-# INLINE enumerateFromStepNum #-}
95−enumerateFromThenNum :: (Monad m, Num a) => Unfold m (a, a) a
104+enumerateFromStepNum :: (Monad m, Num a) => Unfold m (a, a) a
96−enumerateFromThenNum = Unfold step inject
105+enumerateFromStepNum = Unfold step inject
97
106
98 where
107 where
99
108
100− inject (!from, !next) = return (from, next - from, 0)
109+ inject (!from, !stride) = return (from, stride, 0)
101
110
102 -- Note that the counter "i" is the same type as the type being enumerated.
111 -- Note that the counter "i" is the same type as the type being enumerated.
103 -- It may overflow, for example, if we are enumerating Word8, after 255 the
112 -- It may overflow, for example, if we are enumerating Word8, after 255 the
@@ -108,42 +117,61 @@ enumerateFromThenNum = Unfold step inject
108 return $
117 return $
109 (Yield $! (from + i * stride)) $! (from, stride, i + 1)
118 (Yield $! (from + i * stride)) $! (from, stride, i + 1)
110
119
120+-- | Same as 'enumerateFromStepNum (from, next)' using a stride of @next - from@:
121+--
122+-- >>> enumerateFromThenNum =
123+-- lmap (\(from, next) -> (from, next - from)) enumerateFromStepNum
124+--
125+-- Example:
126+--
127+-- >>> Stream.toList $ Stream.take 10 $ Stream.unfold Unfold.enumerateFromThenNum (255::Word8,0)
128+-- [255,0,1,2,3,4,5,6,7,8]
129+--
130+-- The implementation is numerically stable for floating point values.
131+--
132+-- Note that 'enumerateFromThenIntegral' is faster for integrals.
133+--
134+-- Note that in the strange world of floating point numbers, using
135+-- @enumerateFromThenNum (from, from + 1)@ is almost exactly the same as
136+-- @enumerateFromStepNum (from, 1) but not precisely the same. Because @(from +
137+-- 1) - from@ is not exactly 1, it may lose some precision, the loss may also
138+-- be aggregated in each step, if you want that precision then use
139+-- 'enumerateFromStepNum' instead.
140+--
141+-- /Internal/
142+--
143+{-# INLINE enumerateFromThenNum #-}
144+enumerateFromThenNum :: (Monad m, Num a) => Unfold m (a, a) a
111−-- | Same as 'enumerateFromThenNum' using a stride of 1.
145+enumerateFromThenNum =
146+ lmap (\(from, next) -> (from, next - from)) enumerateFromStepNum
147+
148+-- | Same as 'enumerateFromStepNum' using a stride of 1:
149+--
150+-- >>> enumerateFromNum = lmap (\from -> (from, 1)) enumerateFromStepNum
112--
151--
152+-- Also, same as 'enumerateFromThenNum' using a stride of 1 but see the note in
153+-- 'enumerateFromThenNum' about the loss of precision:
154+--
113−-- >> enumerateFromNum = lmap (\from -> (from, from + 1)) enumerateFromThenNum
155+-- >>> enumerateFromNum = lmap (\from -> (from, from + 1)) enumerateFromThenNum
114--
156--
115-- /Internal/
157-- /Internal/
116--
158--
117{-# INLINE enumerateFromNum #-}
159{-# INLINE enumerateFromNum #-}
118enumerateFromNum :: (Monad m, Num a) => Unfold m a a
160enumerateFromNum :: (Monad m, Num a) => Unfold m a a
119−enumerateFromNum = Unfold step inject
161+enumerateFromNum = lmap (\from -> (from, 1)) enumerateFromStepNum
120−
121−-- XXX It loses the precisions if we use
122−-- enumerateFromNum = lmap (\from -> (from, from + 1)) enumerateFromThenNum
123−-- Example ‘S.unfold (UF.take 5 $ UF.enumerateFromFractional) 3.9’
124−-- [3.9,4.9,5.9,6.900000000000001,7.900000000000002]
125−
126− where
127−
128− inject !from = return (from, 1, 0)
129−
130− {-# INLINE_LATE step #-}
131− step (from, stride, i) =
132− return $
133− (Yield $! (from + i * stride)) $! (from, stride, i + 1)
134
162
135-- | Generate a finite stream starting from a from and next values with
163-- | Generate a finite stream starting from a from and next values with
136−-- stride of (next-from) till to value. The implementation is numerically
164+-- stride of (next-from) up to value. The implementation is numerically
137-- stable for floating point values.
165-- stable for floating point values.
138--
166--
139−-- Note 'enumerateFromStepIntegral' is faster for integrals.
167+-- Note that 'enumerateFromThenToIntegral' is faster for integrals.
140--
168--
141-- /Internal/
169-- /Internal/
142--
170--
143−{-# INLINE enumerateFromThenToNumBounded #-}
171+{-# INLINE enumerateFromThenToNum #-}
144−enumerateFromThenToNumBounded :: (Monad m, Ord a, Fractional a) =>
172+enumerateFromThenToNum :: (Monad m, Ord a, Fractional a) =>
145 Unfold m (a, a, a) a
173 Unfold m (a, a, a) a
146−enumerateFromThenToNumBounded = Unfold step inject
174+enumerateFromThenToNum = Unfold step inject
147
175
148 where
176 where
149
177
@@ -168,14 +196,14 @@ enumerateFromThenToNumBounded = Unfold step inject
168-- stride of value 1 will be used. The implementation is numerically
196-- stride of value 1 will be used. The implementation is numerically
169-- stable for floating point values.
197-- stable for floating point values.
170--
198--
171−-- Note 'enumerateFromStepIntegralBounded' is faster for integrals.
199+-- Note that 'enumerateFromToIntegral' is faster for integrals.
172--
200--
173-- /Internal/
201-- /Internal/
174--
202--
175−{-# INLINE enumerateFromToNumBounded #-}
203+{-# INLINE enumerateFromToNum #-}
176−enumerateFromToNumBounded :: (Monad m, Ord a, Fractional a) =>
204+enumerateFromToNum :: (Monad m, Ord a, Fractional a) =>
177 Unfold m (a, a) a
205 Unfold m (a, a) a
178−enumerateFromToNumBounded = Unfold step inject
206+enumerateFromToNum = Unfold step inject
179
207
180 where
208 where
181
209
@@ -203,6 +231,39 @@ enumerateFromStepIntegral = Unfold step inject
203 {-# INLINE_LATE step #-}
231 {-# INLINE_LATE step #-}
204 step (x, stride) = return $ Yield x $! (x + stride, stride)
232 step (x, stride) = return $ Yield x $! (x + stride, stride)
205
233
234+-- | Can be used to enumerate unbounded integrals. Starting with value from
235+-- with stride of next-from till value of to.
236+--
237+-- /Internal/
238+--
239+{-# INLINE_NORMAL enumerateFromToStepIntegral #-}
240+enumerateFromToStepIntegral :: (Integral a, Monad m) =>
241+ Unfold m (a, a, a) a
242+enumerateFromToStepIntegral = Unfold step inject
243+ where
244+ inject (from, next, to) =
245+ from `seq` next `seq` to `seq` return (from, next - from, to)
246+ {-# INLINE_LATE step #-}
247+ step (x, stride, to) = return $
248+ if stride == 0
249+ then
250+ if x <= to then Yield x $! (x, stride, to) else Stop
251+
252+ else
253+ if stride > 0
254+ then
255+ if x <= to
256+ then Yield x $! (x + stride, stride, to)
257+ else Stop
258+ else
259+ if x >= to
260+ then Yield x $! (x + stride, stride, to)
261+ else Stop
262+
263+------------------------------------------------------------------------------
264+-- Enumeration of Bounded Integrals
265+------------------------------------------------------------------------------
266+
206-- | Can be used to enumerate bounded integrals from starting from with
267-- | Can be used to enumerate bounded integrals from starting from with
207-- stride of (next-from) . This check for overflow or underflow for
268-- stride of (next-from) . This check for overflow or underflow for
208-- bounded integrals.
269-- bounded integrals.
@@ -265,35 +326,6 @@ enumerateFromToStepIntegralBounded = Unfold step inject
265 then Yield x $! (x + stride, stride, to)
326 then Yield x $! (x + stride, stride, to)
266 else Stop
327 else Stop
267
328
268−-- | Can be used to enumerate unbounded integrals. Startting with value from
269−-- with stride of next-from till value of to.
270−--
271−-- /Internal/
272−--
273−{-# INLINE_NORMAL enumerateFromToStepIntegral #-}
274−enumerateFromToStepIntegral :: (Integral a, Monad m) =>
275− Unfold m (a, a, a) a
276−enumerateFromToStepIntegral = Unfold step inject
277− where
278− inject (from, next, to) =
279− from `seq` next `seq` to `seq` return (from, next - from, to)
280− {-# INLINE_LATE step #-}
281− step (x, stride, to) = return $
282− if stride == 0
283− then
284− if x <= to then Yield x $! (x, stride, to) else Stop
285−
286− else
287− if stride > 0
288− then
289− if x <= to
290− then Yield x $! (x + stride, stride, to)
291− else Stop
292− else
293− if x >= to
294− then Yield x $! (x + stride, stride, to)
295− else Stop
296−
297-- Enumerate Unbounded Integrals ----------------------------------------------
329-- Enumerate Unbounded Integrals ----------------------------------------------
298{-# INLINE enumerateFromIntegral #-}
330{-# INLINE enumerateFromIntegral #-}
299enumerateFromIntegral :: (Monad m, Integral a) => Unfold m a a
331enumerateFromIntegral :: (Monad m, Integral a) => Unfold m a a
@@ -340,7 +372,7 @@ enumerateFromThenToIntegralBounded = enumerateFromToStepIntegralBounded
340
372
341{-# INLINE_NORMAL enumerateFromFractional #-}
373{-# INLINE_NORMAL enumerateFromFractional #-}
342enumerateFromFractional :: (Monad m, Fractional a) => Unfold m a a
374enumerateFromFractional :: (Monad m, Fractional a) => Unfold m a a
343−enumerateFromFractional = enumerateFromNum
375+enumerateFromFractional = enumerateFromNum
344
376
345{-# INLINE_NORMAL enumerateFromThenFractional #-}
377{-# INLINE_NORMAL enumerateFromThenFractional #-}
346enumerateFromThenFractional :: (Monad m, Fractional a) => Unfold m (a, a) a
378enumerateFromThenFractional :: (Monad m, Fractional a) => Unfold m (a, a) a
@@ -349,12 +381,128 @@ enumerateFromThenFractional = enumerateFromThenNum
349{-# INLINE_NORMAL enumerateFromToFractional #-}
381{-# INLINE_NORMAL enumerateFromToFractional #-}
350enumerateFromToFractional :: (Monad m, Fractional a, Ord a) =>
382enumerateFromToFractional :: (Monad m, Fractional a, Ord a) =>
351 Unfold m (a, a) a
383 Unfold m (a, a) a
352−enumerateFromToFractional = enumerateFromToNumBounded
384+enumerateFromToFractional = enumerateFromToNum
353
385
354{-# INLINE_NORMAL enumerateFromThenToFractional #-}
386{-# INLINE_NORMAL enumerateFromThenToFractional #-}
355−enumerateFromThenToFractional :: (Monad m, Fractional a, Ord a) =>
387+enumerateFromThenToFractional :: (Monad m, Fractional a, Ord a) =>
356 Unfold m (a, a, a) a
388 Unfold m (a, a, a) a
357−enumerateFromThenToFractional = enumerateFromThenToNumBounded
389+enumerateFromThenToFractional = enumerateFromThenToNum
390+
391+-------------------------------------------------------------------------------
392+-- Enumeration of Enum types not larger than Int
393+-------------------------------------------------------------------------------
394+
395+-- XXX should we check for bounds here? Otherwise it will cycle back to "to" if
396+-- to is smaller?
397+--
398+-- | Enumerate from given starting Enum value 'from' and to Enum value 'to'
399+-- with stride of 1 till to value.
400+--
401+-- /Internal/
402+--
403+{-# INLINE enumerateFromToSmall #-}
404+enumerateFromToSmall :: (Monad m, Enum a) => Unfold m (a, a) a
405+enumerateFromToSmall = map toEnum $
406+ Unfold step inject
407+ where
408+ inject (from, to) =
409+ fromEnum from
410+ `seq` fromEnum to
411+ `seq` return (fromEnum from, 1, fromEnum to)
412+ {-# INLINE_LATE step #-}
413+ step (x, stride, to) = return $
414+ if x <= to then Yield x $! (x + stride, stride, to) else Stop
415+
416+-- | Enumerate from given starting Enum value 'from' and then Enum value 'next'
417+-- and to Enum value 'to' with stride of (fromEnum next - fromEnum from)
418+-- till to value.
419+--
420+-- /Internal/
421+--
422+{-# INLINE enumerateFromThenToSmall #-}
423+enumerateFromThenToSmall :: (Monad m, Enum a) => Unfold m (a, a, a) a
424+enumerateFromThenToSmall = map toEnum $
425+ Unfold step inject
426+ where
427+ inject (from, next, to) =
428+ fromEnum from
429+ `seq` fromEnum next
430+ `seq` fromEnum to
431+ `seq` return
432+ (fromEnum from, fromEnum next - fromEnum from, fromEnum to)
433+ {-# INLINE_LATE step #-}
434+ step (x, stride, to) = return $
435+ if stride == 0
436+ then
437+ if x <= to then Yield x $! (x, stride, to) else Stop
438+
439+ else
440+ if stride > 0
441+ then
442+ if x <= to
443+ then Yield x $! (x + stride, stride, to)
444+ else Stop
445+ else
446+ if x >= 0 && x >= to
447+ then Yield x $! (x + stride, stride, to)
448+ else Stop
449+
450+-------------------------------------------------------------------------------
451+-- Bounded Enumeration of Enum types not larger than Int
452+-------------------------------------------------------------------------------
453+
454+-- | Enumerate from given starting Enum value 'from' with stride of 1 till
455+-- maxBound
456+--
457+-- /Internal/
458+--
459+{-# INLINE enumerateFromSmallBounded #-}
460+enumerateFromSmallBounded :: forall a m. (Monad m, Bounded a, Enum a) => Unfold m a a
461+enumerateFromSmallBounded = map toEnum $
462+ Unfold step inject
463+ where
464+ inject from =
465+ fromEnum from
466+ `seq` return (fromEnum from, 1, fromEnum (maxBound :: a))
467+ {-# INLINE_LATE step #-}
468+ step (x, stride, to) = return $
469+ if x <= to then Yield x $! (x + stride, stride, to) else Stop
470+
471+-- | Enumerate from given starting Enum value 'from' and next Enum value 'next'
472+-- with stride of (fromEnum next - fromEnum from) till maxBound.
473+--
474+-- /Internal/
475+--
476+{-# INLINE enumerateFromThenSmallBounded #-}
477+enumerateFromThenSmallBounded :: forall a m. (Monad m, Bounded a, Enum a) =>
478+ Unfold m (a, a) a
479+enumerateFromThenSmallBounded = map toEnum $
480+ Unfold step inject
481+ where
482+ inject (from, next) =
483+ fromEnum from
484+ `seq` fromEnum next
485+ `seq` return
486+ ( fromEnum from
487+ , fromEnum next - fromEnum from
488+ , fromEnum (maxBound :: a)
489+ )
490+ {-# INLINE_LATE step #-}
491+ step (x, stride, to) = return $
492+ if stride == 0
493+ then
494+ if x <= to then Yield x $! (x, stride, to) else Stop
495+
496+ else
497+ if stride > 0
498+ then
499+ if x <= to
500+ then Yield x $! (x + stride, stride, to)
501+ else Stop
502+ else
503+ if x >= 0
504+ then Yield x $! (x + stride, stride, to)
505+ else Stop
358
506
359-------------------------------------------------------------------------------
507-------------------------------------------------------------------------------
360-- Enumerable type class
508-- Enumerable type class
@@ -463,115 +611,6 @@ class Enum a => Enumerable a where
463 -- /Pre-release/
611 -- /Pre-release/
464 enumerateFromThenTo :: Monad m => Unfold m (a, a, a) a
612 enumerateFromThenTo :: Monad m => Unfold m (a, a, a) a
465
613
466−-------------------------------------------------------------------------------
467−-- Enumeration of Enum types not larger than Int
468−-------------------------------------------------------------------------------
469−
470−-- | Enumerate from given starting Enum value 'from' with stride of 1 till
471−-- maxBound
472−--
473−-- /Internal/
474−--
475−{-# INLINE enumerateFromSmall #-}
476−enumerateFromSmall :: forall a m. (Monad m, Bounded a, Enum a) => Unfold m a a
477−enumerateFromSmall = map toEnum $
478− Unfold step inject
479− where
480− inject from =
481− fromEnum from
482− `seq` return (fromEnum from, 1, fromEnum (maxBound :: a))
483− {-# INLINE_LATE step #-}
484− step (x, stride, to) = return $
485− if x <= to then Yield x $! (x + stride, stride, to) else Stop
486−
487−-- | Enumerate from given starting Enum value 'from' and next Enum value 'next'
488−-- with stride of (fromEnum next - fromEnum from) till maxBound.
489−--
490−-- /Internal/
491−--
492−{-# INLINE enumerateFromThenSmall #-}
493−enumerateFromThenSmall :: forall a m. (Monad m, Bounded a, Enum a) =>
494− Unfold m (a, a) a
495−enumerateFromThenSmall = map toEnum $
496− Unfold step inject
497− where
498− inject (from, next) =
499− fromEnum from
500− `seq` fromEnum next
501− `seq` return
502− ( fromEnum from
503− , fromEnum next - fromEnum from
504− , fromEnum (maxBound :: a)
505− )
506− {-# INLINE_LATE step #-}
507− step (x, stride, to) = return $
508− if stride == 0
509− then
510− if x <= to then Yield x $! (x, stride, to) else Stop
511−
512− else
513− if stride > 0
514− then
515− if x <= to
516− then Yield x $! (x + stride, stride, to)
517− else Stop
518− else
519− if x >= 0
520− then Yield x $! (x + stride, stride, to)
521− else Stop
522−
523−-- | Enumerate from given starting Enum value 'from' and to Enum value 'to'
524−-- with stride of 1 till to value.
525−--
526−-- /Internal/
527−--
528−{-# INLINE enumerateFromToSmall #-}
529−enumerateFromToSmall :: (Monad m, Enum a) => Unfold m (a, a) a
530−enumerateFromToSmall = map toEnum $
531− Unfold step inject
532− where
533− inject (from, to) =
534− fromEnum from
535− `seq` fromEnum to
536− `seq` return (fromEnum from, 1, fromEnum to)
537− {-# INLINE_LATE step #-}
538− step (x, stride, to) = return $
539− if x <= to then Yield x $! (x + stride, stride, to) else Stop
540−
541−-- | Enumerate from given starting Enum value 'from' and then Enum value 'next'
542−-- and to Enum value 'to' with stride of (fromEnum next - fromEnum from)
543−-- till to value.
544−--
545−-- /Internal/
546−--
547−{-# INLINE enumerateFromThenToSmall #-}
548−enumerateFromThenToSmall :: (Monad m, Enum a) => Unfold m (a, a, a) a
549−enumerateFromThenToSmall = map toEnum $
550− Unfold step inject
551− where
552− inject (from, next, to) =
553− fromEnum from
554− `seq` fromEnum next
555− `seq` fromEnum to
556− `seq` return
557− (fromEnum from, fromEnum next - fromEnum from, fromEnum to)
558− {-# INLINE_LATE step #-}
559− step (x, stride, to) = return $
560− if stride == 0
561− then
562− if x <= to then Yield x $! (x, stride, to) else Stop
563−
564− else
565− if stride > 0
566− then
567− if x <= to
568− then Yield x $! (x + stride, stride, to)
569− else Stop
570− else
571− if x >= 0 && x >= to
572− then Yield x $! (x + stride, stride, to)
573− else Stop
574−
575-------------------------------------------------------------------------------
614-------------------------------------------------------------------------------
576-- Enumerable Instances
615-- Enumerable Instances
577-------------------------------------------------------------------------------
616-------------------------------------------------------------------------------
@@ -580,9 +619,9 @@ enumerateFromThenToSmall = map toEnum $
580#define ENUMERABLE_BOUNDED_SMALL(SMALL_TYPE) \
619#define ENUMERABLE_BOUNDED_SMALL(SMALL_TYPE) \
581instance Enumerable SMALL_TYPE where { \
620instance Enumerable SMALL_TYPE where { \
582 {-# INLINE enumerateFrom #-}; \
621 {-# INLINE enumerateFrom #-}; \
583− enumerateFrom = enumerateFromSmall; \
622+ enumerateFrom = enumerateFromSmallBounded; \
584 {-# INLINE enumerateFromThen #-}; \
623 {-# INLINE enumerateFromThen #-}; \
585− enumerateFromThen = enumerateFromThenSmall; \
624+ enumerateFromThen = enumerateFromThenSmallBounded; \
586 {-# INLINE enumerateFromTo #-}; \
625 {-# INLINE enumerateFromTo #-}; \
587 enumerateFromTo = enumerateFromToSmall; \
626 enumerateFromTo = enumerateFromToSmall; \
588 {-# INLINE enumerateFromThenTo #-}; \
627 {-# INLINE enumerateFromThenTo #-}; \
src/Streamly/Internal/Data/Unfold/Type.hs+2
@@ -35,6 +35,8 @@ module Streamly.Internal.Data.Unfold.Type
35 , supply
35 , supply
36 , supplyFirst
36 , supplyFirst
37 , supplySecond
37 , supplySecond
38+
39+ -- * Trimming
38 , takeWhileM
40 , takeWhileM
39 , takeWhile
41 , takeWhile
40
42
test/Streamly/Test/Data/Unfold.hs+32−32
@@ -281,19 +281,19 @@ enumerateFromThenToIntegralBounded =
281 in testUnfold unf (f :: Int, th, to) $
281 in testUnfold unf (f :: Int, th, to) $
282 Prelude.take 50 $ Prelude.enumFromThenTo f th to
282 Prelude.take 50 $ Prelude.enumFromThenTo f th to
283
283
284−enumerateFromSmall :: Property
284+enumerateFromSmallBounded :: Property
285−enumerateFromSmall =
285+enumerateFromSmallBounded =
286 property
286 property
287 $ \f ->
287 $ \f ->
288− let unf = UF.take 50 UF.enumerateFromSmall
288+ let unf = UF.take 50 UF.enumerateFromSmallBounded
289 in testUnfold unf (f :: Char) $
289 in testUnfold unf (f :: Char) $
290 Prelude.take 50 $ Prelude.enumFrom f
290 Prelude.take 50 $ Prelude.enumFrom f
291
291
292−enumerateFromThenSmall :: Property
292+enumerateFromThenSmallBounded :: Property
293−enumerateFromThenSmall =
293+enumerateFromThenSmallBounded =
294 property
294 property
295 $ \f th ->
295 $ \f th ->
296− let unf = UF.take 50 UF.enumerateFromThenSmall
296+ let unf = UF.take 50 UF.enumerateFromThenSmallBounded
297 in testUnfold unf (f :: Char, th) $
297 in testUnfold unf (f :: Char, th) $
298 Prelude.take 50 $ Prelude.enumFromThen f th
298 Prelude.take 50 $ Prelude.enumFromThen f th
299
299
@@ -313,19 +313,19 @@ enumerateFromThenToSmall =
313 in testUnfold unf (f :: Char, th, to) $
313 in testUnfold unf (f :: Char, th, to) $
314 Prelude.take 50 $ Prelude.enumFromThenTo f th to
314 Prelude.take 50 $ Prelude.enumFromThenTo f th to
315
315
316−enumerateFromSmallOrd :: Property
316+enumerateFromSmallBoundedOrd :: Property
317−enumerateFromSmallOrd =
317+enumerateFromSmallBoundedOrd =
318 property
318 property
319 $ \f ->
319 $ \f ->
320− let unf = UF.take 3 UF.enumerateFromSmall
320+ let unf = UF.take 3 UF.enumerateFromSmallBounded
321 in testUnfold unf (f :: Ordering) $
321 in testUnfold unf (f :: Ordering) $
322 Prelude.take 3 $ Prelude.enumFrom f
322 Prelude.take 3 $ Prelude.enumFrom f
323
323
324−enumerateFromThenSmallOrd :: Property
324+enumerateFromThenSmallBoundedOrd :: Property
325−enumerateFromThenSmallOrd =
325+enumerateFromThenSmallBoundedOrd =
326 property
326 property
327 $ \f th ->
327 $ \f th ->
328− let unf = UF.take 3 UF.enumerateFromThenSmall
328+ let unf = UF.take 3 UF.enumerateFromThenSmallBounded
329 in testUnfold unf (f :: Ordering, th) $
329 in testUnfold unf (f :: Ordering, th) $
330 Prelude.take 3 $ Prelude.enumFromThen f th
330 Prelude.take 3 $ Prelude.enumFromThen f th
331
331
@@ -346,19 +346,19 @@ enumerateFromThenToSmallOrd =
346 Prelude.take 3 $ Prelude.enumFromThenTo f th to
346 Prelude.take 3 $ Prelude.enumFromThenTo f th to
347
347
348-------------------------------------------------------------------------------
348-------------------------------------------------------------------------------
349−enumerateFromSmallBool :: Property
349+enumerateFromSmallBoundedBool :: Property
350−enumerateFromSmallBool =
350+enumerateFromSmallBoundedBool =
351 property
351 property
352 $ \f ->
352 $ \f ->
353− let unf = UF.take 2 UF.enumerateFromSmall
353+ let unf = UF.take 2 UF.enumerateFromSmallBounded
354 in testUnfold unf (f :: Bool) $
354 in testUnfold unf (f :: Bool) $
355 Prelude.take 2 $ Prelude.enumFrom f
355 Prelude.take 2 $ Prelude.enumFrom f
356
356
357−enumerateFromThenSmallBool :: Property
357+enumerateFromThenSmallBoundedBool :: Property
358−enumerateFromThenSmallBool =
358+enumerateFromThenSmallBoundedBool =
359 property
359 property
360 $ \f th ->
360 $ \f th ->
361− let unf = UF.take 2 UF.enumerateFromThenSmall
361+ let unf = UF.take 2 UF.enumerateFromThenSmallBounded
362 in testUnfold unf (f :: Bool, th) $
362 in testUnfold unf (f :: Bool, th) $
363 Prelude.take 2 $ Prelude.enumFromThen f th
363 Prelude.take 2 $ Prelude.enumFromThen f th
364
364
@@ -378,19 +378,19 @@ enumerateFromThenToSmallBool =
378 in testUnfold unf (f :: Bool, th, to) $
378 in testUnfold unf (f :: Bool, th, to) $
379 Prelude.take 2 $ Prelude.enumFromThenTo f th to
379 Prelude.take 2 $ Prelude.enumFromThenTo f th to
380-------------------------------------------------------------------------------
380-------------------------------------------------------------------------------
381−enumerateFromSmallUnit :: Property
381+enumerateFromSmallBoundedUnit :: Property
382−enumerateFromSmallUnit =
382+enumerateFromSmallBoundedUnit =
383 property
383 property
384 $ \f ->
384 $ \f ->
385− let unf = UF.take 1 UF.enumerateFromSmall
385+ let unf = UF.take 1 UF.enumerateFromSmallBounded
386 in testUnfold unf (f :: ()) $
386 in testUnfold unf (f :: ()) $
387 Prelude.take 1 $ Prelude.enumFrom f
387 Prelude.take 1 $ Prelude.enumFrom f
388
388
389−enumerateFromThenSmallUnit :: Property
389+enumerateFromThenSmallBoundedUnit :: Property
390−enumerateFromThenSmallUnit =
390+enumerateFromThenSmallBoundedUnit =
391 property
391 property
392 $ \f th ->
392 $ \f th ->
393− let unf = UF.take 1 UF.enumerateFromThenSmall
393+ let unf = UF.take 1 UF.enumerateFromThenSmallBounded
394 in testUnfold unf (f :: (), th) $
394 in testUnfold unf (f :: (), th) $
395 Prelude.take 1 $ Prelude.enumFromThen f th
395 Prelude.take 1 $ Prelude.enumFromThen f th
396
396
@@ -623,23 +623,23 @@ testGeneration =
623 prop "enumerateFromToIntegralBounded" enumerateFromToIntegralBounded
623 prop "enumerateFromToIntegralBounded" enumerateFromToIntegralBounded
624 prop "enumerateFromThenToIntegralBounded" enumerateFromThenToIntegralBounded
624 prop "enumerateFromThenToIntegralBounded" enumerateFromThenToIntegralBounded
625 ----------- Enumerate from Small Integral -------------------------
625 ----------- Enumerate from Small Integral -------------------------
626− prop "enumerateFromSmall" enumerateFromSmall
626+ prop "enumerateFromSmallBounded" enumerateFromSmallBounded
627− prop "enumerateFromThenSmall" enumerateFromThenSmall
627+ prop "enumerateFromThenSmallBounded" enumerateFromThenSmallBounded
628 prop "enumerateFromToSmall" enumerateFromToSmall
628 prop "enumerateFromToSmall" enumerateFromToSmall
629 prop "enumerateFromThenToSmall" enumerateFromThenToSmall
629 prop "enumerateFromThenToSmall" enumerateFromThenToSmall
630 --
630 --
631− prop "enumerateFromSmallOrd" enumerateFromSmallOrd
631+ prop "enumerateFromSmallBoundedOrd" enumerateFromSmallBoundedOrd
632− prop "enumerateFromThenSmallOrd" enumerateFromThenSmallOrd
632+ prop "enumerateFromThenSmallBoundedOrd" enumerateFromThenSmallBoundedOrd
633 prop "enumerateFromToSmallOrd" enumerateFromToSmallOrd
633 prop "enumerateFromToSmallOrd" enumerateFromToSmallOrd
634 prop "enumerateFromThenToSmallOrd" enumerateFromThenToSmallOrd
634 prop "enumerateFromThenToSmallOrd" enumerateFromThenToSmallOrd
635
635
636− prop "enumerateFromSmallUnit" enumerateFromSmallUnit
636+ prop "enumerateFromSmallBoundedUnit" enumerateFromSmallBoundedUnit
637− prop "enumerateFromThenSmallUnit" enumerateFromThenSmallUnit
637+ prop "enumerateFromThenSmallBoundedUnit" enumerateFromThenSmallBoundedUnit
638 prop "enumerateFromToSmallUnit" enumerateFromToSmallUnit
638 prop "enumerateFromToSmallUnit" enumerateFromToSmallUnit
639 prop "enumerateFromThenToSmallUnit" enumerateFromThenToSmallUnit
639 prop "enumerateFromThenToSmallUnit" enumerateFromThenToSmallUnit
640
640
641− prop "enumerateFromSmallUnit" enumerateFromSmallBool
641+ prop "enumerateFromSmallBoundedUnit" enumerateFromSmallBoundedBool
642− prop "enumerateFromThenSmallBool" enumerateFromThenSmallBool
642+ prop "enumerateFromThenSmallBoundedBool" enumerateFromThenSmallBoundedBool
643 prop "enumerateFromToSmallBool" enumerateFromToSmallBool
643 prop "enumerateFromToSmallBool" enumerateFromToSmallBool
644 prop "enumerateFromThenToSmallBool" enumerateFromThenToSmallBool
644 prop "enumerateFromThenToSmallBool" enumerateFromThenToSmallBool
645
645