2015-12-24 06:38:02 +03:00
|
|
|
module Source where
|
|
|
|
|
2015-12-24 06:38:20 +03:00
|
|
|
import Range
|
2015-12-24 06:48:31 +03:00
|
|
|
import qualified Data.Vector as Vector
|
2015-12-24 06:38:20 +03:00
|
|
|
|
2015-12-24 07:06:38 +03:00
|
|
|
newtype Source a = Source { getVector :: Vector.Vector a }
|
2015-12-24 06:38:20 +03:00
|
|
|
deriving (Eq, Show, Functor, Foldable)
|
|
|
|
|
2015-12-24 07:37:51 +03:00
|
|
|
fromList :: [a] -> Source a
|
|
|
|
fromList = Source . Vector.fromList
|
2015-12-24 06:38:20 +03:00
|
|
|
|
2015-12-24 07:26:37 +03:00
|
|
|
toList :: Source a -> [a]
|
|
|
|
toList = Vector.toList . getVector
|
2015-12-24 06:38:20 +03:00
|
|
|
|
2015-12-24 07:25:00 +03:00
|
|
|
slice :: Range -> Source a -> Source a
|
2015-12-24 07:27:07 +03:00
|
|
|
slice range = Source . Vector.slice (start range) (end range - start range) . getVector
|
2015-12-24 06:38:20 +03:00
|
|
|
|
|
|
|
toString :: Source Char -> String
|
2015-12-24 07:26:37 +03:00
|
|
|
toString = toList
|
2015-12-24 06:38:20 +03:00
|
|
|
|
|
|
|
at :: Source a -> Int -> a
|
2015-12-24 07:29:27 +03:00
|
|
|
at = (Vector.!) . getVector
|
2015-12-24 07:04:50 +03:00
|
|
|
|
|
|
|
null :: Source a -> Bool
|
2015-12-24 07:28:13 +03:00
|
|
|
null = Vector.null . getVector
|
2015-12-24 07:05:01 +03:00
|
|
|
|
2015-12-24 07:06:49 +03:00
|
|
|
cons :: a -> Source a -> Source a
|
|
|
|
cons a = Source . Vector.cons a . getVector
|
|
|
|
|
2015-12-24 07:05:01 +03:00
|
|
|
uncons :: Source a -> Maybe (a, Source a)
|
|
|
|
uncons (Source vector) = if Vector.null vector then Nothing else Just (Vector.head vector, Source $ Vector.tail vector)
|
2015-12-24 07:16:09 +03:00
|
|
|
|
|
|
|
break :: (a -> Bool) -> Source a -> (Source a, Source a)
|
2015-12-24 07:23:16 +03:00
|
|
|
break predicate (Source vector) = let (start, remainder) = Vector.break predicate vector in (Source start, Source remainder)
|
2015-12-24 07:41:08 +03:00
|
|
|
|
|
|
|
(++) :: Source a -> Source a -> Source a
|
|
|
|
(++) (Source a) = Source . (a Vector.++) . getVector
|