on 9e2e765 help sign in
streamly/benchmark/LinearOps.hs
619 lines 20 kB history raw
1-- |
2-- Module : BenchmarkOps
3-- Copyright : (c) 2018 Harendra Kumar
4--
5-- License : MIT
6-- Maintainer : harendra.kumar@gmail.com
8{-# LANGUAGE CPP #-}
9{-# LANGUAGE FlexibleContexts #-}
10{-# LANGUAGE ScopedTypeVariables #-}
11{-# LANGUAGE DeriveAnyClass #-}
12{-# LANGUAGE DeriveGeneric #-}
14module LinearOps where
16import Control.Monad (when)
17import Control.Monad.IO.Class (MonadIO)
18import Data.Functor.Identity (Identity, runIdentity)
19import Data.Maybe (fromJust)
20import Prelude
21 (Monad, Int, (+), ($), (.), return, fmap, even, (>), (<=), (==), (>=),
22 subtract, undefined, Maybe(..), odd, Bool, not, (>>=), mapM_, curry,
23 maxBound, div, IO, compare, Double, fromIntegral, Integer, (<$>),
24 (<*>), flip, (**), (/))
25import qualified Prelude as P
26import qualified Data.Foldable as F
27import qualified GHC.Exts as GHC
28import Control.DeepSeq (NFData)
29import GHC.Generics (Generic)
31import qualified Streamly as S hiding (foldMapWith, runStream)
32import qualified Streamly.Prelude as S
34value, maxValue, value2 :: Int
35#ifdef LINEAR_ASYNC
36value = 10000
37#else
38value = 100000
39#endif
40maxValue = value + 1
41value2 = P.round (P.fromIntegral value**(1/2::P.Double)) -- double nested loop
43-------------------------------------------------------------------------------
44-- Benchmark ops
45-------------------------------------------------------------------------------
47-------------------------------------------------------------------------------
48-- Stream generation and elimination
49-------------------------------------------------------------------------------
51type Stream m a = S.SerialT m a
53{-# INLINE source #-}
54source :: (S.MonadAsync m, S.IsStream t) => Int -> t m Int
55source n = sourceUnfoldrM n
57{-# INLINE sourceIntFromTo #-}
58sourceIntFromTo :: (Monad m, S.IsStream t) => Int -> t m Int
59sourceIntFromTo n = S.enumerateFromTo n (n + value)
61{-# INLINE sourceIntFromThenTo #-}
62sourceIntFromThenTo :: (Monad m, S.IsStream t) => Int -> t m Int
63sourceIntFromThenTo n = S.enumerateFromThenTo n (n + 1) (n + value)
65{-# INLINE sourceFracFromTo #-}
66sourceFracFromTo :: (Monad m, S.IsStream t) => Int -> t m Double
67sourceFracFromTo n =
68 S.enumerateFromTo (fromIntegral n) (fromIntegral (n + value))
70{-# INLINE sourceFracFromThenTo #-}
71sourceFracFromThenTo :: (Monad m, S.IsStream t) => Int -> t m Double
72sourceFracFromThenTo n = S.enumerateFromThenTo (fromIntegral n)
73 (fromIntegral n + 1.0001) (fromIntegral (n + value))
75{-# INLINE sourceIntegerFromStep #-}
76sourceIntegerFromStep :: (Monad m, S.IsStream t) => Int -> t m Integer
77sourceIntegerFromStep n =
78 S.take value $ S.enumerateFromThen (fromIntegral n) (fromIntegral n + 1)
80{-# INLINE sourceFromList #-}
81sourceFromList :: (Monad m, S.IsStream t) => Int -> t m Int
82sourceFromList n = S.fromList [n..n+value]
84{-# INLINE sourceFromListM #-}
85sourceFromListM :: (S.MonadAsync m, S.IsStream t) => Int -> t m Int
86sourceFromListM n = S.fromListM (Prelude.fmap return [n..n+value])
88{-# INLINE sourceFromFoldable #-}
89sourceFromFoldable :: S.IsStream t => Int -> t m Int
90sourceFromFoldable n = S.fromFoldable [n..n+value]
92{-# INLINE sourceFromFoldableM #-}
93sourceFromFoldableM :: (S.IsStream t, S.MonadAsync m) => Int -> t m Int
94sourceFromFoldableM n = S.fromFoldableM (Prelude.fmap return [n..n+value])
96{-# INLINE sourceFoldMapWith #-}
97sourceFoldMapWith :: (S.IsStream t, S.Semigroup (t m Int))
98 => Int -> t m Int
99sourceFoldMapWith n = S.foldMapWith (S.<>) S.yield [n..n+value]
101{-# INLINE sourceFoldMapWithM #-}
102sourceFoldMapWithM :: (S.IsStream t, Monad m, S.Semigroup (t m Int))
103 => Int -> t m Int
104sourceFoldMapWithM n = S.foldMapWith (S.<>) (S.yieldM . return) [n..n+value]
106{-# INLINE sourceFoldMapM #-}
107sourceFoldMapM :: (S.IsStream t, Monad m, P.Monoid (t m Int))
108 => Int -> t m Int
109sourceFoldMapM n = F.foldMap (S.yieldM . return) [n..n+value]
111{-# INLINE sourceUnfoldr #-}
112sourceUnfoldr :: (Monad m, S.IsStream t) => Int -> t m Int
113sourceUnfoldr n = S.unfoldr step n
114 where
115 step cnt =
116 if cnt > n + value
117 then Nothing
118 else Just (cnt, cnt + 1)
120{-# INLINE sourceUnfoldrM #-}
121sourceUnfoldrM :: (S.IsStream t, S.MonadAsync m) => Int -> t m Int
122sourceUnfoldrM n = S.unfoldrM step n
123 where
124 step cnt =
125 if cnt > n + value
126 then return Nothing
127 else return (Just (cnt, cnt + 1))
129{-# INLINE sourceUnfoldrMN #-}
130sourceUnfoldrMN :: (S.IsStream t, S.MonadAsync m) => Int -> Int -> t m Int
131sourceUnfoldrMN m n = S.unfoldrM step n
132 where
133 step cnt =
134 if cnt > n + m
135 then return Nothing
136 else return (Just (cnt, cnt + 1))
138{-# INLINE sourceUnfoldrMAction #-}
139sourceUnfoldrMAction :: (S.IsStream t, S.MonadAsync m) => Int -> t m (m Int)
140sourceUnfoldrMAction n = S.serially $ S.unfoldrM step n
141 where
142 step cnt =
143 if cnt > n + value
144 then return Nothing
145 else return (Just (return cnt, cnt + 1))
147-------------------------------------------------------------------------------
148-- Pure stream generation
149-------------------------------------------------------------------------------
151{-# INLINE sourceIsList #-}
152sourceIsList :: Int -> S.SerialT Identity Int
153sourceIsList n = GHC.fromList [n..n+value]
155{-# INLINE sourceIsString #-}
156sourceIsString :: Int -> S.SerialT Identity P.Char
157sourceIsString n = GHC.fromString (P.replicate (n + value) 'a')
159-------------------------------------------------------------------------------
160-- Elimination
161-------------------------------------------------------------------------------
163{-# INLINE runStream #-}
164runStream :: Monad m => Stream m a -> m ()
165runStream = S.drain
167{-# INLINE toList #-}
168toList :: Monad m => Stream m Int -> m [Int]
170{-# INLINE head #-}
171{-# INLINE last #-}
172{-# INLINE maximum #-}
173{-# INLINE minimum #-}
174{-# INLINE find #-}
175{-# INLINE findIndex #-}
176{-# INLINE elemIndex #-}
177{-# INLINE foldl1'Reduce #-}
178head, last, minimum, maximum, find, findIndex, elemIndex, foldl1'Reduce
179 :: Monad m => Stream m Int -> m (Maybe Int)
181{-# INLINE minimumBy #-}
182{-# INLINE maximumBy #-}
183minimumBy, maximumBy :: Monad m => Stream m Int -> m (Maybe Int)
185{-# INLINE foldl'Reduce #-}
186{-# INLINE foldl'ReduceMap #-}
187{-# INLINE foldlM'Reduce #-}
188{-# INLINE foldrMReduce #-}
189{-# INLINE length #-}
190{-# INLINE sum #-}
191{-# INLINE product #-}
192foldl'Reduce, foldl'ReduceMap, foldlM'Reduce, foldrMReduce, length, sum, product
193 :: Monad m
194 => Stream m Int -> m Int
196{-# INLINE foldl'Build #-}
197{-# INLINE foldlM'Build #-}
198{-# INLINE foldrMBuild #-}
199foldrMBuild, foldl'Build, foldlM'Build
200 :: Monad m
201 => Stream m Int -> m [Int]
203{-# INLINE all #-}
204{-# INLINE any #-}
205{-# INLINE and #-}
206{-# INLINE or #-}
207{-# INLINE null #-}
208{-# INLINE elem #-}
209{-# INLINE notElem #-}
210null, elem, notElem, all, any, and, or :: Monad m => Stream m Int -> m Bool
212{-# INLINE toNull #-}
213toNull :: Monad m => (t m a -> S.SerialT m a) -> t m a -> m ()
214toNull t = runStream . t
216{-# INLINE uncons #-}
217uncons :: Monad m => Stream m Int -> m ()
218uncons s = do
219 r <- S.uncons s
220 case r of
221 Nothing -> return ()
222 Just (_, t) -> uncons t
224{-# INLINE init #-}
225init :: Monad m => Stream m a -> m ()
226init s = S.init s >>= Prelude.mapM_ S.drain
228{-# INLINE tail #-}
229tail :: Monad m => Stream m a -> m ()
230tail s = S.tail s >>= Prelude.mapM_ tail
232{-# INLINE nullHeadTail #-}
233nullHeadTail :: Monad m => Stream m Int -> m ()
234nullHeadTail s = do
235 r <- S.null s
236 when (not r) $ do
237 _ <- S.head s
238 S.tail s >>= Prelude.mapM_ nullHeadTail
240{-# INLINE mapM_ #-}
241mapM_ :: Monad m => Stream m Int -> m ()
242mapM_ = S.mapM_ (\_ -> return ())
244toList = S.toList
246{-
247{-# INLINE toRevList #-}
248toRevList :: Monad m => Stream m Int -> m [Int]
249toRevList = S.toRevList
250-}
252foldrMBuild = S.foldrM (\x xs -> xs >>= return . (x :)) (return [])
253foldl'Build = S.foldl' (flip (:)) []
254foldlM'Build = S.foldlM' (\xs x -> return $ x : xs) []
256foldrMReduce = S.foldrM (\x xs -> xs >>= return . (x +)) (return 0)
257foldl'Reduce = S.foldl' (+) 0
258foldl'ReduceMap = P.fmap (+1) . S.foldl' (+) 0
259foldl1'Reduce = S.foldl1' (+)
260foldlM'Reduce = S.foldlM' (\xs a -> return $ a + xs) 0
262last = S.last
263null = S.null
264head = S.head
265elem = S.elem maxValue
266notElem = S.notElem maxValue
267length = S.length
268all = S.all (<= maxValue)
269any = S.any (> maxValue)
270and = S.and . S.map (<= maxValue)
271or = S.or . S.map (> maxValue)
272find = S.find (== maxValue)
273findIndex = S.findIndex (== maxValue)
274elemIndex = S.elemIndex maxValue
275maximum = S.maximum
276minimum = S.minimum
277sum = S.sum
278product = S.product
279minimumBy = S.minimumBy compare
280maximumBy = S.maximumBy compare
282-------------------------------------------------------------------------------
283-- Transformation
284-------------------------------------------------------------------------------
286{-# INLINE transform #-}
287transform :: Monad m => Stream m a -> m ()
288transform = runStream
290{-# INLINE composeN #-}
291composeN
292 :: MonadIO m
293 => Int -> (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
294composeN n f =
295 case n of
296 1 -> transform . f
297 2 -> transform . f . f
298 3 -> transform . f . f . f
299 4 -> transform . f . f . f . f
300 _ -> undefined
302-- polymorphic stream version of composeN
303{-# INLINE composeN' #-}
304composeN'
305 :: (S.IsStream t, Monad m)
306 => Int -> (t m Int -> Stream m Int) -> t m Int -> m ()
307composeN' n f =
308 case n of
309 1 -> transform . f
310 2 -> transform . f . S.adapt . f
311 3 -> transform . f . S.adapt . f . S.adapt . f
312 4 -> transform . f . S.adapt . f . S.adapt . f . S.adapt . f
313 _ -> undefined
315{-# INLINE scan #-}
316{-# INLINE scanl1' #-}
317{-# INLINE map #-}
318{-# INLINE fmap #-}
319{-# INLINE mapMaybe #-}
320{-# INLINE filterEven #-}
321{-# INLINE filterAllOut #-}
322{-# INLINE filterAllIn #-}
323{-# INLINE takeOne #-}
324{-# INLINE takeAll #-}
325{-# INLINE takeWhileTrue #-}
326{-# INLINE takeWhileMTrue #-}
327{-# INLINE dropOne #-}
328{-# INLINE dropAll #-}
329{-# INLINE dropWhileTrue #-}
330{-# INLINE dropWhileMTrue #-}
331{-# INLINE dropWhileFalse #-}
332{-# INLINE findIndices #-}
333{-# INLINE elemIndices #-}
334{-# INLINE insertBy #-}
335{-# INLINE deleteBy #-}
336{-# INLINE reverse #-}
337{-# INLINE reverse' #-}
338{-# INLINE foldrS #-}
339{-# INLINE foldrSMap #-}
340{-# INLINE foldrT #-}
341{-# INLINE foldrTMap #-}
342scan, scanl1', map, fmap, mapMaybe, filterEven, filterAllOut,
343 filterAllIn, takeOne, takeAll, takeWhileTrue, takeWhileMTrue, dropOne,
344 dropAll, dropWhileTrue, dropWhileMTrue, dropWhileFalse,
345 findIndices, elemIndices, insertBy, deleteBy, reverse, reverse',
346 foldrS, foldrSMap, foldrT, foldrTMap
347 :: MonadIO m
348 => Int -> Stream m Int -> m ()
350{-# INLINE mapMaybeM #-}
351mapMaybeM :: S.MonadAsync m => Int -> Stream m Int -> m ()
353{-# INLINE mapM #-}
354{-# INLINE map' #-}
355{-# INLINE fmap' #-}
356mapM, map' :: (S.IsStream t, S.MonadAsync m)
357 => (t m Int -> S.SerialT m Int) -> Int -> t m Int -> m ()
359fmap' :: (S.IsStream t, S.MonadAsync m, P.Functor (t m))
360 => (t m Int -> S.SerialT m Int) -> Int -> t m Int -> m ()
362{-# INLINE sequence #-}
363sequence :: (S.IsStream t, S.MonadAsync m)
364 => (t m Int -> S.SerialT m Int) -> t m (m Int) -> m ()
366scan n = composeN n $ S.scanl' (+) 0
367scanl1' n = composeN n $ S.scanl1' (+)
368fmap n = composeN n $ Prelude.fmap (+1)
369fmap' t n = composeN' n $ t . Prelude.fmap (+1)
370map n = composeN n $ S.map (+1)
371map' t n = composeN' n $ t . S.map (+1)
372mapM t n = composeN' n $ t . S.mapM return
373mapMaybe n = composeN n $ S.mapMaybe
374 (\x -> if Prelude.odd x then Nothing else Just x)
375mapMaybeM n = composeN n $ S.mapMaybeM
376 (\x -> if Prelude.odd x then return Nothing else return $ Just x)
377sequence t = transform . t . S.sequence
378filterEven n = composeN n $ S.filter even
379filterAllOut n = composeN n $ S.filter (> maxValue)
380filterAllIn n = composeN n $ S.filter (<= maxValue)
381takeOne n = composeN n $ S.take 1
382takeAll n = composeN n $ S.take maxValue
383takeWhileTrue n = composeN n $ S.takeWhile (<= maxValue)
384takeWhileMTrue n = composeN n $ S.takeWhileM (return . (<= maxValue))
385dropOne n = composeN n $ S.drop 1
386dropAll n = composeN n $ S.drop maxValue
387dropWhileTrue n = composeN n $ S.dropWhile (<= maxValue)
388dropWhileMTrue n = composeN n $ S.dropWhileM (return . (<= maxValue))
389dropWhileFalse n = composeN n $ S.dropWhile (> maxValue)
390findIndices n = composeN n $ S.findIndices (== maxValue)
391elemIndices n = composeN n $ S.elemIndices maxValue
392insertBy n = composeN n $ S.insertBy compare maxValue
393deleteBy n = composeN n $ S.deleteBy (>=) maxValue
394reverse n = composeN n $ S.reverse
395reverse' n = composeN n $ S.reverse'
396foldrS n = composeN n $ S.foldrS S.cons S.nil
397foldrSMap n = composeN n $ S.foldrS (\x xs -> x + 1 `S.cons` xs) S.nil
398foldrT n = composeN n $ S.foldrT S.cons S.nil
399foldrTMap n = composeN n $ S.foldrT (\x xs -> x + 1 `S.cons` xs) S.nil
401-------------------------------------------------------------------------------
402-- Iteration
403-------------------------------------------------------------------------------
405iterStreamLen, maxIters :: Int
406iterStreamLen = 10
407maxIters = 10000
409{-# INLINE iterateSource #-}
410iterateSource
411 :: S.MonadAsync m
412 => (Stream m Int -> Stream m Int) -> Int -> Int -> Stream m Int
413iterateSource g i n = f i (sourceUnfoldrMN iterStreamLen n)
414 where
415 f (0 :: Int) m = g m
416 f x m = g (f (x P.- 1) m)
418{-# INLINE iterateMapM #-}
419{-# INLINE iterateScan #-}
420{-# INLINE iterateScanl1 #-}
421{-# INLINE iterateFilterEven #-}
422{-# INLINE iterateTakeAll #-}
423{-# INLINE iterateDropOne #-}
424{-# INLINE iterateDropWhileFalse #-}
425{-# INLINE iterateDropWhileTrue #-}
426iterateMapM, iterateScan, iterateScanl1, iterateFilterEven, iterateTakeAll,
427 iterateDropOne, iterateDropWhileFalse, iterateDropWhileTrue
428 :: S.MonadAsync m
429 => Int -> Stream m Int
431-- this is quadratic
432iterateScan = iterateSource (S.scanl' (+) 0) (maxIters `div` 10)
433-- so is this
434iterateScanl1 = iterateSource (S.scanl1' (+)) (maxIters `div` 10)
436iterateMapM = iterateSource (S.mapM return) maxIters
437iterateFilterEven = iterateSource (S.filter even) maxIters
438iterateTakeAll = iterateSource (S.take maxValue) maxIters
439iterateDropOne = iterateSource (S.drop 1) maxIters
440iterateDropWhileFalse = iterateSource (S.dropWhile (> maxValue)) maxIters
441iterateDropWhileTrue = iterateSource (S.dropWhile (<= maxValue)) maxIters
443-------------------------------------------------------------------------------
444-- Zipping and concat
445-------------------------------------------------------------------------------
447{-# INLINE zip #-}
448{-# INLINE zipM #-}
449{-# INLINE mergeBy #-}
450zip, zipM, mergeBy :: Monad m => Stream m Int -> m ()
452zip src = do
453 r <- S.tail src
454 let src1 = fromJust r
455 transform (S.zipWith (,) src src1)
456zipM src = do
457 r <- S.tail src
458 let src1 = fromJust r
459 transform (S.zipWithM (curry return) src src1)
461mergeBy src = do
462 r <- S.tail src
463 let src1 = fromJust r
464 transform (S.mergeBy P.compare src src1)
466{-# INLINE isPrefixOf #-}
467{-# INLINE isSubsequenceOf #-}
468isPrefixOf, isSubsequenceOf :: Monad m => Stream m Int -> m Bool
470isPrefixOf src = S.isPrefixOf src src
471isSubsequenceOf src = S.isSubsequenceOf src src
473{-# INLINE stripPrefix #-}
474stripPrefix :: Monad m => Stream m Int -> m ()
475stripPrefix src = do
476 _ <- S.stripPrefix src src
477 return ()
479{-# INLINE zipAsync #-}
480{-# INLINE zipAsyncM #-}
481{-# INLINE zipAsyncAp #-}
482zipAsync, zipAsyncAp, zipAsyncM :: S.MonadAsync m => Stream m Int -> m ()
484zipAsync src = do
485 r <- S.tail src
486 let src1 = fromJust r
487 transform (S.zipAsyncWith (,) src src1)
489zipAsyncM src = do
490 r <- S.tail src
491 let src1 = fromJust r
492 transform (S.zipAsyncWithM (curry return) src src1)
494zipAsyncAp src = do
495 r <- S.tail src
496 let src1 = fromJust r
497 transform (S.zipAsyncly $ (,) <$> S.serially src
498 <*> S.serially src1)
500{-# INLINE eqBy #-}
501eqBy :: (Monad m, P.Eq a) => Stream m a -> m P.Bool
502eqBy src = S.eqBy (==) src src
504{-# INLINE cmpBy #-}
505cmpBy :: (Monad m, P.Ord a) => Stream m a -> m P.Ordering
506cmpBy src = S.cmpBy P.compare src src
508concatStreamLen, maxNested :: Int
509-- concatStreamLen = value2
510-- maxNested = value2
511concatStreamLen = 1
512maxNested = 100000
514{-# INLINE concatMap #-}
515concatMap :: S.MonadAsync m => Int -> Stream m Int
516concatMap n = S.concatMap (\_ -> sourceUnfoldrMN maxNested n)
517 (sourceUnfoldrMN concatStreamLen n)
519-------------------------------------------------------------------------------
520-- Mixed Composition
521-------------------------------------------------------------------------------
523{-# INLINE scanMap #-}
524{-# INLINE dropMap #-}
525{-# INLINE dropScan #-}
526{-# INLINE takeDrop #-}
527{-# INLINE takeScan #-}
528{-# INLINE takeMap #-}
529{-# INLINE filterDrop #-}
530{-# INLINE filterTake #-}
531{-# INLINE filterScan #-}
532{-# INLINE filterScanl1 #-}
533{-# INLINE filterMap #-}
534scanMap, dropMap, dropScan, takeDrop, takeScan, takeMap, filterDrop,
535 filterTake, filterScan, filterScanl1, filterMap
536 :: MonadIO m => Int -> Stream m Int -> m ()
538scanMap n = composeN n $ S.map (subtract 1) . S.scanl' (+) 0
539dropMap n = composeN n $ S.map (subtract 1) . S.drop 1
540dropScan n = composeN n $ S.scanl' (+) 0 . S.drop 1
541takeDrop n = composeN n $ S.drop 1 . S.take maxValue
542takeScan n = composeN n $ S.scanl' (+) 0 . S.take maxValue
543takeMap n = composeN n $ S.map (subtract 1) . S.take maxValue
544filterDrop n = composeN n $ S.drop 1 . S.filter (<= maxValue)
545filterTake n = composeN n $ S.take maxValue . S.filter (<= maxValue)
546filterScan n = composeN n $ S.scanl' (+) 0 . S.filter (<= maxBound)
547filterScanl1 n = composeN n $ S.scanl1' (+) . S.filter (<= maxBound)
548filterMap n = composeN n $ S.map (subtract 1) . S.filter (<= maxValue)
550data Pair a b = Pair !a !b deriving (Generic, NFData)
552{-# INLINE sumProductFold #-}
553sumProductFold :: Monad m => Stream m Int -> m (Int, Int)
554sumProductFold = S.foldl' (\(s,p) x -> (s + x, p P.* x)) (0,1)
556{-# INLINE sumProductScan #-}
557sumProductScan :: Monad m => Stream m Int -> m (Pair Int Int)
558sumProductScan = S.foldl' (\(Pair _ p) (s0,x) -> Pair s0 (p P.* x)) (Pair 0 1)
559 . S.scanl' (\(s,_) x -> (s + x,x)) (0,0)
561-------------------------------------------------------------------------------
562-- Pure stream operations
563-------------------------------------------------------------------------------
565{-# INLINE eqInstance #-}
566eqInstance :: Stream Identity Int -> Bool
567eqInstance src = src == src
569{-# INLINE eqInstanceNotEq #-}
570eqInstanceNotEq :: Stream Identity Int -> Bool
571eqInstanceNotEq src = src P./= src
573{-# INLINE ordInstance #-}
574ordInstance :: Stream Identity Int -> Bool
575ordInstance src = src P.< src
577{-# INLINE ordInstanceMin #-}
578ordInstanceMin :: Stream Identity Int -> Stream Identity Int
579ordInstanceMin src = P.min src src
581{-# INLINE showInstance #-}
582showInstance :: Stream Identity Int -> P.String
583showInstance src = P.show src
585{-# INLINE showInstanceList #-}
586showInstanceList :: [Int] -> P.String
587showInstanceList src = P.show src
589{-# INLINE readInstance #-}
590readInstance :: P.String -> Stream Identity Int
591readInstance str =
592 let r = P.reads str
593 in case r of
594 [(x,"")] -> x
595 _ -> P.error "readInstance: no parse"
597{-# INLINE readInstanceList #-}
598readInstanceList :: P.String -> [Int]
599readInstanceList str =
600 let r = P.reads str
601 in case r of
602 [(x,"")] -> x
603 _ -> P.error "readInstance: no parse"
605{-# INLINE pureFoldl' #-}
606pureFoldl' :: Stream Identity Int -> Int
607pureFoldl' = runIdentity . S.foldl' (+) 0
609{-# INLINE foldableFoldl' #-}
610foldableFoldl' :: Stream Identity Int -> Int
611foldableFoldl' = F.foldl' (+) 0
613{-# INLINE foldableSum #-}
614foldableSum :: Stream Identity Int -> Int
615foldableSum = P.sum
617{-# INLINE traversableMapM #-}
618traversableMapM :: Stream Identity Int -> IO (Stream Identity Int)
619traversableMapM = P.mapM return