PHPackages                             php-fp/php-fp-state - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. php-fp/php-fp-state

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

php-fp/php-fp-state
===================

An implementation of the State monad in PHP.

610PHP

Since Apr 18Pushed 10y agoCompare

[ Source](https://github.com/php-fp/php-fp-state)[ Packagist](https://packagist.org/packages/php-fp/php-fp-state)[ RSS](/packages/php-fp-php-fp-state/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependenciesVersions (1)Used By (0)

The State Monad for PHP. [![Build Status](https://camo.githubusercontent.com/644fc1b4fe1d655ce95e0b5e9ea39050eea7166a0367fc0fe7bde14af4f72a6e/68747470733a2f2f7472617669732d63692e6f72672f7068702d66702f7068702d66702d73746174652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/php-fp/php-fp-state)
=================================================================================================================================================================================================================================================================================================================

[](#the-state-monad-for-php-)

Intro
-----

[](#intro)

Purely functional programming has no state. State is, after all, impurity, and purity is what we're aiming to achieve. Well, let's examine state for a moment.

An instruction executed within a given state leads to an updated state. If we were writing an interpreter, we might write the execution function's type signature as `State -> Function -> State`. The old state and the instruction are combined to make a new state.

Now, if we consider the program to be a list of instructions, and the desired output to be a state, we get `State -> [Instruction] -> State`. We have an initial state, and we combine these instructions, one after another, with the state, finally giving the end state.

`exec :: (State -> Function -> State) -> State -> [Instruction] -> State`

Does this look familiar?

`reduce :: (b -> a -> b) -> b -> [a] -> b`

Well, hey: we can implement the notion of state using `reduce`, resulting in a purely functional flow with equivalent power. However, this doesn't mean we should write all our programs like this: there are lots of places where state is totally unnecessary, and we should try to keep them away from it.

What we'd really like to do is to have state for specific computations, and it just so happens that monads are perfect for this! Each chaining function should be restricted to one state interaction, which forces you to clarify your process.

API
---

[](#api)

In the following type signatures, constructors and static functions are written as one would see in pure languages such as Haskell. The others contain a pipe, where the type before the pipe represents the type of the current State instance, and the type after the pipe represents the function.

### `of :: a -> State a b`

[](#of--a---state-a-b)

The State monad has an applicative functor for wrapping values within a State monad. The `evalState` function is the complement:

```
