Skip to content

Instantly share code, notes, and snippets.

@paolino
Last active March 14, 2022 21:58
Show Gist options
  • Save paolino/9b9b2d11eabe5d3a74670ce8ee87474a to your computer and use it in GitHub Desktop.
Save paolino/9b9b2d11eabe5d3a74670ce8ee87474a to your computer and use it in GitHub Desktop.
track a streaming filter activity
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
import Control.Monad.Fix (fix)
import Data.List (tails)
import Streaming (Of ((:>)), Stream)
import qualified Streaming.Prelude as S
import Test.Hspec (shouldBe)
type Filter a =
forall n x.
Monad n
=> Stream (Of a) n x
-> Stream (Of a) n x
trackFilter
:: Monad m
=> Filter a
-> Stream (Of a) m r
-> Stream (Of a) (Stream (Of Int) m) r
trackFilter g =
-- attach a substream of (number of elements discarded by filter g + 1)
error "program me"
-- did you know all multiple of 13 in fibonacci happens every 7 elements ?
main :: IO ()
main = do
l1 :> l2 <-
S.toList
. S.toList_
. S.take 10
$ trackFilter
do selectMultiples 13
do fib
(l1, l2)
`shouldBe` ( [7, 7, 7, 7, 7, 7, 7, 7, 7, 7]
, [ 13
, 377
, 10946
, 317811
, 9227465
, 267914296
, 7778742049
, 225851433717
, 6557470319842
, 190392490709135
]
)
fib :: Monad m => Stream (Of Integer) m ()
fib = S.each do fix $ \xs -> 1 : 1 : (sum . take 2 <$> tails xs)
selectMultiples :: Integer -> Filter Integer
selectMultiples q s = S.for
do s
do
\n -> case mod n q of
0 -> S.yield n
_ -> pure ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment