# Data.Machine.Mealy

- Package
- purescript-machines
- Repository
- purescript-contrib/purescript-machines

This module provides the building blocks required to create finite state machines.

### #MealyT Source

`newtype MealyT f i o`

Mealy is a finite state machine, where:

`f`

is the effect under which we evaluate,`i`

is the input type and`o`

is the output type.

#### Instances

`(Functor f) => Functor (MealyT f i)`

`(Apply f) => Apply (MealyT f i)`

`(Applicative f) => Applicative (MealyT f i)`

`(Functor f) => Profunctor (MealyT f)`

`(Functor f) => Strong (MealyT f)`

`(Monad f) => Semigroup (MealyT f i o)`

`(Monad f) => Monoid (MealyT f i o)`

`(Monad f) => Semigroupoid (MealyT f)`

`(Monad f) => Category (MealyT f)`

`(Monad f) => Bind (MealyT f i)`

`(Monad f) => Monad (MealyT f i)`

`(Monad f) => Alt (MealyT f i)`

`(Monad f) => Plus (MealyT f i)`

`(Monad f) => Alternative (MealyT f i)`

`(Monad f) => MonadPlus (MealyT f i)`

`(MonadEffect f) => MonadEffect (MealyT f i)`

`Lazy (MealyT f i o)`

### #pureMealy Source

`pureMealy :: forall f i o. Applicative f => (i -> Step f i o) -> MealyT f i o`

Wrap a pure function into a machine. The function can either
terminate via `Halt`

, or `Emit`

a value and then decide whether
to `Halt`

, continue with a different function, or (usually) wrap
itself via `pureMealy`

recursively.

For example, we can `Halt`

on zero:

```
haltOn0 :: forall f. Applicative f => MealyT f Int Int
haltOn0 = pureMealy go
where
go 0 = Halt
go n = Emit n (pureMealy haltOn0)
```

### #halt Source

`halt :: forall f i o. Applicative f => MealyT f i o`

A machine which halts for any input.

### #take Source

`take :: forall f i o. Applicative f => Int -> MealyT f i o -> MealyT f i o`

Limit the number of outputs of a machine. After using up the `n`

allotted outputs, the machine will halt.

### #toUnfoldable Source

`toUnfoldable :: forall f g i o. Unfoldable g => Comonad f => i -> MealyT f i o -> g o`

Extract all the outputs of a machine, given some input.

### #singleton Source

`singleton :: forall f i o. Applicative f => o -> MealyT f i o`

Creates a machine which emits a single value before halting.

### #fromMaybe Source

`fromMaybe :: forall f i o. Applicative f => Maybe o -> MealyT f i o`

Creates a machine which either emits a single value before halting
(for `Just`

), or just halts (in the case of `Nothing`

).

### #interleave Source

`interleave :: forall f i o. Monad f => MealyT f i o -> MealyT f i o -> MealyT f i o`

Interleaves the values of two machines with matching inputs and outputs.

### #ifte Source

`ifte :: forall f i a b. Monad f => MealyT f i a -> (a -> MealyT f i b) -> MealyT f i b -> MealyT f i b`

If then else: given a machine producing `a`

, a continuation `f`

,
and a machine producing `b`

, generate a machine which will
grab outputs from the first machine and pass them over to the
continuation as long as neither halts.
Once the process halts, the second (`b`

) machine is returned.

### #wrapEffect Source

`wrapEffect :: forall f i o. Applicative f => f o -> MealyT f i o`

Creates a machine which wraps an effectful computation and ignores its input.

- Modules
- Data.
Machine. Mealy