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