Skip to content

support for mealy style scanners #108

@paolino

Description

@paolino

I'm finding hard time to see support for Scan like from foldl package so I made it up. Is there a reason for not supporting this ? Or I just don't see it ?

{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE NoMonomorphismRestriction #-}

import Streaming
import qualified Streaming.Prelude as S
import qualified Control.Scanl as SL
import qualified Control.Foldl as L
import Protolude
import Control.Arrow ((>>>),(&&&))

-- | support for mealy type scanners (see 'Control.Scanl' from foldl)
mealy :: Monad m => (s -> a -> (s, b)) -> s -> Stream (Of a) m () -> Stream (Of b) m ()
mealy step s q =
  effect $
    S.next q <&> either
      do pure
      do
        \(x, rest) ->
          let (s', x') = step s x
           in S.yield x' >> mealy step s' rest

track :: IO ()
track = S.print $ SL.purely_ mealy trackMean S.readLn
  where
    trackMean = ps L.mean >>> (ps L.minimum &&& ps L.maximum)
    ps = SL.postscan

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions