**Paste:**#107665**Authors:**1HaskellADay, geophf and Anonymous Coward**Language:**Haskell**Channel:**-**Created:**2014-07-17 17:28:27 UTC**Revisions:**- 2016-06-15 19:46:54 UTC #7993913983104974848 (diff): Stream of Consciousness (geophf)
- 2016-06-15 19:46:15 UTC #1666457477035589632 (diff): Stream of Consciousness (geophf)
- 2015-10-26 23:44:22 UTC #7060893386714120192 (diff): No title (Anonymous Coward)
- 2014-08-15 16:44:44 UTC #109438 (diff): No title (1HaskellADay)
- 2014-07-24 16:08:34 UTC #108035 (diff): No title (1HaskellADay)
- 2014-07-22 19:41:30 UTC #107901 (diff): No title (geophf)
- 2014-07-18 19:35:33 UTC #107717 (diff): No title (1HaskellADay)
- 2014-07-17 17:28:27 UTC #107665: TakeS this Stream and ... it! (1HaskellADay)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | module Data.Stream where import Control.Comonad import Control.Monad infixr 5 :< -- This is a solution to the bonus Haskell question -- at http://lpaste.net/107655 -- a stream is a ... well, stream of objects, usually defined by some -- unfold-like function ('unfold' function can also be considered a kind -- of a continuation ... but can't everything, then? Even not-continued -- functions have a continuation: it's called 'stop.' ;) data Stream a = a :< Stream a instance Show a => Show (Stream a) where show stream@(a :< as) = "Stream" ++ showSome 5 stream showSome :: Show a => Int -> Stream a -> String showSome x stream = "{ " ++ s' (fromInt x) stream ++ "... }" where s' 0 _ = "" s' n (a :< as) = show a ++ " :< " ++ s' (pred n) as instance Functor Stream where fmap f (a :< as) = f a :< fmap f as instance Comonad Stream where extract (a :< _) = a duplicate stream@(a :< as) = stream :< duplicate as -- a chooser function to pick some element from the stream: choose :: Stream a -> [(a, Stream a)] choose (a :< as) = (a, as) : choose as -- So, a stream of 2-4-2 numbers that are primes-...ish are: torf :: Integer -> Integer torf x = 6 - x -- given that x is either 2 or 4 primesish :: Stream Integer primesish = let seed = 7 :< 11 :< (seed =>> \(a :< b :< _) -> b + torf (b - a)) in 2 :< 3 :< 5 :< seed nats :: Stream Integer nats = 0 :< (nats =>> succ . extract) fibs :: Integral a => Stream a fibs = 0 :< 1 :< (fibs =>> \(a :< b :< _) -> a + b) one :: Stream Double one = 0.5 :< (one =>> (0.5 *) . extract) oneThird :: Stream Double oneThird = 0.5 :< (oneThird =>> ((-0.5) *) . extract) takeS :: Int -> Stream a -> [a] takeS 0 _ = [] takeS n (a :< as) = a : takeS (pred n) as dropS :: Int -> Stream a -> Stream a dropS 0 s = s dropS n (_ :< as) = dropS (pred n) as takeWhileS :: (a -> Bool) -> Stream a -> [a] takeWhileS pred (a :< as) | pred a = a : takeWhileS pred as | otherwise = [] dropWhileS :: (a -> Bool) -> Stream a -> Stream a dropWhileS pred stream@(a :< as) | pred a = dropWhileS pred as | otherwise = stream filterS :: (a -> Bool) -> Stream a -> Stream a filterS pred (a :< as) | pred a = a :< filterS pred as | otherwise = filterS pred as tailS :: Stream a -> Stream a tailS (_ :< as) = as headS :: Stream a -> a headS (a :< _) = a |