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