control-dsl-0.2.0.1: An alternative to monads

Safe HaskellSafe
LanguageHaskell2010

Control.Dsl.Yield

Description

This module contains the Yield data type and related Dsl instances.

The Yield data type can be used to create generators, similar to the yield keyword in C#, Python, and ECMAScript.

Synopsis

Documentation

data Yield x r a where Source #

This Yield keyword produces an element in a list generator

Examples

Expand

randomGenerator is an example to create an xorshift pseudo-random number generator that returns an infinite list of generated numbers.

>>> :set -XTypeApplications
>>> :set -XRebindableSyntax
>>> import Prelude hiding ((>>), (>>=), return, fail)
>>> import Control.Dsl
>>> import Data.Word
>>> import Data.Bits
>>> :{
randomGenerator :: Word32 -> [Word32]
randomGenerator seed =
  do let tmp1 = xor seed $ shiftL seed 13
     let tmp2 = xor tmp1 $ shiftR tmp1 17
     let tmp3 = xor tmp2 $ shiftL tmp2 5
     Yield tmp3
     randomGenerator tmp3
:}
>>> take 5 $ randomGenerator 2463534242
[723471715,2497366906,2064144800,2008045182,3532304609]

Constructors

Yield :: x -> Yield x r () 
Instances
PolyCont (Yield x) [x] () Source # 
Instance details

Defined in Control.Dsl.Yield

Methods

runPolyCont :: Yield x r' () -> (() -> [x]) -> [x] Source #

PolyCont (Yield x) (Cont r [x]) () Source # 
Instance details

Defined in Control.Dsl.Yield

Methods

runPolyCont :: Yield x r' () -> (() -> Cont r [x]) -> Cont r [x] Source #