]);
```
-### Haskell Do Notation in PHP
-**NOTE:** This is experimental feature.
-
-``` php
-use Widmogrod\Monad\IO as IO;
-use Widmogrod\Monad\Control as C;
-use Widmogrod\Functional as f;
-
-$do = control\doo([
- IO\putStrLn('Your name:'), // put on screen line: Your name:
- '$name' =>
- IO\getLine(), // prompt for the name, and store it in '$name' key
-
- control\runWith(IO\putStrLn, ['$name']), // put on screen entered name
-
- IO\putStrLn('Your surname:'), // put on screen line: Your surname:
- '$surname' =>
- IO\getLine(), // prompt for surname, and store it in '$surname' key
-
- control\runWith(function($surname, $name) { // put on screen: "Hello $surname, $name"
- return IO\putStrLn(sprintf("Hello %s, %s", $surname, $name));
- }, ['$surname', '$name']),
-]);
-
-// performs operation, before that nothings happens from above code.
-$do->run();
-```
-
-Example output:
-```txt
-Your name:
-Gabriel
-Your surname:
-Habryn
-Hello Habryn, Gabriel
-```
-
### Sequencing Monad operations
This variant of `sequence_` ignores the result.
"src/Functional/strings.php",
"src/Functional/monoid.php",
"src/Functional/setoid.php",
- "src/Monad/Control/functions.php",
"src/Monad/Either/functions.php",
"src/Monad/Maybe/functions.php",
"src/Monad/Free/functions.php",
+++ /dev/null
-<?php
-
-namespace Widmogrod\Monad\Control;
-
-use Widmogrod\Monad as M;
-
-/**
- * doo :: State IO m => [m a] -> m a
- *
- * Haskell like "do notation" simple implementation.
- * Since "do" is reserved keyword in PHP then I use "doo".
- *
- * @param array|M\IO[] $monads
- *
- * @return M\IO
- */
-function doo(array $monads)
-{
- return M\IO::of(function () use ($monads) {
- $result = null;
- $data = [];
- // TODO do it by foldWithKeys
- foreach ($monads as $key => $monad) {
- // TODO do it better - maybe?
- if ($monad instanceof M\IO) {
- $monad = ioState($monad);
- }
-
- $state = [$key, $data];
- [$result, [$_, $data]] = M\State\runState($monad, $state);
- }
-
- return $result;
- });
-}
-
-/**
- * runWith :: (a -> IO b) -> [a] -> State IO b
- *
- * @param callable $function
- * @param array $argsNames
- *
- * @return M\State
- */
-function runWith(callable $function, array $argsNames)
-{
- return M\State::of(function (array $state) use ($function, $argsNames) {
- [$key, $data] = $state;
-
- $args = array_reduce($argsNames, function ($base, $index) use ($data) {
- $base[$index] = $data[$index];
-
- return $base;
- }, []);
-
- $value = $function(...$args)->run();
-
- $data[$key] = $value;
- $newState = [$key, $data];
-
- return [$value, $newState];
- });
-}
-
-/**
- * ioState :: IO a -> State IO a
- *
- * @param M\IO $io
- *
- * @return M\State
- */
-function ioState(M\IO $io)
-{
- return M\State::of(function ($state) use ($io) {
- [$key, $data] = $state;
-
- $value = $io->run();
-
- $data[$key] = $value;
- $newState = [$key, $data];
-
- return [$value, $newState];
- });
-}