2e038b441cf68387455d50ab3212ce8ddab82224
[darcs-mirrors/vector.git] / Data / Vector / IVector.hs
1 {-# LANGUAGE Rank2Types, MultiParamTypeClasses, FlexibleContexts,
2 ScopedTypeVariables #-}
3 -- |
4 -- Module : Data.Vector.IVector
5 -- Copyright : (c) Roman Leshchinskiy 2008
6 -- License : BSD-style
7 --
8 -- Maintainer : Roman Leshchinskiy <rl@cse.unsw.edu.au>
9 -- Stability : experimental
10 -- Portability : non-portable
11 --
12 -- Generic interface to pure vectors
13 --
14
15 #include "phases.h"
16
17 module Data.Vector.IVector (
18 -- * Immutable vectors
19 IVector(..),
20
21 -- * Length information
22 length, null,
23
24 -- * Construction
25 empty, singleton, cons, snoc, replicate, (++), copy,
26
27 -- * Accessing individual elements
28 (!), head, last, indexM, headM, lastM,
29
30 -- * Subvectors
31 slice, init, tail, take, drop,
32
33 -- * Permutations
34 accum, (//), update, backpermute, reverse,
35
36 -- * Mapping
37 map, concatMap,
38
39 -- * Zipping and unzipping
40 zipWith, zipWith3, zip, zip3, unzip, unzip3,
41
42 -- * Comparisons
43 eq, cmp,
44
45 -- * Filtering
46 filter, takeWhile, dropWhile,
47
48 -- * Searching
49 elem, notElem, find, findIndex,
50
51 -- * Folding
52 foldl, foldl1, foldl', foldl1', foldr, foldr1,
53
54 -- * Specialised folds
55 and, or, sum, product, maximum, minimum,
56
57 -- * Unfolding
58 unfoldr,
59
60 -- * Scans
61 prescanl, prescanl',
62 postscanl, postscanl',
63 scanl, scanl', scanl1, scanl1',
64
65 -- * Enumeration
66 enumFromTo, enumFromThenTo,
67
68 -- * Conversion to/from lists
69 toList, fromList,
70
71 -- * Conversion to/from Streams
72 stream, unstream,
73
74 -- * MVector-based initialisation
75 new
76 ) where
77
78 import qualified Data.Vector.MVector as MVector
79 import Data.Vector.MVector ( MVector )
80
81 import qualified Data.Vector.MVector.New as New
82 import Data.Vector.MVector.New ( New )
83
84 import qualified Data.Vector.Fusion.Stream as Stream
85 import Data.Vector.Fusion.Stream ( Stream, MStream )
86 import qualified Data.Vector.Fusion.Stream.Monadic as MStream
87 import Data.Vector.Fusion.Stream.Size
88 import Data.Vector.Fusion.Util
89
90 import Control.Exception ( assert )
91
92 import Prelude hiding ( length, null,
93 replicate, (++),
94 head, last,
95 init, tail, take, drop, reverse,
96 map, concatMap,
97 zipWith, zipWith3, zip, zip3, unzip, unzip3,
98 filter, takeWhile, dropWhile,
99 elem, notElem,
100 foldl, foldl1, foldr, foldr1,
101 and, or, sum, product, maximum, minimum,
102 scanl, scanl1,
103 enumFromTo, enumFromThenTo )
104
105 -- | Class of immutable vectors.
106 --
107 class IVector v a where
108 -- | Construct a pure vector from a monadic initialiser (not fusible!)
109 vnew :: (forall mv m. MVector mv m a => m (mv a)) -> v a
110
111 -- | Length of the vector (not fusible!)
112 vlength :: v a -> Int
113
114 -- | Yield a part of the vector without copying it. No range checks!
115 unsafeSlice :: v a -> Int -> Int -> v a
116
117 -- | Yield the element at the given position in a monad. The monad allows us
118 -- to be strict in the vector if we want. Suppose we had
119 --
120 -- > unsafeIndex :: v a -> Int -> a
121 --
122 -- instead. Now, if we wanted to copy a vector, we'd do something like
123 --
124 -- > copy mv v ... = ... unsafeWrite mv i (unsafeIndex v i) ...
125 --
126 -- For lazy vectors, the indexing would not be evaluated which means that we
127 -- would retain a reference to the original vector in each element we write.
128 -- This is not what we want!
129 --
130 -- With 'unsafeIndexM', we can do
131 --
132 -- > copy mv v ... = ... case unsafeIndexM v i of
133 -- > Box x -> unsafeWrite mv i x ...
134 --
135 -- which does not have this problem because indexing (but not the returned
136 -- element!) is evaluated immediately.
137 --
138 unsafeIndexM :: Monad m => v a -> Int -> m a
139
140 -- Fusion
141 -- ------
142
143 -- | Construct a pure vector from a monadic initialiser
144 new :: IVector v a => New a -> v a
145 {-# INLINE new #-}
146 new m = new' undefined m
147
148 -- | Same as 'new' but with a dummy argument necessary for correctly typing
149 -- the rule @uninplace@.
150 --
151 -- See http://hackage.haskell.org/trac/ghc/ticket/2600
152 new' :: IVector v a => v a -> New a -> v a
153 {-# INLINE_STREAM new' #-}
154 new' _ m = vnew (New.run m)
155
156 -- | Convert a vector to a 'Stream'
157 stream :: IVector v a => v a -> Stream a
158 {-# INLINE_STREAM stream #-}
159 stream v = v `seq` (Stream.unfoldr get 0 `Stream.sized` Exact n)
160 where
161 n = length v
162
163 {-# INLINE get #-}
164 get i | i < n = case unsafeIndexM v i of Box x -> Just (x, i+1)
165 | otherwise = Nothing
166
167 -- | Create a vector from a 'Stream'
168 unstream :: IVector v a => Stream a -> v a
169 {-# INLINE unstream #-}
170 unstream s = new (New.unstream s)
171
172 {-# RULES
173
174 "stream/unstream [IVector]" forall v s.
175 stream (new' v (New.unstream s)) = s
176
177 "New.unstream/stream/new [IVector]" forall v p.
178 New.unstream (stream (new' v p)) = p
179
180 #-}
181
182 inplace :: (forall m. Monad m => MStream m a -> MStream m a)
183 -> Stream a -> Stream a
184 {-# INLINE_STREAM inplace #-}
185 inplace f s = f s
186
187 {-# RULES
188
189 "inplace [IVector]"
190 forall (f :: forall m. Monad m => MStream m a -> MStream m a) v m.
191 New.unstream (inplace f (stream (new' v m))) = New.transform f m
192
193 "uninplace [IVector]"
194 forall (f :: forall m. Monad m => MStream m a -> MStream m a) v m.
195 stream (new' v (New.transform f m)) = inplace f (stream (new' v m))
196
197 "inplace/inplace [IVector]"
198 forall (f :: forall m. Monad m => MStream m a -> MStream m a)
199 (g :: forall m. Monad m => MStream m a -> MStream m a)
200 s.
201 inplace f (inplace g s) = inplace (f . g) s
202
203 #-}
204
205 -- Length
206 -- ------
207
208 length :: IVector v a => v a -> Int
209 {-# INLINE_STREAM length #-}
210 length v = vlength v
211
212 {-# RULES
213
214 "length/unstream [IVector]" forall v s.
215 length (new' v (New.unstream s)) = Stream.length s
216
217 #-}
218
219 null :: IVector v a => v a -> Bool
220 {-# INLINE_STREAM null #-}
221 null v = vlength v == 0
222
223 {-# RULES
224
225 "null/unstream [IVector]" forall v s.
226 null (new' v (New.unstream s)) = Stream.null s
227
228 #-}
229
230 -- Construction
231 -- ------------
232
233 -- | Empty vector
234 empty :: IVector v a => v a
235 {-# INLINE empty #-}
236 empty = unstream Stream.empty
237
238 -- | Vector with exaclty one element
239 singleton :: IVector v a => a -> v a
240 {-# INLINE singleton #-}
241 singleton x = unstream (Stream.singleton x)
242
243 -- | Vector of the given length with the given value in each position
244 replicate :: IVector v a => Int -> a -> v a
245 {-# INLINE replicate #-}
246 replicate n = unstream . Stream.replicate n
247
248 -- | Prepend an element
249 cons :: IVector v a => a -> v a -> v a
250 {-# INLINE cons #-}
251 cons x = unstream . Stream.cons x . stream
252
253 -- | Append an element
254 snoc :: IVector v a => v a -> a -> v a
255 {-# INLINE snoc #-}
256 snoc v = unstream . Stream.snoc (stream v)
257
258 infixr 5 ++
259 -- | Concatenate two vectors
260 (++) :: IVector v a => v a -> v a -> v a
261 {-# INLINE (++) #-}
262 v ++ w = unstream (stream v Stream.++ stream w)
263
264 -- | Create a copy of a vector. Useful when dealing with slices.
265 copy :: IVector v a => v a -> v a
266 {-# INLINE_STREAM copy #-}
267 copy = unstream . stream
268
269 {-# RULES
270
271 "copy/unstream [IVector]" forall v s.
272 copy (new' v (New.unstream s)) = new' v (New.unstream s)
273
274 #-}
275
276 -- Accessing individual elements
277 -- -----------------------------
278
279 -- | Indexing
280 (!) :: IVector v a => v a -> Int -> a
281 {-# INLINE_STREAM (!) #-}
282 v ! i = assert (i >= 0 && i < length v)
283 $ unId (unsafeIndexM v i)
284
285 -- | First element
286 head :: IVector v a => v a -> a
287 {-# INLINE_STREAM head #-}
288 head v = v ! 0
289
290 -- | Last element
291 last :: IVector v a => v a -> a
292 {-# INLINE_STREAM last #-}
293 last v = v ! (length v - 1)
294
295 {-# RULES
296
297 "(!)/unstream [IVector]" forall v i s.
298 new' v (New.unstream s) ! i = s Stream.!! i
299
300 "head/unstream [IVector]" forall v s.
301 head (new' v (New.unstream s)) = Stream.head s
302
303 "last/unstream [IVector]" forall v s.
304 last (new' v (New.unstream s)) = Stream.last s
305
306 #-}
307
308 -- | Monadic indexing which can be strict in the vector while remaining lazy in
309 -- the element.
310 indexM :: (IVector v a, Monad m) => v a -> Int -> m a
311 {-# INLINE_STREAM indexM #-}
312 indexM v i = assert (i >= 0 && i < length v)
313 $ unsafeIndexM v i
314
315 headM :: (IVector v a, Monad m) => v a -> m a
316 {-# INLINE_STREAM headM #-}
317 headM v = indexM v 0
318
319 lastM :: (IVector v a, Monad m) => v a -> m a
320 {-# INLINE_STREAM lastM #-}
321 lastM v = indexM v (length v - 1)
322
323 {-# RULES
324
325 "indexM/unstream [IVector]" forall v i s.
326 indexM (new' v (New.unstream s)) i = return (s Stream.!! i)
327
328 "headM/unstream [IVector]" forall v s.
329 headM (new' v (New.unstream s)) = return (Stream.head s)
330
331 "lastM/unstream [IVector]" forall v s.
332 lastM (new' v (New.unstream s)) = return (Stream.last s)
333
334 #-}
335
336 -- Subarrays
337 -- ---------
338
339 -- FIXME: slicing doesn't work with the inplace stuff at the moment
340
341 -- | Yield a part of the vector without copying it. Safer version of
342 -- 'unsafeSlice'.
343 slice :: IVector v a => v a -> Int -- ^ starting index
344 -> Int -- ^ length
345 -> v a
346 {-# INLINE_STREAM slice #-}
347 slice v i n = assert (i >= 0 && n >= 0 && i+n <= length v)
348 $ unsafeSlice v i n
349
350 -- | Yield all but the last element without copying.
351 init :: IVector v a => v a -> v a
352 {-# INLINE_STREAM init #-}
353 init v = slice v 0 (length v - 1)
354
355 -- | All but the first element (without copying).
356 tail :: IVector v a => v a -> v a
357 {-# INLINE_STREAM tail #-}
358 tail v = slice v 1 (length v - 1)
359
360 -- | Yield the first @n@ elements without copying.
361 take :: IVector v a => Int -> v a -> v a
362 {-# INLINE_STREAM take #-}
363 take n v = slice v 0 (min n' (length v))
364 where n' = max n 0
365
366 -- | Yield all but the first @n@ elements without copying.
367 drop :: IVector v a => Int -> v a -> v a
368 {-# INLINE_STREAM drop #-}
369 drop n v = slice v (min n' len) (max 0 (len - n'))
370 where n' = max n 0
371 len = length v
372
373 {-# RULES
374
375 "slice/new [IVector]" forall v p i n.
376 slice (new' v p) i n = new' v (New.slice p i n)
377
378 "init/new [IVector]" forall v p.
379 init (new' v p) = new' v (New.init p)
380
381 "tail/new [IVector]" forall v p.
382 tail (new' v p) = new' v (New.tail p)
383
384 "take/new [IVector]" forall n v p.
385 take n (new' v p) = new' v (New.take n p)
386
387 "drop/new [IVector]" forall n v p.
388 drop n (new' v p) = new' v (New.drop n p)
389
390 #-}
391
392 -- Permutations
393 -- ------------
394
395 accum :: IVector v a => (a -> b -> a) -> v a -> [(Int,b)] -> v a
396 {-# INLINE accum #-}
397 accum f v us = new (New.accum f (New.unstream (stream v))
398 (Stream.fromList us))
399
400 (//) :: IVector v a => v a -> [(Int, a)] -> v a
401 {-# INLINE (//) #-}
402 v // us = new (New.update (New.unstream (stream v))
403 (Stream.fromList us))
404
405 update :: (IVector v a, IVector v (Int, a)) => v a -> v (Int, a) -> v a
406 {-# INLINE update #-}
407 update v w = new (New.update (New.unstream (stream v)) (stream w))
408
409 -- This somewhat non-intuitive definition ensures that the resulting vector
410 -- does not retain references to the original one even if it is lazy in its
411 -- elements. This would not be the case if we simply used
412 --
413 -- backpermute v is = map (v!) is
414 backpermute :: (IVector v a, IVector v Int) => v a -> v Int -> v a
415 {-# INLINE backpermute #-}
416 backpermute v is = unstream
417 . MStream.trans (Id . unBox)
418 . MStream.mapM (indexM v)
419 . MStream.trans (Box . unId)
420 $ stream is
421
422 reverse :: (IVector v a) => v a -> v a
423 {-# INLINE reverse #-}
424 reverse = new . New.reverse . New.unstream . stream
425
426 -- Mapping
427 -- -------
428
429 -- | Map a function over a vector
430 map :: (IVector v a, IVector v b) => (a -> b) -> v a -> v b
431 {-# INLINE map #-}
432 map f = unstream . Stream.map f . stream
433
434 inplace_map :: IVector v a => (a -> a) -> v a -> v a
435 {-# INLINE inplace_map #-}
436 inplace_map f = unstream . inplace (MStream.map f) . stream
437
438 {-# RULES
439
440 "map->inplace_map [IVector]" map = inplace_map
441
442 #-}
443
444 concatMap :: (IVector v a, IVector v b) => (a -> v b) -> v a -> v b
445 {-# INLINE concatMap #-}
446 concatMap f = unstream . Stream.concatMap (stream . f) . stream
447
448 -- Zipping/unzipping
449 -- -----------------
450
451 -- | Zip two vectors with the given function.
452 zipWith :: (IVector v a, IVector v b, IVector v c) => (a -> b -> c) -> v a -> v b -> v c
453 {-# INLINE zipWith #-}
454 zipWith f xs ys = unstream (Stream.zipWith f (stream xs) (stream ys))
455
456 -- | Zip three vectors with the given function.
457 zipWith3 :: (IVector v a, IVector v b, IVector v c, IVector v d) => (a -> b -> c -> d) -> v a -> v b -> v c -> v d
458 {-# INLINE zipWith3 #-}
459 zipWith3 f xs ys zs = unstream (Stream.zipWith3 f (stream xs) (stream ys) (stream zs))
460
461 zip :: (IVector v a, IVector v b, IVector v (a,b)) => v a -> v b -> v (a, b)
462 {-# INLINE zip #-}
463 zip = zipWith (,)
464
465 zip3 :: (IVector v a, IVector v b, IVector v c, IVector v (a, b, c)) => v a -> v b -> v c -> v (a, b, c)
466 {-# INLINE zip3 #-}
467 zip3 = zipWith3 (,,)
468
469 unzip :: (IVector v a, IVector v b, IVector v (a,b)) => v (a, b) -> (v a, v b)
470 {-# INLINE unzip #-}
471 unzip xs = (map fst xs, map snd xs)
472
473 unzip3 :: (IVector v a, IVector v b, IVector v c, IVector v (a, b, c)) => v (a, b, c) -> (v a, v b, v c)
474 {-# INLINE unzip3 #-}
475 unzip3 xs = (map (\(a, b, c) -> a) xs, map (\(a, b, c) -> b) xs, map (\(a, b, c) -> c) xs)
476
477 -- Comparisons
478 -- -----------
479
480 eq :: (IVector v a, Eq a) => v a -> v a -> Bool
481 {-# INLINE eq #-}
482 xs `eq` ys = stream xs == stream ys
483
484 cmp :: (IVector v a, Ord a) => v a -> v a -> Ordering
485 {-# INLINE cmp #-}
486 cmp xs ys = compare (stream xs) (stream ys)
487
488 -- Filtering
489 -- ---------
490
491 -- | Drop elements which do not satisfy the predicate
492 filter :: IVector v a => (a -> Bool) -> v a -> v a
493 {-# INLINE filter #-}
494 filter f = unstream . inplace (MStream.filter f) . stream
495
496 -- | Yield the longest prefix of elements satisfying the predicate.
497 takeWhile :: IVector v a => (a -> Bool) -> v a -> v a
498 {-# INLINE takeWhile #-}
499 takeWhile f = unstream . Stream.takeWhile f . stream
500
501 -- | Drop the longest prefix of elements that satisfy the predicate.
502 dropWhile :: IVector v a => (a -> Bool) -> v a -> v a
503 {-# INLINE dropWhile #-}
504 dropWhile f = unstream . Stream.dropWhile f . stream
505
506 -- Searching
507 -- ---------
508
509 infix 4 `elem`
510 -- | Check whether the vector contains an element
511 elem :: (IVector v a, Eq a) => a -> v a -> Bool
512 {-# INLINE elem #-}
513 elem x = Stream.elem x . stream
514
515 infix 4 `notElem`
516 -- | Inverse of `elem`
517 notElem :: (IVector v a, Eq a) => a -> v a -> Bool
518 {-# INLINE notElem #-}
519 notElem x = Stream.notElem x . stream
520
521 -- | Yield 'Just' the first element matching the predicate or 'Nothing' if no
522 -- such element exists.
523 find :: IVector v a => (a -> Bool) -> v a -> Maybe a
524 {-# INLINE find #-}
525 find f = Stream.find f . stream
526
527 -- | Yield 'Just' the index of the first element matching the predicate or
528 -- 'Nothing' if no such element exists.
529 findIndex :: IVector v a => (a -> Bool) -> v a -> Maybe Int
530 {-# INLINE findIndex #-}
531 findIndex f = Stream.findIndex f . stream
532
533 -- Folding
534 -- -------
535
536 -- | Left fold
537 foldl :: IVector v b => (a -> b -> a) -> a -> v b -> a
538 {-# INLINE foldl #-}
539 foldl f z = Stream.foldl f z . stream
540
541 -- | Lefgt fold on non-empty vectors
542 foldl1 :: IVector v a => (a -> a -> a) -> v a -> a
543 {-# INLINE foldl1 #-}
544 foldl1 f = Stream.foldl1 f . stream
545
546 -- | Left fold with strict accumulator
547 foldl' :: IVector v b => (a -> b -> a) -> a -> v b -> a
548 {-# INLINE foldl' #-}
549 foldl' f z = Stream.foldl' f z . stream
550
551 -- | Left fold on non-empty vectors with strict accumulator
552 foldl1' :: IVector v a => (a -> a -> a) -> v a -> a
553 {-# INLINE foldl1' #-}
554 foldl1' f = Stream.foldl1' f . stream
555
556 -- | Right fold
557 foldr :: IVector v a => (a -> b -> b) -> b -> v a -> b
558 {-# INLINE foldr #-}
559 foldr f z = Stream.foldr f z . stream
560
561 -- | Right fold on non-empty vectors
562 foldr1 :: IVector v a => (a -> a -> a) -> v a -> a
563 {-# INLINE foldr1 #-}
564 foldr1 f = Stream.foldr1 f . stream
565
566 -- Specialised folds
567 -- -----------------
568
569 and :: IVector v Bool => v Bool -> Bool
570 {-# INLINE and #-}
571 and = Stream.and . stream
572
573 or :: IVector v Bool => v Bool -> Bool
574 {-# INLINE or #-}
575 or = Stream.or . stream
576
577 sum :: (IVector v a, Num a) => v a -> a
578 {-# INLINE sum #-}
579 sum = Stream.foldl' (+) 0 . stream
580
581 product :: (IVector v a, Num a) => v a -> a
582 {-# INLINE product #-}
583 product = Stream.foldl' (*) 1 . stream
584
585 maximum :: (IVector v a, Ord a) => v a -> a
586 {-# INLINE maximum #-}
587 maximum = Stream.foldl1' max . stream
588
589 minimum :: (IVector v a, Ord a) => v a -> a
590 {-# INLINE minimum #-}
591 minimum = Stream.foldl1' min . stream
592
593 -- Unfolding
594 -- ---------
595
596 unfoldr :: IVector v a => (b -> Maybe (a, b)) -> b -> v a
597 {-# INLINE unfoldr #-}
598 unfoldr f = unstream . Stream.unfoldr f
599
600 -- Scans
601 -- -----
602
603 -- | Prefix scan
604 prescanl :: (IVector v a, IVector v b) => (a -> b -> a) -> a -> v b -> v a
605 {-# INLINE prescanl #-}
606 prescanl f z = unstream . Stream.prescanl f z . stream
607
608 inplace_prescanl :: IVector v a => (a -> a -> a) -> a -> v a -> v a
609 {-# INLINE inplace_prescanl #-}
610 inplace_prescanl f z = unstream . inplace (MStream.prescanl f z) . stream
611
612 {-# RULES
613
614 "prescanl -> inplace_prescanl [IVector]" prescanl = inplace_prescanl
615
616 #-}
617
618 -- | Prefix scan with strict accumulator
619 prescanl' :: (IVector v a, IVector v b) => (a -> b -> a) -> a -> v b -> v a
620 {-# INLINE prescanl' #-}
621 prescanl' f z = unstream . Stream.prescanl' f z . stream
622
623 inplace_prescanl' :: IVector v a => (a -> a -> a) -> a -> v a -> v a
624 {-# INLINE inplace_prescanl' #-}
625 inplace_prescanl' f z = unstream . inplace (MStream.prescanl' f z) . stream
626
627 {-# RULES
628
629 "prescanl' -> inplace_prescanl' [IVector]" prescanl' = inplace_prescanl'
630
631 #-}
632
633 -- | Suffix scan
634 postscanl :: (IVector v a, IVector v b) => (a -> b -> a) -> a -> v b -> v a
635 {-# INLINE postscanl #-}
636 postscanl f z = unstream . Stream.postscanl f z . stream
637
638 inplace_postscanl :: IVector v a => (a -> a -> a) -> a -> v a -> v a
639 {-# INLINE inplace_postscanl #-}
640 inplace_postscanl f z = unstream . inplace (MStream.postscanl f z) . stream
641
642 {-# RULES
643
644 "postscanl -> inplace_postscanl [IVector]" postscanl = inplace_postscanl
645
646 #-}
647
648 -- | Suffix scan with strict accumulator
649 postscanl' :: (IVector v a, IVector v b) => (a -> b -> a) -> a -> v b -> v a
650 {-# INLINE postscanl' #-}
651 postscanl' f z = unstream . Stream.postscanl' f z . stream
652
653 inplace_postscanl' :: IVector v a => (a -> a -> a) -> a -> v a -> v a
654 {-# INLINE inplace_postscanl' #-}
655 inplace_postscanl' f z = unstream . inplace (MStream.postscanl' f z) . stream
656
657 {-# RULES
658
659 "postscanl' -> inplace_postscanl' [IVector]" postscanl' = inplace_postscanl'
660
661 #-}
662
663 -- | Haskell-style scan
664 scanl :: (IVector v a, IVector v b) => (a -> b -> a) -> a -> v b -> v a
665 {-# INLINE scanl #-}
666 scanl f z = unstream . Stream.scanl f z . stream
667
668 -- | Haskell-style scan with strict accumulator
669 scanl' :: (IVector v a, IVector v b) => (a -> b -> a) -> a -> v b -> v a
670 {-# INLINE scanl' #-}
671 scanl' f z = unstream . Stream.scanl' f z . stream
672
673 -- | Scan over a non-empty vector
674 scanl1 :: IVector v a => (a -> a -> a) -> v a -> v a
675 {-# INLINE scanl1 #-}
676 scanl1 f = unstream . inplace (MStream.scanl1 f) . stream
677
678 -- | Scan over a non-empty vector with a strict accumulator
679 scanl1' :: IVector v a => (a -> a -> a) -> v a -> v a
680 {-# INLINE scanl1' #-}
681 scanl1' f = unstream . inplace (MStream.scanl1' f) . stream
682
683 -- Enumeration
684 -- -----------
685
686 enumFromTo :: (IVector v a, Enum a) => a -> a -> v a
687 {-# INLINE enumFromTo #-}
688 enumFromTo from to = from `seq` to `seq` unfoldr enumFromTo_go (fromEnum from)
689 where
690 to_i = fromEnum to
691 enumFromTo_go i | i <= to_i = Just (toEnum i, i + 1)
692 | otherwise = Nothing
693
694 enumFromThenTo :: (IVector v a, Enum a) => a -> a -> a -> v a
695 {-# INLINE enumFromThenTo #-}
696 enumFromThenTo from next to = from `seq` next `seq` to `seq` unfoldr enumFromThenTo_go from_i
697 where
698 from_i = fromEnum from
699 to_i = fromEnum to
700 step_i = fromEnum next - from_i
701 enumFromThenTo_go i | i <= to_i = Just (toEnum i, i + step_i)
702 | otherwise = Nothing
703
704 -- | Convert a vector to a list
705 toList :: IVector v a => v a -> [a]
706 {-# INLINE toList #-}
707 toList = Stream.toList . stream
708
709 -- | Convert a list to a vector
710 fromList :: IVector v a => [a] -> v a
711 {-# INLINE fromList #-}
712 fromList = unstream . Stream.fromList
713