-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2810 from composewell/scanl-module
Add a Scanl module representing left scans
- Loading branch information
Showing
29 changed files
with
6,821 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{- $setup | ||
>>> :m | ||
>>> :set -XFlexibleContexts | ||
>>> import Control.Monad (void) | ||
>>> import qualified Data.Foldable as Foldable | ||
>>> import Data.Bifunctor(bimap) | ||
>>> import Data.Function ((&)) | ||
>>> import Data.Functor.Identity (Identity, runIdentity) | ||
>>> import Data.IORef (newIORef, readIORef, writeIORef) | ||
>>> import Data.Maybe (fromJust, isJust) | ||
>>> import Data.Monoid (Endo(..), Last(..), Sum(..)) | ||
>>> import Prelude hiding (length, sum, minimum, maximum) | ||
>>> import Streamly.Data.Array (Array) | ||
>>> import Streamly.Data.Fold (Fold, Tee(..)) | ||
>>> import Streamly.Data.Stream (Stream) | ||
>>> import qualified Data.Map as Map | ||
>>> import qualified Data.Set as Set | ||
>>> import qualified Data.IntSet as IntSet | ||
>>> import qualified Streamly.Data.Array as Array | ||
>>> import qualified Streamly.Data.Fold as Fold | ||
>>> import qualified Streamly.Data.MutArray as MutArray | ||
>>> import qualified Streamly.Data.Parser as Parser | ||
>>> import qualified Streamly.Data.Scanl as Scanl | ||
>>> import qualified Streamly.Data.Stream as Stream | ||
>>> import qualified Streamly.Data.StreamK as StreamK | ||
>>> import qualified Streamly.Data.Unfold as Unfold | ||
For APIs that have not been released yet. | ||
>>> import qualified Streamly.Internal.Data.Fold as Fold | ||
>>> import qualified Streamly.Internal.Data.Ring as Ring | ||
>>> import qualified Streamly.Internal.Data.Scanl as Scanl | ||
>>> import qualified Streamly.Internal.Data.Stream as Stream | ||
-} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
{-# LANGUAGE CPP #-} | ||
-- | | ||
-- Module : Streamly.Data.Scanl | ||
-- Copyright : (c) 2019 Composewell Technologies | ||
-- License : BSD-3-Clause | ||
-- Maintainer : [email protected] | ||
-- Stability : released | ||
-- Portability : GHC | ||
-- | ||
|
||
module Streamly.Data.Scanl | ||
( | ||
-- * Setup | ||
-- | To execute the code examples provided in this module in ghci, please | ||
-- run the following commands first. | ||
-- | ||
-- $setup | ||
|
||
-- * Scanl Type | ||
|
||
Scanl -- (..) | ||
|
||
-- * Constructors | ||
, mkScanl | ||
, mkScanlM | ||
, mkScanl1 | ||
, mkScanl1M | ||
, mkScanr | ||
|
||
-- * Scans | ||
-- ** Accumulators | ||
-- | Scans that never terminate, these scans are much like strict left | ||
-- folds. 'mconcat' is the fundamental accumulator. All other accumulators | ||
-- can be expressed in terms of 'mconcat' using a suitable Monoid. Instead | ||
-- of writing scans we could write Monoids and turn them into scans. | ||
|
||
-- Monoids | ||
, sconcat | ||
, mconcat | ||
, foldMap | ||
, foldMapM | ||
|
||
-- Reducers | ||
, drain | ||
-- , drainMapM | ||
, length | ||
, countDistinct | ||
, countDistinctInt | ||
-- , frequency | ||
, sum | ||
, product | ||
, mean | ||
, rollingHash | ||
, rollingHashWithSalt | ||
|
||
-- Collectors | ||
, toList | ||
, toListRev | ||
, toSet | ||
, toIntSet | ||
, topBy | ||
|
||
-- ** Non-Empty Accumulators | ||
-- | Accumulators that do not have a default value, therefore, return | ||
-- 'Nothing' on an empty stream. | ||
, latest | ||
, maximumBy | ||
, maximum | ||
, minimumBy | ||
, minimum | ||
|
||
-- ** Filtering Scanners | ||
-- | Accumulators that are usually run as a scan using the 'potscanlMaybe' | ||
-- combinator. | ||
, findIndices | ||
, elemIndices | ||
, deleteBy | ||
-- , uniq | ||
, uniqBy | ||
, nub | ||
, nubInt | ||
|
||
-- ** Terminating Folds | ||
-- , satisfy | ||
-- , maybe | ||
|
||
, the | ||
|
||
-- * Transformations | ||
-- | Transformations are modifiers of scans. In the type @Scan m a b@, @a@ | ||
-- is the input type and @b@ is the output type. Transformations can be | ||
-- applied either on the input side (contravariant) or on the output side | ||
-- (covariant). Therefore, transformations have one of the following | ||
-- general shapes: | ||
-- | ||
-- * @... -> Scanl m a b -> Scanl m c b@ (input transformation) | ||
-- * @... -> Scanl m a b -> Scanl m a c@ (output transformation) | ||
-- | ||
-- The input side transformations are more interesting for scans. Most of | ||
-- the following sections describe the input transformation operations on a | ||
-- scan. When an operation makes sense on both input and output side we use | ||
-- the prefix @l@ (for left) for input side operations and the prefix @r@ | ||
-- (for right) for output side operations. | ||
|
||
-- ** Mapping on output | ||
-- | The 'Functor' instance of a scan maps on the output of the scan: | ||
-- | ||
-- >>> Stream.toList $ Stream.scanl (fmap show Scanl.sum) (Stream.enumerateFromTo 1 10) | ||
-- ["0","1","3","6","10","15","21","28","36","45","55"] | ||
-- | ||
, rmapM | ||
|
||
-- ** Mapping on Input | ||
, lmap | ||
, lmapM | ||
|
||
-- ** Filtering | ||
, filter | ||
, filterM | ||
|
||
-- -- ** Mapping Filters | ||
, mapMaybe | ||
, catMaybes | ||
, catLefts | ||
, catRights | ||
, catEithers | ||
|
||
-- ** Trimming | ||
, take | ||
, takeEndBy | ||
, takeEndBy_ | ||
|
||
-- ** Key-value Scanners | ||
, classify | ||
, classifyIO | ||
|
||
-- ** Transforming the Monad | ||
, morphInner | ||
|
||
-- * Combinators | ||
-- | Transformations that combine two or more scans. | ||
|
||
-- ** Scanning | ||
, scanl | ||
, postscanl | ||
, postscanlMaybe | ||
|
||
-- ** Parallel Distribution | ||
-- | The 'Applicative' instance distributes the input to both scans. | ||
|
||
, teeWith | ||
--, teeWithFst | ||
--, teeWithMin | ||
, tee | ||
, distribute | ||
|
||
-- ** Partitioning | ||
-- | Direct items in the input stream to different scans using a binary | ||
-- scan selector. | ||
|
||
, partition | ||
--, partitionByM | ||
--, partitionByFstM | ||
--, partitionByMinM | ||
--, partitionBy | ||
|
||
-- ** Unzipping | ||
, unzip | ||
|
||
-- * Dynamic Combinators | ||
-- | The scan to be used is generated dynamically based on the input. | ||
|
||
-- ** Key-value Scanners | ||
, demux | ||
, demuxIO | ||
) | ||
where | ||
|
||
import Prelude | ||
hiding (Foldable(..), filter, drop, dropWhile, take, takeWhile, zipWith, | ||
map, mapM_, sequence, all, any, | ||
notElem, head, last, tail, | ||
reverse, iterate, init, and, or, lookup, (!!), | ||
scanl, scanl1, replicate, concatMap, mconcat, unzip, | ||
span, splitAt, break, mapM, maybe) | ||
|
||
import Streamly.Internal.Data.Scanl | ||
|
||
#include "DocTestDataScanl.hs" |
Oops, something went wrong.