{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE BlockArguments #-}
module Control.Churro.Prelude where
import Control.Churro.Types
import Prelude hiding (id, (.))
import Control.Arrow (arr)
import Control.Category (id, (.), (>>>))
import Control.Concurrent (threadDelay)
import Control.Concurrent.Async (async, cancel, Async, wait)
import Control.Exception (Exception, SomeException, try)
import Control.Monad (replicateM_, when)
import Data.Foldable (foldMap', for_)
import Data.Maybe (isJust)
import Data.Time (NominalDiffTime)
import Data.Void (Void)
import GHC.Natural (Natural)
import Data.Traversable (for)
runWait :: Transport t => Churro a t Void Void -> IO a
runWait :: forall (t :: * -> *) a. Transport t => Churro a t Void Void -> IO a
runWait Churro a t Void Void
x = forall a. Async a -> IO a
wait forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall (t :: * -> *) a.
Transport t =>
Churro a t Void Void -> IO (Async a)
run Churro a t Void Void
x
runWait_ :: Transport t => Churro () t Void Void -> IO ()
runWait_ :: forall (t :: * -> *). Transport t => Churro () t Void Void -> IO ()
runWait_ = forall (t :: * -> *) a. Transport t => Churro a t Void Void -> IO a
runWait
runWaitList :: (Transport t, Monoid a) => Churro a t Void b -> IO [b]
runWaitList :: forall (t :: * -> *) a b.
(Transport t, Monoid a) =>
Churro a t Void b -> IO [b]
runWaitList Churro a t Void b
c = forall (t :: * -> *) a. Transport t => Churro a t Void Void -> IO a
runWait forall a b. (a -> b) -> a -> b
$ (Churro a t Void b
c forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (cat :: * -> * -> *) a b.
(Functor (cat a), Category cat) =>
(a -> b) -> cat a b
arr' (forall a. a -> [a] -> [a]
:[])) forall (t :: * -> *) fo gi a1 fi a2 go.
(Transport t, fo ~ gi) =>
Churro a1 t fi fo -> Churro a2 t gi go -> Churro a2 t fi go
>>>> forall (t :: * -> *) a.
(Transport t, Monoid a) =>
Churro a t a Void
sink
runWaitList_ :: Transport t => Churro () t Void b -> IO [b]
runWaitList_ :: forall (t :: * -> *) b. Transport t => Churro () t Void b -> IO [b]
runWaitList_ = forall (t :: * -> *) a b.
(Transport t, Monoid a) =>
Churro a t Void b -> IO [b]
runWaitList
run :: Transport t => Churro a t Void Void -> IO (Async a)
run :: forall (t :: * -> *) a.
Transport t =>
Churro a t Void Void -> IO (Async a)
run = forall (t :: * -> *) a i o.
Transport t =>
Churro a t i o -> IO (Async a)
run'
run_ :: Transport t => Churro () t Void Void -> IO (Async ())
run_ :: forall (t :: * -> *).
Transport t =>
Churro () t Void Void -> IO (Async ())
run_ = forall (t :: * -> *) a i o.
Transport t =>
Churro a t i o -> IO (Async a)
run'
run' :: Transport t => Churro a t i o -> IO (Async a)
run' :: forall (t :: * -> *) a i o.
Transport t =>
Churro a t i o -> IO (Async a)
run' Churro a t i o
c = do
(In t (Maybe i)
_i,Out t (Maybe o)
_o,Async a
a) <- forall a (t :: * -> *) i o.
Churro a t i o -> IO (In t (Maybe i), Out t (Maybe o), Async a)
runChurro Churro a t i o
c
forall (m :: * -> *) a. Monad m => a -> m a
return Async a
a
sourceSingleton :: Transport t => o -> Churro () t Void o
sourceSingleton :: forall (t :: * -> *) o. Transport t => o -> Churro () t Void o
sourceSingleton o
x = forall (t :: * -> *) (f :: * -> *) o.
(Transport t, Foldable f) =>
f o -> Churro () t Void o
sourceList [o
x]
sourceList :: (Transport t, Foldable f) => f o -> Churro () t Void o
sourceList :: forall (t :: * -> *) (f :: * -> *) o.
(Transport t, Foldable f) =>
f o -> Churro () t Void o
sourceList = forall (t :: * -> *) o a.
Transport t =>
((o -> IO ()) -> IO a) -> Churro a t Void o
sourceIO forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_
sourceIO :: Transport t => ((o -> IO ()) -> IO a) -> Churro a t Void o
sourceIO :: forall (t :: * -> *) o a.
Transport t =>
((o -> IO ()) -> IO a) -> Churro a t Void o
sourceIO (o -> IO ()) -> IO a
cb =
forall (t :: * -> *) i o a.
Transport t =>
(Out t (Maybe i) -> In t (Maybe o) -> IO a) -> Churro a t i o
buildChurro \Out t (Maybe Void)
_i In t (Maybe o)
o -> do
a
r <- (o -> IO ()) -> IO a
cb (forall (t :: * -> *) a. Transport t => In t a -> a -> IO ()
yeet In t (Maybe o)
o forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. a -> Maybe a
Just)
forall (t :: * -> *) a. Transport t => In t a -> a -> IO ()
yeet In t (Maybe o)
o forall a. Maybe a
Nothing
forall (m :: * -> *) a. Monad m => a -> m a
return a
r
sourceIO_ :: Transport t => ((o -> IO ()) -> IO ()) -> Churro () t Void o
sourceIO_ :: forall (t :: * -> *) o.
Transport t =>
((o -> IO ()) -> IO ()) -> Churro () t Void o
sourceIO_ = forall (t :: * -> *) o a.
Transport t =>
((o -> IO ()) -> IO a) -> Churro a t Void o
sourceIO
sources :: (Transport t, Traversable f, Monoid a) => f (Churro a t Void o) -> Churro a t Void o
sources :: forall (t :: * -> *) (f :: * -> *) a o.
(Transport t, Traversable f, Monoid a) =>
f (Churro a t Void o) -> Churro a t Void o
sources f (Churro a t Void o)
ss = forall (t :: * -> *) i o a.
Transport t =>
(Out t (Maybe i) -> In t (Maybe o) -> IO a) -> Churro a t i o
buildChurro \Out t (Maybe Void)
_i In t (Maybe o)
o -> do
f (In t (Maybe Void), Out t (Maybe o), Async a)
cs <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall a (t :: * -> *) i o.
Churro a t i o -> IO (In t (Maybe i), Out t (Maybe o), Async a)
runChurro f (Churro a t Void o)
ss
forall b a. IO b -> IO a -> IO a
finally' (forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (\(In t (Maybe Void)
_,Out t (Maybe o)
_,Async a
a) -> forall a. Async a -> IO ()
cancel Async a
a) f (In t (Maybe Void), Out t (Maybe o), Async a)
cs) do
f (Async a)
as <- forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for f (In t (Maybe Void), Out t (Maybe o), Async a)
cs \(In t (Maybe Void)
_i,Out t (Maybe o)
o',Async a
a) ->
forall a. IO a -> IO (Async a)
async do
forall (t :: * -> *) b a.
(Transport t, Monoid b) =>
Out t (Maybe a) -> (Maybe a -> IO b) -> IO b
yankAll' Out t (Maybe o)
o' \Maybe o
v -> do
case Maybe o
v of
Maybe o
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just o
x -> forall (t :: * -> *) a. Transport t => In t a -> a -> IO ()
yeet In t (Maybe o)
o (forall a. a -> Maybe a
Just o
x)
forall a. Async a -> IO a
wait Async a
a
a
r <- forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap' forall a. Async a -> IO a
wait f (Async a)
as
forall (t :: * -> *) a. Transport t => In t a -> a -> IO ()
yeet In t (Maybe o)
o forall a. Maybe a
Nothing
forall (m :: * -> *) a. Monad m => a -> m a
return a
r
sources_ :: Transport t => [Source () t o] -> Source () t o
sources_ :: forall (t :: * -> *) o.
Transport t =>
[Source () t o] -> Source () t o
sources_ = forall (t :: * -> *) (f :: * -> *) a o.
(Transport t, Traversable f, Monoid a) =>
f (Churro a t Void o) -> Churro a t Void o
sources
sink :: (Transport t, Monoid a) => Churro a t a Void
sink :: forall (t :: * -> *) a.
(Transport t, Monoid a) =>
Churro a t a Void
sink = forall (t :: * -> *) a o.
(Transport t, Monoid a) =>
(o -> IO a) -> Churro a t o Void
sinkIO forall (m :: * -> *) a. Monad m => a -> m a
return
sink_ :: Transport t => Churro () t i Void
sink_ :: forall (t :: * -> *) i. Transport t => Churro () t i Void
sink_ = forall (t :: * -> *) a o.
(Transport t, Monoid a) =>
(o -> IO a) -> Churro a t o Void
sinkIO (forall a b. a -> b -> a
const (forall (m :: * -> *) a. Monad m => a -> m a
return ()))
sinkIO :: (Transport t, Monoid a) => (o -> IO a) -> Churro a t o Void
sinkIO :: forall (t :: * -> *) a o.
(Transport t, Monoid a) =>
(o -> IO a) -> Churro a t o Void
sinkIO o -> IO a
cb = forall (t :: * -> *) i o a.
Transport t =>
(Out t (Maybe i) -> In t (Maybe o) -> IO a) -> Churro a t i o
buildChurro \Out t (Maybe o)
i In t (Maybe Void)
_o -> forall (t :: * -> *) a i.
(Transport t, Monoid a) =>
Out t (Maybe i) -> (i -> IO a) -> IO a
yankAll Out t (Maybe o)
i o -> IO a
cb
sinkIO_ :: Transport t => (o -> IO ()) -> Churro () t o Void
sinkIO_ :: forall (t :: * -> *) o.
Transport t =>
(o -> IO ()) -> Churro () t o Void
sinkIO_ o -> IO ()
cb = forall (t :: * -> *) i o a.
Transport t =>
(Out t (Maybe i) -> In t (Maybe o) -> IO a) -> Churro a t i o
buildChurro \Out t (Maybe o)
i In t (Maybe Void)
_o -> forall (t :: * -> *) a i.
(Transport t, Monoid a) =>
Out t (Maybe i) -> (i -> IO a) -> IO a
yankAll Out t (Maybe o)
i o -> IO ()
cb
sinkHO :: Transport t => (((i -> IO ()) -> IO ()) -> IO a) -> Churro a t i o
sinkHO :: forall (t :: * -> *) i a o.
Transport t =>
(((i -> IO ()) -> IO ()) -> IO a) -> Churro a t i o
sinkHO ((i -> IO ()) -> IO ()) -> IO a
cb = forall (t :: * -> *) i o a.
Transport t =>
(Out t (Maybe i) -> In t (Maybe o) -> IO a) -> Churro a t i o
buildChurro \Out t (Maybe i)
i In t (Maybe o)
_o -> ((i -> IO ()) -> IO ()) -> IO a
cb (forall (t :: * -> *) a i.
(Transport t, Monoid a) =>
Out t (Maybe i) -> (i -> IO a) -> IO a
yankAll Out t (Maybe i)
i)
sinkPrint :: (Transport t, Show a) => Churro () t a Void
sinkPrint :: forall (t :: * -> *) a. (Transport t, Show a) => Churro () t a Void
sinkPrint = forall (t :: * -> *) a o.
(Transport t, Monoid a) =>
(o -> IO a) -> Churro a t o Void
sinkIO forall a. Show a => a -> IO ()
print
process :: Transport t => (a -> IO b) -> Churro () t a b
process :: forall (t :: * -> *) a b.
Transport t =>
(a -> IO b) -> Churro () t a b
process a -> IO b
f = forall (t :: * -> *) i o.
Transport t =>
(i -> IO [o]) -> Churro () t i o
processN (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (f :: * -> *) a. Applicative f => a -> f a
pure forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> IO b
f)
processPrint :: (Transport t, Show b) => Churro () t b b
processPrint :: forall (t :: * -> *) b. (Transport t, Show b) => Churro () t b b
processPrint = forall (t :: * -> *) a b.
Transport t =>
(a -> IO b) -> Churro () t a b
process \b
x -> do forall a. Show a => a -> IO ()
print b
x forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return b
x
processDebug :: (Transport t, Show b) => String -> Churro () t b b
processDebug :: forall (t :: * -> *) b.
(Transport t, Show b) =>
String -> Churro () t b b
processDebug String
d = forall (t :: * -> *) a b.
Transport t =>
(a -> IO b) -> Churro () t a b
process \b
x -> String -> IO ()
putStrLn (String
"Debugging [" forall a. Semigroup a => a -> a -> a
<> String
d forall a. Semigroup a => a -> a -> a
<> String
"]: " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show b
x) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return b
x
processN :: Transport t => (i -> IO [o]) -> Churro () t i o
processN :: forall (t :: * -> *) i o.
Transport t =>
(i -> IO [o]) -> Churro () t i o
processN i -> IO [o]
f =
forall (t :: * -> *) i o a.
Transport t =>
(Out t (Maybe i) -> In t (Maybe o) -> IO a) -> Churro a t i o
buildChurro \Out t (Maybe i)
i In t (Maybe o)
o -> do
forall (t :: * -> *) a i.
(Transport t, Monoid a) =>
Out t (Maybe i) -> (i -> IO a) -> IO a
yankAll Out t (Maybe i)
i \i
x -> do forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (forall (t :: * -> *) a. Transport t => In t a -> a -> IO ()
yeet In t (Maybe o)
o forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. a -> Maybe a
Just) forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< i -> IO [o]
f i
x
forall (t :: * -> *) a. Transport t => In t a -> a -> IO ()
yeet In t (Maybe o)
o forall a. Maybe a
Nothing
concatC :: Transport t => Churro () t [o] o
concatC :: forall (t :: * -> *) o. Transport t => Churro () t [o] o
concatC = forall (t :: * -> *) i o.
Transport t =>
(i -> IO [o]) -> Churro () t i o
processN (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id)
processes :: (Traversable f, Transport t1, Transport t2, Monoid a) => f (Churro a t1 i o) -> Churro a t2 i o
processes :: forall (f :: * -> *) (t1 :: * -> *) (t2 :: * -> *) a i o.
(Traversable f, Transport t1, Transport t2, Monoid a) =>
f (Churro a t1 i o) -> Churro a t2 i o
processes f (Churro a t1 i o)
cs = forall a (t :: * -> *) i o.
IO (In t (Maybe i), Out t (Maybe o), Async a) -> Churro a t i o
Churro do
(In t2 (Maybe i)
i, Out t2 (Maybe i)
o ) <- forall (t :: * -> *) a. Transport t => IO (In t a, Out t a)
flex
(In t2 (Maybe o)
i', Out t2 (Maybe o)
o') <- forall (t :: * -> *) a. Transport t => IO (In t a, Out t a)
flex
let
worker :: Churro a t i o -> IO (Async a)
worker Churro a t i o
c = forall a. IO a -> IO (Async a)
async do
forall a (t :: * -> *) i o b.
Churro a t i o
-> (In t (Maybe i) -> Out t (Maybe o) -> Async a -> IO b) -> IO b
withChurro Churro a t i o
c \In t (Maybe i)
ci Out t (Maybe o)
co Async a
ca -> do
Async ()
a' <- forall a. IO a -> IO (Async a)
async do
forall (t1 :: * -> *) (t2 :: * -> *) i.
(Transport t1, Transport t2) =>
Out t1 (Maybe i) -> In t2 (Maybe i) -> IO ()
c2c' Out t (Maybe o)
co In t2 (Maybe o)
i'
forall (t :: * -> *) a. Transport t => In t a -> a -> IO ()
yeet In t2 (Maybe i)
i forall a. Maybe a
Nothing
forall (t1 :: * -> *) (t2 :: * -> *) i.
(Transport t1, Transport t2) =>
Out t1 (Maybe i) -> In t2 (Maybe i) -> IO ()
c2c' Out t2 (Maybe i)
o In t (Maybe i)
ci
forall (t :: * -> *) a. Transport t => In t a -> a -> IO ()
yeet In t (Maybe i)
ci forall a. Maybe a
Nothing
forall a. Async a -> IO a
wait Async ()
a'
forall a. Async a -> IO a
wait Async a
ca
f (Async a)
as <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall {t :: * -> *} {a}.
Transport t =>
Churro a t i o -> IO (Async a)
worker f (Churro a t1 i o)
cs
Async a
a <- forall a. IO a -> IO (Async a)
async do
a
r <- forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap' forall a. Async a -> IO a
wait f (Async a)
as
forall (t :: * -> *) a. Transport t => In t a -> a -> IO ()
yeet In t2 (Maybe o)
i' forall a. Maybe a
Nothing
forall (m :: * -> *) a. Monad m => a -> m a
return a
r
forall (m :: * -> *) a. Monad m => a -> m a
return (In t2 (Maybe i)
i,Out t2 (Maybe o)
o',Async a
a)
where
c2c' :: (Transport t1, Transport t2) => Out t1 (Maybe i) -> In t2 (Maybe i) -> IO ()
c2c' :: forall (t1 :: * -> *) (t2 :: * -> *) i.
(Transport t1, Transport t2) =>
Out t1 (Maybe i) -> In t2 (Maybe i) -> IO ()
c2c' Out t1 (Maybe i)
o In t2 (Maybe i)
i = forall (t :: * -> *) a i.
(Transport t, Monoid a) =>
Out t (Maybe i) -> (i -> IO a) -> IO a
yankAll Out t1 (Maybe i)
o (forall (t :: * -> *) a. Transport t => In t a -> a -> IO ()
yeet In t2 (Maybe i)
i forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. a -> Maybe a
Just)
thief :: (Transport t1, Transport t2, Monoid a) => Int -> Churro a t1 i o -> Churro a t2 i o
thief :: forall (t1 :: * -> *) (t2 :: * -> *) a i o.
(Transport t1, Transport t2, Monoid a) =>
Int -> Churro a t1 i o -> Churro a t2 i o
thief Int
n Churro a t1 i o
c = forall (f :: * -> *) (t1 :: * -> *) (t2 :: * -> *) a i o.
(Traversable f, Transport t1, Transport t2, Monoid a) =>
f (Churro a t1 i o) -> Churro a t2 i o
processes (forall a. Int -> a -> [a]
replicate Int
n Churro a t1 i o
c)
justs :: Transport t => Churro () t (Maybe a) a
justs :: forall (t :: * -> *) a. Transport t => Churro () t (Maybe a) a
justs = forall (t :: * -> *) a b.
Transport t =>
(a -> [b]) -> Churro () t a b
mapN (forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] forall (f :: * -> *) a. Applicative f => a -> f a
pure)
lefts :: Transport t => Churro () t (Either a b) a
lefts :: forall (t :: * -> *) a b. Transport t => Churro () t (Either a b) a
lefts = forall (t :: * -> *) a b.
Transport t =>
(a -> [b]) -> Churro () t a b
mapN (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a b. a -> b -> a
const []))
rights :: Transport t => Churro () t (Either a b) b
rights :: forall (t :: * -> *) a b. Transport t => Churro () t (Either a b) b
rights = forall (t :: * -> *) a b.
Transport t =>
(a -> [b]) -> Churro () t a b
mapN (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> b -> a
const []) forall (f :: * -> *) a. Applicative f => a -> f a
pure)
takeC :: (Transport t, Integral n) => n -> Churro () t a a
takeC :: forall (t :: * -> *) n a.
(Transport t, Integral n) =>
n -> Churro () t a a
takeC n
n = forall (t :: * -> *) i o a.
Transport t =>
(Out t (Maybe i) -> In t (Maybe o) -> IO a) -> Churro a t i o
buildChurro \Out t (Maybe a)
i In t (Maybe a)
o -> forall {t} {t :: * -> *} {t :: * -> *} {a}.
(Ord t, Num t, Transport t, Transport t, Enum t) =>
t -> Out t (Maybe a) -> In t (Maybe a) -> IO ()
go n
n Out t (Maybe a)
i In t (Maybe a)
o
where
go :: t -> Out t (Maybe a) -> In t (Maybe a) -> IO ()
go t
t Out t (Maybe a)
i In t (Maybe a)
o
| t
t forall a. Ord a => a -> a -> Bool
<= t
0 = forall (t :: * -> *) a. Transport t => In t a -> a -> IO ()
yeet In t (Maybe a)
o forall a. Maybe a
Nothing
| Bool
otherwise = do
Maybe a
x <- forall (t :: * -> *) a. Transport t => Out t a -> IO a
yank Out t (Maybe a)
i
forall (t :: * -> *) a. Transport t => In t a -> a -> IO ()
yeet In t (Maybe a)
o Maybe a
x
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (forall a. Maybe a -> Bool
isJust Maybe a
x) do t -> Out t (Maybe a) -> In t (Maybe a) -> IO ()
go (forall a. Enum a => a -> a
pred t
t) Out t (Maybe a)
i In t (Maybe a)
o
dropC :: (Transport t, Integral n) => n -> Churro () t a a
dropC :: forall (t :: * -> *) n a.
(Transport t, Integral n) =>
n -> Churro () t a a
dropC n
n = forall (t :: * -> *) i o a.
Transport t =>
(Out t (Maybe i) -> In t (Maybe o) -> IO a) -> Churro a t i o
buildChurro \Out t (Maybe a)
i In t (Maybe a)
o -> do
forall (m :: * -> *) a. Applicative m => Int -> m a -> m ()
replicateM_ (forall a b. (Integral a, Num b) => a -> b
fromIntegral n
n) (forall (t :: * -> *) a. Transport t => Out t a -> IO a
yank Out t (Maybe a)
i)
forall (t :: * -> *) a b.
Transport t =>
(a -> b) -> Out t (Maybe a) -> In t (Maybe b) -> IO ()
c2c forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id Out t (Maybe a)
i In t (Maybe a)
o
filterC :: Transport t => (a -> Bool) -> Churro () t a a
filterC :: forall (t :: * -> *) a.
Transport t =>
(a -> Bool) -> Churro () t a a
filterC a -> Bool
p = forall (t :: * -> *) a b.
Transport t =>
(a -> [b]) -> Churro () t a b
mapN (forall a. (a -> Bool) -> [a] -> [a]
filter a -> Bool
p forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure)
mapN :: Transport t => (a -> [b]) -> Churro () t a b
mapN :: forall (t :: * -> *) a b.
Transport t =>
(a -> [b]) -> Churro () t a b
mapN a -> [b]
f = forall (t :: * -> *) i o.
Transport t =>
(i -> IO [o]) -> Churro () t i o
processN (forall (m :: * -> *) a. Monad m => a -> m a
return forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> [b]
f)
delay :: Transport t => NominalDiffTime -> Churro () t a a
delay :: forall (t :: * -> *) a.
Transport t =>
NominalDiffTime -> Churro () t a a
delay = forall (t :: * -> *) a. Transport t => Int -> Churro () t a a
delayMicro forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a b. (RealFrac a, Integral b) => a -> b
ceiling @Double forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. Fractional a => Rational -> a
fromRational forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (forall a. Num a => a -> a -> a
*Rational
1000000) forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. Real a => a -> Rational
toRational
delayMicro :: Transport t => Int -> Churro () t a a
delayMicro :: forall (t :: * -> *) a. Transport t => Int -> Churro () t a a
delayMicro Int
d = forall (t :: * -> *) a b.
Transport t =>
(a -> IO b) -> Churro () t a b
process \a
x -> do
Int -> IO ()
threadDelay Int
d
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
withPrevious :: Transport t => Churro () t a (a,a)
withPrevious :: forall (t :: * -> *) a. Transport t => Churro () t a (a, a)
withPrevious = forall (t :: * -> *) i o a.
Transport t =>
(Out t (Maybe i) -> In t (Maybe o) -> IO a) -> Churro a t i o
buildChurro \Out t (Maybe a)
i In t (Maybe (a, a))
o -> do
forall {t :: * -> *} {t :: * -> *} {b}.
(Transport t, Transport t) =>
Maybe b -> Out t (Maybe b) -> In t (Maybe (b, b)) -> IO ()
prog forall a. Maybe a
Nothing Out t (Maybe a)
i In t (Maybe (a, a))
o
forall (t :: * -> *) a. Transport t => In t a -> a -> IO ()
yeet In t (Maybe (a, a))
o forall a. Maybe a
Nothing
where
prog :: Maybe b -> Out t (Maybe b) -> In t (Maybe (b, b)) -> IO ()
prog Maybe b
x Out t (Maybe b)
i In t (Maybe (b, b))
o = do
Maybe b
y <- forall (t :: * -> *) a. Transport t => Out t a -> IO a
yank Out t (Maybe b)
i
case (Maybe b
x,Maybe b
y) of
(Just b
x', Just b
y') -> forall (t :: * -> *) a. Transport t => In t a -> a -> IO ()
yeet In t (Maybe (b, b))
o (forall a. a -> Maybe a
Just (b
x',b
y')) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Maybe b -> Out t (Maybe b) -> In t (Maybe (b, b)) -> IO ()
prog Maybe b
y Out t (Maybe b)
i In t (Maybe (b, b))
o
(Maybe b
Nothing, Just b
y') -> Maybe b -> Out t (Maybe b) -> In t (Maybe (b, b)) -> IO ()
prog (forall a. a -> Maybe a
Just b
y') Out t (Maybe b)
i In t (Maybe (b, b))
o
(Maybe b, Maybe b)
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
processRetry :: Transport t => Natural -> (i -> IO o) -> Churro () t i o
processRetry :: forall (t :: * -> *) i o.
Transport t =>
Natural -> (i -> IO o) -> Churro () t i o
processRetry Natural
retries i -> IO o
f = forall e (t :: * -> *) i o.
(Exception e, Transport t) =>
Natural -> (i -> IO o) -> Churro () t i (Either e o)
processRetry' @SomeException Natural
retries i -> IO o
f forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (t :: * -> *) a b. Transport t => Churro () t (Either a b) b
rights
processRetry' :: (Exception e, Transport t) => Natural -> (i -> IO o) -> Churro () t i (Either e o)
processRetry' :: forall e (t :: * -> *) i o.
(Exception e, Transport t) =>
Natural -> (i -> IO o) -> Churro () t i (Either e o)
processRetry' Natural
retries i -> IO o
f = forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (Natural
0,) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (t :: * -> *) e n a b.
(Transport t, Exception e, Ord n, Enum n) =>
n -> (a -> IO b) -> Churro () t (n, a) (Either e b)
processRetry'' Natural
retries i -> IO o
f
processRetry'' :: (Transport t, Exception e, Ord n, Enum n) => n -> (a -> IO b) -> Churro () t (n, a) (Either e b)
processRetry'' :: forall (t :: * -> *) e n a b.
(Transport t, Exception e, Ord n, Enum n) =>
n -> (a -> IO b) -> Churro () t (n, a) (Either e b)
processRetry'' n
retries a -> IO b
f =
forall (t :: * -> *) i o a.
Transport t =>
(In t (Maybe i) -> Out t (Maybe i) -> In t (Maybe o) -> IO a)
-> Churro a t i o
buildChurro' \In t (Maybe (n, a))
i' Out t (Maybe (n, a))
o In t (Maybe (Either e b))
i -> do
forall (t :: * -> *) a i.
(Transport t, Monoid a) =>
Out t (Maybe i) -> (i -> IO a) -> IO a
yankAll Out t (Maybe (n, a))
o \(n
n, a
y) -> do
Either e b
r <- forall e a. Exception e => IO a -> IO (Either e a)
try do a -> IO b
f a
y
forall (t :: * -> *) a. Transport t => In t a -> a -> IO ()
yeet In t (Maybe (Either e b))
i (forall a. a -> Maybe a
Just Either e b
r)
case Either e b
r of
Right b
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
Left e
_ -> forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (n
n forall a. Ord a => a -> a -> Bool
< n
retries) do forall (t :: * -> *) a. Transport t => In t a -> a -> IO ()
yeet In t (Maybe (n, a))
i' (forall a. a -> Maybe a
Just (forall a. Enum a => a -> a
succ n
n, a
y))
forall (t :: * -> *) a. Transport t => In t a -> a -> IO ()
yeet In t (Maybe (Either e b))
i forall a. Maybe a
Nothing