2018-06-23 18:27:24 +03:00
|
|
|
-- |
|
|
|
|
-- Module : StreamKOps
|
|
|
|
-- Copyright : (c) 2018 Harendra Kumar
|
|
|
|
--
|
|
|
|
-- License : BSD3
|
2019-11-06 19:25:35 +03:00
|
|
|
-- Maintainer : streamly@composewell.com
|
2018-06-23 18:27:24 +03:00
|
|
|
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
2018-11-06 00:42:08 +03:00
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
2018-06-23 18:27:24 +03:00
|
|
|
|
|
|
|
module StreamKOps where
|
|
|
|
|
2018-10-13 06:11:02 +03:00
|
|
|
import Control.Monad (when)
|
2018-10-24 07:37:02 +03:00
|
|
|
import Data.Maybe (isJust)
|
2018-06-23 18:27:24 +03:00
|
|
|
import Prelude
|
2018-11-06 00:42:08 +03:00
|
|
|
(Monad, Int, (+), ($), (.), return, even, (>), (<=), div,
|
2018-12-27 11:29:25 +03:00
|
|
|
subtract, undefined, Maybe(..), not, (>>=),
|
2019-05-27 20:59:21 +03:00
|
|
|
maxBound, flip, (<$>), (<*>), round, (/), (**), (<))
|
2018-11-06 00:42:08 +03:00
|
|
|
import qualified Prelude as P
|
2019-05-27 20:59:21 +03:00
|
|
|
import qualified Data.List as List
|
2018-06-23 18:27:24 +03:00
|
|
|
|
2019-12-09 12:44:05 +03:00
|
|
|
import qualified Streamly.Internal.Data.Stream.StreamK as S
|
2019-12-09 13:29:34 +03:00
|
|
|
import qualified Streamly.Internal.Data.Stream.Prelude as SP
|
2019-09-25 08:30:44 +03:00
|
|
|
import qualified Streamly.Internal.Data.SVar as S
|
2018-06-23 18:27:24 +03:00
|
|
|
|
2019-05-27 20:59:21 +03:00
|
|
|
value, value2, value3, value16, maxValue :: Int
|
2018-07-13 16:57:10 +03:00
|
|
|
value = 100000
|
2019-05-27 20:59:21 +03:00
|
|
|
value2 = round (P.fromIntegral value**(1/2::P.Double)) -- double nested loop
|
|
|
|
value3 = round (P.fromIntegral value**(1/3::P.Double)) -- triple nested loop
|
|
|
|
value16 = round (P.fromIntegral value**(1/16::P.Double)) -- triple nested loop
|
|
|
|
maxValue = value
|
2018-06-23 18:27:24 +03:00
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- Benchmark ops
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
|
2018-06-27 02:50:14 +03:00
|
|
|
{-# INLINE toNull #-}
|
|
|
|
{-# INLINE uncons #-}
|
2018-10-24 07:37:02 +03:00
|
|
|
{-# INLINE nullTail #-}
|
|
|
|
{-# INLINE headTail #-}
|
2018-06-23 18:27:24 +03:00
|
|
|
{-# INLINE zip #-}
|
2019-05-27 20:59:21 +03:00
|
|
|
toNull, uncons, nullTail, headTail, zip
|
2018-06-23 18:27:24 +03:00
|
|
|
:: Monad m
|
|
|
|
=> Stream m Int -> m ()
|
|
|
|
|
|
|
|
{-# INLINE toList #-}
|
|
|
|
toList :: Monad m => Stream m Int -> m [Int]
|
|
|
|
{-# INLINE foldl #-}
|
|
|
|
foldl :: Monad m => Stream m Int -> m Int
|
|
|
|
{-# INLINE last #-}
|
|
|
|
last :: Monad m => Stream m Int -> m (Maybe Int)
|
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- Stream generation and elimination
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
|
2018-07-13 15:17:01 +03:00
|
|
|
type Stream m a = S.Stream m a
|
2018-06-23 18:27:24 +03:00
|
|
|
|
|
|
|
{-# INLINE sourceUnfoldr #-}
|
|
|
|
sourceUnfoldr :: Int -> Stream m Int
|
|
|
|
sourceUnfoldr n = S.unfoldr step n
|
|
|
|
where
|
|
|
|
step cnt =
|
|
|
|
if cnt > n + value
|
|
|
|
then Nothing
|
2018-10-13 06:11:02 +03:00
|
|
|
else Just (cnt, cnt + 1)
|
2018-06-23 18:27:24 +03:00
|
|
|
|
2019-05-27 20:59:21 +03:00
|
|
|
{-# INLINE sourceUnfoldrN #-}
|
|
|
|
sourceUnfoldrN :: Int -> Int -> Stream m Int
|
|
|
|
sourceUnfoldrN m n = S.unfoldr step n
|
|
|
|
where
|
|
|
|
step cnt =
|
|
|
|
if cnt > n + m
|
|
|
|
then Nothing
|
|
|
|
else Just (cnt, cnt + 1)
|
|
|
|
|
2018-06-23 18:27:24 +03:00
|
|
|
{-# INLINE sourceUnfoldrM #-}
|
|
|
|
sourceUnfoldrM :: S.MonadAsync m => Int -> Stream m Int
|
|
|
|
sourceUnfoldrM n = S.unfoldrM step n
|
|
|
|
where
|
|
|
|
step cnt =
|
|
|
|
if cnt > n + value
|
|
|
|
then return Nothing
|
|
|
|
else return (Just (cnt, cnt + 1))
|
|
|
|
|
2018-11-06 00:42:08 +03:00
|
|
|
{-# INLINE sourceUnfoldrMN #-}
|
|
|
|
sourceUnfoldrMN :: S.MonadAsync m => Int -> Int -> Stream m Int
|
|
|
|
sourceUnfoldrMN m n = S.unfoldrM step n
|
|
|
|
where
|
|
|
|
step cnt =
|
|
|
|
if cnt > n + m
|
|
|
|
then return Nothing
|
|
|
|
else return (Just (cnt, cnt + 1))
|
|
|
|
|
2018-06-23 18:27:24 +03:00
|
|
|
{-
|
|
|
|
{-# INLINE sourceFromEnum #-}
|
|
|
|
sourceFromEnum :: Monad m => Int -> Stream m Int
|
|
|
|
sourceFromEnum n = S.enumFromStepN n 1 value
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# INLINE sourceFromFoldable #-}
|
|
|
|
sourceFromFoldable :: Int -> Stream m Int
|
|
|
|
sourceFromFoldable n = S.fromFoldable [n..n+value]
|
|
|
|
|
2018-07-13 15:17:01 +03:00
|
|
|
{-
|
2018-06-23 18:27:24 +03:00
|
|
|
{-# INLINE sourceFromFoldableM #-}
|
|
|
|
sourceFromFoldableM :: S.MonadAsync m => Int -> Stream m Int
|
2018-07-13 15:17:01 +03:00
|
|
|
sourceFromFoldableM n = S.fromFoldableM (Prelude.fmap return [n..n+value])
|
|
|
|
-}
|
2018-06-23 18:27:24 +03:00
|
|
|
|
|
|
|
{-# INLINE sourceFoldMapWith #-}
|
2018-07-13 15:17:01 +03:00
|
|
|
sourceFoldMapWith :: Int -> Stream m Int
|
2018-11-02 15:28:30 +03:00
|
|
|
sourceFoldMapWith n = SP.foldMapWith S.serial S.yield [n..n+value]
|
2018-06-23 18:27:24 +03:00
|
|
|
|
|
|
|
{-# INLINE sourceFoldMapWithM #-}
|
|
|
|
sourceFoldMapWithM :: Monad m => Int -> Stream m Int
|
2018-11-02 15:28:30 +03:00
|
|
|
sourceFoldMapWithM n = SP.foldMapWith S.serial (S.yieldM . return) [n..n+value]
|
2018-06-23 18:27:24 +03:00
|
|
|
|
|
|
|
{-# INLINE source #-}
|
|
|
|
source :: S.MonadAsync m => Int -> Stream m Int
|
2018-10-13 06:11:02 +03:00
|
|
|
source = sourceUnfoldrM
|
2018-06-23 18:27:24 +03:00
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- Elimination
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
{-# INLINE runStream #-}
|
|
|
|
runStream :: Monad m => Stream m a -> m ()
|
2019-05-12 23:35:22 +03:00
|
|
|
runStream = S.drain
|
2019-05-27 20:59:21 +03:00
|
|
|
-- runStream = S.mapM_ (\_ -> return ())
|
2018-06-23 18:27:24 +03:00
|
|
|
|
2018-12-27 11:29:25 +03:00
|
|
|
{-# INLINE mapM_ #-}
|
|
|
|
mapM_ :: Monad m => Stream m a -> m ()
|
|
|
|
mapM_ = S.mapM_ (\_ -> return ())
|
|
|
|
|
2018-06-23 18:27:24 +03:00
|
|
|
toNull = runStream
|
2018-06-27 02:50:14 +03:00
|
|
|
uncons s = do
|
|
|
|
r <- S.uncons s
|
|
|
|
case r of
|
|
|
|
Nothing -> return ()
|
|
|
|
Just (_, t) -> uncons t
|
|
|
|
|
2018-07-25 22:12:53 +03:00
|
|
|
{-# INLINE init #-}
|
|
|
|
init :: (Monad m, S.IsStream t) => t m a -> m ()
|
|
|
|
init s = do
|
2018-10-13 06:11:02 +03:00
|
|
|
t <- S.init s
|
2019-05-12 23:35:22 +03:00
|
|
|
P.mapM_ S.drain t
|
2018-07-25 22:12:53 +03:00
|
|
|
|
|
|
|
{-# INLINE tail #-}
|
|
|
|
tail :: (Monad m, S.IsStream t) => t m a -> m ()
|
2018-12-27 11:29:25 +03:00
|
|
|
tail s = S.tail s >>= P.mapM_ tail
|
2018-07-25 22:12:53 +03:00
|
|
|
|
2018-10-24 07:37:02 +03:00
|
|
|
nullTail s = do
|
2018-06-27 02:50:14 +03:00
|
|
|
r <- S.null s
|
2018-12-27 11:29:25 +03:00
|
|
|
when (not r) $ S.tail s >>= P.mapM_ nullTail
|
2018-10-24 07:37:02 +03:00
|
|
|
|
|
|
|
headTail s = do
|
|
|
|
h <- S.head s
|
2018-12-27 11:29:25 +03:00
|
|
|
when (isJust h) $ S.tail s >>= P.mapM_ headTail
|
2018-06-27 02:50:14 +03:00
|
|
|
|
2018-06-23 18:27:24 +03:00
|
|
|
toList = S.toList
|
|
|
|
foldl = S.foldl' (+) 0
|
|
|
|
last = S.last
|
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- Transformation
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
{-# INLINE transform #-}
|
|
|
|
transform :: Monad m => Stream m a -> m ()
|
|
|
|
transform = runStream
|
|
|
|
|
2018-10-29 16:32:41 +03:00
|
|
|
{-# INLINE composeN #-}
|
|
|
|
composeN
|
|
|
|
:: Monad m
|
|
|
|
=> Int -> (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
|
|
|
|
composeN n f =
|
|
|
|
case n of
|
|
|
|
1 -> transform . f
|
|
|
|
2 -> transform . f . f
|
|
|
|
3 -> transform . f . f . f
|
|
|
|
4 -> transform . f . f . f . f
|
|
|
|
_ -> undefined
|
|
|
|
|
|
|
|
{-# INLINE scan #-}
|
|
|
|
{-# INLINE map #-}
|
2018-11-06 00:42:08 +03:00
|
|
|
{-# INLINE fmap #-}
|
2018-10-29 16:32:41 +03:00
|
|
|
{-# INLINE filterEven #-}
|
|
|
|
{-# INLINE filterAllOut #-}
|
|
|
|
{-# INLINE filterAllIn #-}
|
|
|
|
{-# INLINE takeOne #-}
|
|
|
|
{-# INLINE takeAll #-}
|
|
|
|
{-# INLINE takeWhileTrue #-}
|
2018-11-02 18:35:21 +03:00
|
|
|
{-# INLINE dropOne #-}
|
2018-10-29 16:32:41 +03:00
|
|
|
{-# INLINE dropAll #-}
|
|
|
|
{-# INLINE dropWhileTrue #-}
|
2018-11-02 18:35:21 +03:00
|
|
|
{-# INLINE dropWhileFalse #-}
|
2019-05-03 08:05:28 +03:00
|
|
|
{-# INLINE foldlS #-}
|
2019-05-27 20:59:21 +03:00
|
|
|
{-# INLINE concatMap #-}
|
2018-11-06 00:42:08 +03:00
|
|
|
scan, map, fmap, filterEven, filterAllOut,
|
2018-11-02 18:35:21 +03:00
|
|
|
filterAllIn, takeOne, takeAll, takeWhileTrue, dropAll, dropOne,
|
2019-05-27 20:59:21 +03:00
|
|
|
dropWhileTrue, dropWhileFalse, foldlS, concatMap
|
2018-10-29 16:32:41 +03:00
|
|
|
:: Monad m
|
|
|
|
=> Int -> Stream m Int -> m ()
|
|
|
|
|
|
|
|
{-# INLINE mapM #-}
|
2019-05-27 20:59:21 +03:00
|
|
|
{-# INLINE mapMSerial #-}
|
2019-06-10 11:45:07 +03:00
|
|
|
{-# INLINE intersperse #-}
|
|
|
|
mapM, mapMSerial, intersperse
|
|
|
|
:: S.MonadAsync m => Int -> Stream m Int -> m ()
|
2018-10-29 16:32:41 +03:00
|
|
|
|
2018-11-02 18:35:21 +03:00
|
|
|
scan n = composeN n $ S.scanl' (+) 0
|
2018-11-06 00:42:08 +03:00
|
|
|
map n = composeN n $ P.fmap (+1)
|
|
|
|
fmap n = composeN n $ P.fmap (+1)
|
2018-11-02 18:35:21 +03:00
|
|
|
mapM n = composeN n $ S.mapM return
|
2019-05-27 20:59:21 +03:00
|
|
|
mapMSerial n = composeN n $ S.mapMSerial return
|
2018-11-02 18:35:21 +03:00
|
|
|
filterEven n = composeN n $ S.filter even
|
|
|
|
filterAllOut n = composeN n $ S.filter (> maxValue)
|
|
|
|
filterAllIn n = composeN n $ S.filter (<= maxValue)
|
|
|
|
takeOne n = composeN n $ S.take 1
|
|
|
|
takeAll n = composeN n $ S.take maxValue
|
|
|
|
takeWhileTrue n = composeN n $ S.takeWhile (<= maxValue)
|
|
|
|
dropOne n = composeN n $ S.drop 1
|
|
|
|
dropAll n = composeN n $ S.drop maxValue
|
|
|
|
dropWhileTrue n = composeN n $ S.dropWhile (<= maxValue)
|
|
|
|
dropWhileFalse n = composeN n $ S.dropWhile (<= 1)
|
2019-05-03 08:05:28 +03:00
|
|
|
foldlS n = composeN n $ S.foldlS (flip S.cons) S.nil
|
2019-07-26 16:34:50 +03:00
|
|
|
-- We use a (sqrt n) element stream as source and then concat the same stream
|
|
|
|
-- for each element to produce an n element stream.
|
2019-05-27 20:59:21 +03:00
|
|
|
concatMap n = composeN n $ (\s -> S.concatMap (\_ -> s) s)
|
2019-06-10 11:45:07 +03:00
|
|
|
intersperse n = composeN n $ S.intersperse maxValue
|
2018-06-23 18:27:24 +03:00
|
|
|
|
2018-11-06 00:42:08 +03:00
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- Iteration
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
iterStreamLen, maxIters :: Int
|
|
|
|
iterStreamLen = 10
|
|
|
|
maxIters = 10000
|
|
|
|
|
|
|
|
{-# INLINE iterateSource #-}
|
|
|
|
iterateSource
|
|
|
|
:: S.MonadAsync m
|
|
|
|
=> (Stream m Int -> Stream m Int) -> Int -> Int -> Stream m Int
|
|
|
|
iterateSource g i n = f i (sourceUnfoldrMN iterStreamLen n)
|
|
|
|
where
|
|
|
|
f (0 :: Int) m = g m
|
|
|
|
f x m = g (f (x P.- 1) m)
|
|
|
|
|
|
|
|
{-# INLINE iterateMapM #-}
|
|
|
|
{-# INLINE iterateScan #-}
|
|
|
|
{-# INLINE iterateFilterEven #-}
|
|
|
|
{-# INLINE iterateTakeAll #-}
|
|
|
|
{-# INLINE iterateDropOne #-}
|
|
|
|
{-# INLINE iterateDropWhileFalse #-}
|
|
|
|
{-# INLINE iterateDropWhileTrue #-}
|
|
|
|
iterateMapM, iterateScan, iterateFilterEven, iterateTakeAll, iterateDropOne,
|
|
|
|
iterateDropWhileFalse, iterateDropWhileTrue
|
|
|
|
:: S.MonadAsync m
|
|
|
|
=> Int -> Stream m Int
|
|
|
|
|
|
|
|
-- this is quadratic
|
|
|
|
iterateScan = iterateSource (S.scanl' (+) 0) (maxIters `div` 10)
|
|
|
|
iterateDropWhileFalse = iterateSource (S.dropWhile (> maxValue))
|
|
|
|
(maxIters `div` 10)
|
|
|
|
|
|
|
|
iterateMapM = iterateSource (S.mapM return) maxIters
|
|
|
|
iterateFilterEven = iterateSource (S.filter even) maxIters
|
|
|
|
iterateTakeAll = iterateSource (S.take maxValue) maxIters
|
|
|
|
iterateDropOne = iterateSource (S.drop 1) maxIters
|
|
|
|
iterateDropWhileTrue = iterateSource (S.dropWhile (<= maxValue)) maxIters
|
|
|
|
|
2018-06-23 18:27:24 +03:00
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- Zipping and concat
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
|
2018-10-13 06:11:02 +03:00
|
|
|
zip src = transform $ S.zipWith (,) src src
|
2018-06-23 18:27:24 +03:00
|
|
|
|
2019-07-26 16:34:50 +03:00
|
|
|
{-# INLINE concatMapRepl4xN #-}
|
|
|
|
concatMapRepl4xN :: Monad m => Stream m Int -> m ()
|
|
|
|
concatMapRepl4xN src = transform $ (S.concatMap (S.replicate 4) src)
|
|
|
|
|
2018-06-23 18:27:24 +03:00
|
|
|
-------------------------------------------------------------------------------
|
2018-10-29 16:32:41 +03:00
|
|
|
-- Mixed Composition
|
2018-06-23 18:27:24 +03:00
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
|
2018-10-29 16:32:41 +03:00
|
|
|
{-# INLINE scanMap #-}
|
|
|
|
{-# INLINE dropMap #-}
|
|
|
|
{-# INLINE dropScan #-}
|
|
|
|
{-# INLINE takeDrop #-}
|
|
|
|
{-# INLINE takeScan #-}
|
|
|
|
{-# INLINE takeMap #-}
|
|
|
|
{-# INLINE filterDrop #-}
|
|
|
|
{-# INLINE filterTake #-}
|
|
|
|
{-# INLINE filterScan #-}
|
|
|
|
{-# INLINE filterMap #-}
|
|
|
|
scanMap, dropMap, dropScan, takeDrop, takeScan, takeMap, filterDrop,
|
|
|
|
filterTake, filterScan, filterMap
|
|
|
|
:: Monad m => Int -> Stream m Int -> m ()
|
|
|
|
|
|
|
|
scanMap n = composeN n $ S.map (subtract 1) . S.scanl' (+) 0
|
|
|
|
dropMap n = composeN n $ S.map (subtract 1) . S.drop 1
|
|
|
|
dropScan n = composeN n $ S.scanl' (+) 0 . S.drop 1
|
|
|
|
takeDrop n = composeN n $ S.drop 1 . S.take maxValue
|
|
|
|
takeScan n = composeN n $ S.scanl' (+) 0 . S.take maxValue
|
|
|
|
takeMap n = composeN n $ S.map (subtract 1) . S.take maxValue
|
|
|
|
filterDrop n = composeN n $ S.drop 1 . S.filter (<= maxValue)
|
|
|
|
filterTake n = composeN n $ S.take maxValue . S.filter (<= maxValue)
|
|
|
|
filterScan n = composeN n $ S.scanl' (+) 0 . S.filter (<= maxBound)
|
|
|
|
filterMap n = composeN n $ S.map (subtract 1) . S.filter (<= maxValue)
|
2019-05-27 20:59:21 +03:00
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- Nested Composition
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
{-# INLINE toNullApNested #-}
|
|
|
|
toNullApNested :: Monad m => Stream m Int -> m ()
|
|
|
|
toNullApNested s = runStream $ do
|
|
|
|
(+) <$> s <*> s
|
|
|
|
|
|
|
|
{-# INLINE toNullNested #-}
|
|
|
|
toNullNested :: Monad m => Stream m Int -> m ()
|
|
|
|
toNullNested s = runStream $ do
|
|
|
|
x <- s
|
|
|
|
y <- s
|
|
|
|
return $ x + y
|
|
|
|
|
|
|
|
{-# INLINE toNullNested3 #-}
|
|
|
|
toNullNested3 :: Monad m => Stream m Int -> m ()
|
|
|
|
toNullNested3 s = runStream $ do
|
|
|
|
x <- s
|
|
|
|
y <- s
|
|
|
|
z <- s
|
|
|
|
return $ x + y + z
|
|
|
|
|
|
|
|
{-# INLINE filterAllOutNested #-}
|
|
|
|
filterAllOutNested
|
|
|
|
:: Monad m
|
|
|
|
=> Stream m Int -> m ()
|
|
|
|
filterAllOutNested str = runStream $ do
|
|
|
|
x <- str
|
|
|
|
y <- str
|
|
|
|
let s = x + y
|
|
|
|
if s < 0
|
|
|
|
then return s
|
|
|
|
else S.nil
|
|
|
|
|
|
|
|
{-# INLINE filterAllInNested #-}
|
|
|
|
filterAllInNested
|
|
|
|
:: Monad m
|
|
|
|
=> Stream m Int -> m ()
|
|
|
|
filterAllInNested str = runStream $ do
|
|
|
|
x <- str
|
|
|
|
y <- str
|
|
|
|
let s = x + y
|
|
|
|
if s > 0
|
|
|
|
then return s
|
|
|
|
else S.nil
|
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- Nested Composition Pure lists
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
{-# INLINE sourceUnfoldrList #-}
|
|
|
|
sourceUnfoldrList :: Int -> Int -> [Int]
|
|
|
|
sourceUnfoldrList maxval n = List.unfoldr step n
|
|
|
|
where
|
|
|
|
step cnt =
|
|
|
|
if cnt > n + maxval
|
|
|
|
then Nothing
|
|
|
|
else Just (cnt, cnt + 1)
|
|
|
|
|
|
|
|
{-# INLINE toNullApNestedList #-}
|
|
|
|
toNullApNestedList :: [Int] -> [Int]
|
|
|
|
toNullApNestedList s = (+) <$> s <*> s
|
|
|
|
|
|
|
|
{-# INLINE toNullNestedList #-}
|
|
|
|
toNullNestedList :: [Int] -> [Int]
|
|
|
|
toNullNestedList s = do
|
|
|
|
x <- s
|
|
|
|
y <- s
|
|
|
|
return $ x + y
|
|
|
|
|
|
|
|
{-# INLINE toNullNestedList3 #-}
|
|
|
|
toNullNestedList3 :: [Int] -> [Int]
|
|
|
|
toNullNestedList3 s = do
|
|
|
|
x <- s
|
|
|
|
y <- s
|
|
|
|
z <- s
|
|
|
|
return $ x + y + z
|
|
|
|
|
|
|
|
{-# INLINE filterAllOutNestedList #-}
|
|
|
|
filterAllOutNestedList :: [Int] -> [Int]
|
|
|
|
filterAllOutNestedList str = do
|
|
|
|
x <- str
|
|
|
|
y <- str
|
|
|
|
let s = x + y
|
|
|
|
if s < 0
|
|
|
|
then return s
|
|
|
|
else []
|
|
|
|
|
|
|
|
{-# INLINE filterAllInNestedList #-}
|
|
|
|
filterAllInNestedList :: [Int] -> [Int]
|
|
|
|
filterAllInNestedList str = do
|
|
|
|
x <- str
|
|
|
|
y <- str
|
|
|
|
let s = x + y
|
|
|
|
if s > 0
|
|
|
|
then return s
|
|
|
|
else []
|