From 801ea7ee9735d200a39a47945e88e04384352eb1 Mon Sep 17 00:00:00 2001 From: Roman Leshchinskiy Date: Sat, 12 Jul 2008 02:07:05 +0000 Subject: [PATCH] Vector combinators --- Data/Vector/Base.hs | 67 +++++++++++++++++++++++++++++++++++++++++++++++---- Data/Vector/Stream.hs | 4 +-- 2 files changed, 64 insertions(+), 7 deletions(-) diff --git a/Data/Vector/Base.hs b/Data/Vector/Base.hs index 494c603..5637d6f 100644 --- a/Data/Vector/Base.hs +++ b/Data/Vector/Base.hs @@ -11,7 +11,13 @@ import qualified Data.Vector.Stream as Stream import Data.Vector.Stream ( Stream ) import Data.Vector.Stream.Size -import Prelude hiding ( length, map, zipWith, sum ) +import Prelude hiding ( length, + replicate, (++), + head, last, + init, tail, take, drop, + map, zipWith, + filter, takeWhile, dropWhile, + foldl, foldl1, foldr, foldr1 ) class Base v a where create :: (forall mv m. Mut.Base mv m a => m (mv m a)) -> v a @@ -42,11 +48,37 @@ unstream s = create (Mut.unstream s) #-} -infixr ++ +-- Construction +-- ------------ + +empty :: Base v a => v a +{-# INLINE empty #-} +empty = unstream Stream.empty + +singleton :: Base v a => a -> v a +{-# INLINE singleton #-} +singleton x = unstream (Stream.singleton x) + +replicate :: Base v a => Int -> a -> v a +{-# INLINE replicate #-} +replicate n = unstream . Stream.replicate n + +cons :: Base v a => a -> v a -> v a +{-# INLINE cons #-} +cons x = unstream . Stream.cons x . stream + +snoc :: Base v a => v a -> a -> v a +{-# INLINE snoc #-} +snoc v = unstream . Stream.snoc (stream v) + +infixr 5 ++ (++) :: Base v a => v a -> v a -> v a {-# INLINE (++) #-} v ++ w = unstream (stream v Stream.++ stream w) +-- Subarrays +-- --------- + take :: Base v a => Int -> v a -> v a {-# INLINE take #-} take n = unstream . Stream.take n . stream @@ -55,6 +87,9 @@ drop :: Base v a => Int -> v a -> v a {-# INLINE drop #-} drop n = unstream . Stream.drop n . stream +-- Mapping/zipping +-- --------------- + map :: (Base v a, Base v b) => (a -> b) -> v a -> v b {-# INLINE map #-} map f = unstream . Stream.map f . stream @@ -63,6 +98,9 @@ zipWith :: (Base v a, Base v b, Base v c) => (a -> b -> c) -> v a -> v b -> v c {-# INLINE zipWith #-} zipWith f xs ys = unstream (Stream.zipWith f (stream xs) (stream ys)) +-- Filtering +-- --------- + filter :: Base v a => (a -> Bool) -> v a -> v a {-# INLINE filter #-} filter f = unstream . Stream.filter f . stream @@ -75,13 +113,32 @@ dropWhile :: Base v a => (a -> Bool) -> v a -> v a {-# INLINE dropWhile #-} dropWhile f = unstream . Stream.dropWhile f . stream +-- Folding +-- ------- + +foldl :: Base v b => (a -> b -> a) -> a -> v b -> a +{-# INLINE foldl #-} +foldl f z = Stream.foldl f z . stream + +foldl1 :: Base v a => (a -> a -> a) -> v a -> a +{-# INLINE foldl1 #-} +foldl1 f = Stream.foldl1 f . stream + foldl' :: Base v b => (a -> b -> a) -> a -> v b -> a {-# INLINE foldl' #-} foldl' f z = Stream.foldl' f z . stream -sum :: (Base v a, Num a) => v a -> a -{-# INLINE sum #-} -sum = foldl' (+) 0 +foldl1' :: Base v a => (a -> a -> a) -> v a -> a +{-# INLINE foldl1' #-} +foldl1' f = Stream.foldl1' f . stream + +foldr :: Base v a => (a -> b -> b) -> b -> v a -> b +{-# INLINE foldr #-} +foldr f z = Stream.foldr f z . stream + +foldr1 :: Base v a => (a -> a -> a) -> v a -> a +{-# INLINE foldr1 #-} +foldr1 f = Stream.foldr1 f . stream toList :: Base v a => v a -> [a] {-# INLINE toList #-} diff --git a/Data/Vector/Stream.hs b/Data/Vector/Stream.hs index c401635..08c64d8 100644 --- a/Data/Vector/Stream.hs +++ b/Data/Vector/Stream.hs @@ -7,7 +7,7 @@ module Data.Vector.Stream ( size, sized, unfold, toList, fromList, length, null, - empty, singleton, replicate, (++), + empty, singleton, cons, snoc, replicate, (++), head, last, (!!), init, tail, take, drop, map, zipWith, @@ -103,7 +103,7 @@ snoc :: Stream a -> a -> Stream a {-# INLINE snoc #-} snoc s x = s ++ singleton x -infixr ++ +infixr 5 ++ (++) :: Stream a -> Stream a -> Stream a {-# INLINE_STREAM (++) #-} Stream stepa sa na ++ Stream stepb sb nb = Stream step (Left sa) (na + nb) -- 1.9.1