bluefin-0.6.0.0: The Bluefin effect system
Safe HaskellNone
LanguageHaskell2010

Bluefin.Capability.Yield

Synopsis

Documentation

Yield allows you to yield values during the execution of a Bluefin operation. It provides similar functionality to Python's yield. The handler of the Yield will either handle each element as soon as it is yielded (for example forEach) or gather all yielded elements into a list (for example yieldToList).

For information about prompt finalization/resource safety when using Bluefin Yields, see Bluefin.Capability.Request.

Capability

type Yield a = Stream a Source #

Capability to yield values of type a. It is implemented as a Request capability that can yield values of type a and then await values of type ().

Handlers

forEach Source #

Arguments

:: forall a b (es :: Effects) r. (forall (e1 :: Effects). Coroutine a b e1 -> Eff (e1 :& es) r) 
-> (a -> Eff es b)

Apply this effectful function for each element of the coroutine

-> Eff es r 

Apply an effectful function to each element yielded to the stream.

>>> runPureEff $ yieldToList $ \y -> do
      forEach (inFoldable [0 .. 3]) $ \i -> do
        yield y i
        yield y (i * 10)
([0, 0, 1, 10, 2, 20, 3, 30], ())

yieldToList Source #

Arguments

:: forall a (es :: Effects) r. (forall (e1 :: Effects). Stream a e1 -> Eff (e1 :& es) r) 
-> Eff es ([a], r)

Yielded elements and final result

Gather all yielded elements into a list.

>>> runPureEff $ yieldToList $ \y -> do
      yield y 1
      yield y 2
      yield y 100
([1,2,100], ())

yieldToReverseList Source #

Arguments

:: forall a (es :: Effects) r. (forall (e :: Effects). Stream a e -> Eff (e :& es) r) 
-> Eff es ([a], r)

Yielded elements in reverse order, and final result

This is more efficient than yieldToList because it gathers the elements into a stack in reverse order. yieldToList then reverses that stack.

>>> runPureEff $ yieldToReverseList $ \y -> do
      yield y 1
      yield y 2
      yield y 100
([100,2,1], ())

withYieldToList Source #

Arguments

:: forall a (es :: Effects) r. (forall (e :: Effects). Stream a e -> Eff (e :& es) ([a] -> r))

Stream computation

-> Eff es r

Result

>>> runPureEff $ withYieldToList $ \y -> do
  yield y 1
  yield y 2
  yield y 100
  pure length
3

ignoreYield Source #

Arguments

:: forall a (es :: Effects) r. (forall (e1 :: Effects). Stream a e1 -> Eff (e1 :& es) r) 
-> Eff es r

͘

Ignore all yielded elements.

>>> runPureEff $ ignoreYield $ \y -> do
     for_ [0 .. 4] $ \i -> do
       yield y i
       yield y (i * 10)

     pure 42
42

enumerate Source #

Arguments

:: forall (e2 :: Effects) (es :: Effects) a r. e2 <: es 
=> (forall (e1 :: Effects). Stream a e1 -> Eff (e1 :& es) r)

͘

-> Stream (Int, a) e2 
-> Eff es r 

Pair each element in the stream with an increasing index, starting from 0.

>>> runPureEff $ yieldToList $ enumerate (inFoldable ["A", "B", "C"])
([(0, "A"), (1, "B"), (2, "C")], ())

enumerateFrom Source #

Arguments

:: forall (e2 :: Effects) (es :: Effects) a r. e2 <: es 
=> Int

Initial value

-> (forall (e1 :: Effects). Stream a e1 -> Eff (e1 :& es) r) 
-> Stream (Int, a) e2 
-> Eff es r 

Pair each element in the stream with an increasing index, starting from an inital value.

>>> runPureEff $ yieldToList $ enumerateFrom 1 (inFoldable ["A", "B", "C"])
([(1, "A"), (2, "B"), (3, "C")], ())

mapMaybe Source #

Arguments

:: forall (e2 :: Effects) (es :: Effects) a b r. e2 <: es 
=> (a -> Maybe b)

Yield from the output stream all of the elemnts of the input stream for which this function returns Just

-> (forall (e1 :: Effects). Stream a e1 -> Eff (e1 :& es) r)

Input stream

-> Stream b e2 
-> Eff es r 

catMaybes Source #

Arguments

:: forall (e2 :: Effects) (es :: Effects) a r. e2 <: es 
=> (forall (e1 :: Effects). Stream (Maybe a) e1 -> Eff (e1 :& es) r)

Input stream

-> Stream a e2 
-> Eff es r 

Remove Nothing elements from a stream.

awaitYield Source #

Arguments

:: forall a (es :: Effects) r. (forall (e :: Effects). Await a e -> Eff (e :& es) r)

Starts running first. Each await from the Consume ...

-> (forall (e :: Effects). Yield a e -> Eff (e :& es) r)

... receives the value yielded from the Yield

-> Eff es r 

awaitYield is connectRequests specialized to Await and Yield, which is the most common case.

Effectful operations

yield Source #

Arguments

:: forall (e1 :: Effects) (es :: Effects) a. e1 <: es 
=> Stream a e1 
-> a

Yield this value from the stream

-> Eff es () 

Yield an element to the stream.

>>> runPureEff $ yieldToList $ \y -> do
      yield y 1
      yield y 2
      yield y 100
([1,2,100], ())

inFoldable Source #

Arguments

:: forall t (e1 :: Effects) (es :: Effects) a. (Foldable t, e1 <: es) 
=> t a

Yield all these values from the stream

-> Stream a e1 
-> Eff es () 
>>> runPureEff $ yieldToList $ inFoldable [1, 2, 100]
([1, 2, 100], ())

cycleToYield Source #

Arguments

:: forall f (e1 :: Effects) (es :: Effects) a. (Foldable f, e1 <: es) 
=> f a 
-> Yield a e1 
-> Eff es ()

͘

runPureEff $ yieldToList $ yOut -> do
  consumeStream
    (\c -> takeAwait 6 c yOut)
    (\yIn -> cycleToYield [1..3] yIn)
([1,2,3,1,2,3],())

takeAwait Source #

Arguments

:: forall (e1 :: Effects) (es :: Effects) (e2 :: Effects) a. (e1 <: es, e2 <: es) 
=> Int 
-> Await a e1 
-> Yield a e2 
-> Eff es ()

͘

runPureEff $ yieldToList $ yOut -> do
  awaitYield
    (\c -> takeAwait 4 c yOut)
    (\yIn -> inFoldable [1..10] yIn)
([1,2,3,4],())