IO
I/O is, in PHP (and really many Programming Languages) often capricious. The IO monad abstracts I/O in accordance with Functional Programming principles: it performs unsafe operations in a controlled environment. It is therefore apt for things like printing to Standard Output (STDOUT), and interacting with the filesystem among other impure actions. To follow is a set of IO primitives available in bingo-functional.
IO#
IO(unsafe)IO :: a -> IO ()
Calls the IO monad constructor thereby initializing a value of type IO.
Since:
bingo-functional- v1.11.0bingo-functional-js- v0.1.0
Argument(s):
- unsafe - Unsafe IO operation
- JavaScript
- PHP
import { IO } from 'bingo-functional-js'
const unsafe = IO(() => 'foo')use function Chemem\Bingo\Functional\Functors\Monads\IO\IO;
$unsafe = IO(fn (): string => 'foo');putChar#
putChar(char)putChar :: Char -> IO ()
Writes a character to the Standard Output Device (STDOUT).
Since:
bingo-functional- v1.11.0
Argument(s):
- char - The character to write
use function Chemem\Bingo\Functional\Functors\Monads\mcompose;use Chemem\Bingo\Functional\Functors\Monads\IO;
$io = mcompose(IO\getChar, IO\putChar);
$io(IO\IO(fn (): string => 'x'));putStr#
putStr(str)putStr :: String -> IO ()
Writes a string to the Standard Output Device.
Since:
bingo-functional- v1.11.0
Argument(s):
- str - The string to write
use function Chemem\Bingo\Functional\Functors\Monads\mcompose;use Chemem\Bingo\Functional\Functors\Monads\IO;
$io = mcompose(IO\getLine, IO\putStr);
$io(IO\IO(fn (): string => 'IO > '));putStrLn#
putStrLn(str)putStr :: String -> IO ()
Works like putStr() but appends a new line to STDOUT output.
Since:
bingo-functional- v1.11.0
Argument(s):
- str - The string to write
use function Chemem\Bingo\Functional\Functors\Monads\mcompose;use Chemem\Bingo\Functional\Functors\Monads\IO;
$io = mcompose(IO\getLine, IO\putStrLn);
$io(IO\IO(fn (): string => 'IO > '));getChar#
getChar()getChar :: IO Char
Reads character from Standard Input Device (STDIN).
Since:
bingo-functional- v1.11.0
Argument(s):
None
use function Chemem\Bingo\Functional\Functors\Monads\mcompose;use Chemem\Bingo\Functional\Functors\Monads\IO;
$io = mcompose(IO\getChar, IO\putChar);
$io(IO\IO(fn (): string => 'x'));getLine#
getLine()getLine :: IO String
Reads a line from the Standard Input Device (STDIN).
Since:
bingo-functional- v1.11.0
Argument(s):
None
use function Chemem\Bingo\Functional\Functors\Monads\mcompose;use Chemem\Bingo\Functional\Functors\Monads\IO;
$io = mcompose(IO\getLine, IO\putStr);
$io(IO\IO(fn (): string => 'IO > '));interact#
interact(function)interact :: (String -> String) -> IO ()
Takes a function with a String -> String signature, parses Standard Input device input and conveys output to same standard device.
Since:
bingo-functional- v1.11.0
Argument(s):
- function - An arbitrary string-transforming function
use function Chemem\Bingo\Functional\Functors\Monads\mcompose;use Chemem\Bingo\Functional\Functors\Monads\IO;
$io = mcompose( fn ($_) => IO\interact(fn ($str) => printf('%s', strtoupper($str))), IO\putStr);
$io(IO\IO(fn (): string => 'IO > '));_print#
_print(printable)_print :: Show a => a -> IO ()
Outputs a value of any printable type to the Standard Output device (STDOUT).
Since:
bingo-functional- v1.11.0
Argument(s):
- printable - An IO instance with printable data
use function Chemem\Bingo\Functional\Functors\Monads\IO\{IO, _print};
$io = _print(IO('foo'));IOException#
IOException(message)IOException :: String -> IO ()
Throws an IO Exception in controlled environment.
Since:
bingo-functional- v1.11.0bingo-functional-js- v0.1.0
Argument(s):
- message - An arbitrary IO Exception message
- JavaScript
- PHP
import { IOException } from 'bingo-functional-js'
const err = IOException('An error occurred!')use function Chemem\Bingo\Functional\Functors\Monads\IO\IOException;
$err = IOExcpetion('An error occurred!');catchIO#
catchIO(operation)catchIO :: IO () -> IO ()
Catches an IO Exception in a controlled environment.
Since:
bingo-functional- v1.11.0bingo-functional-js- v0.1.0
Argument(s):
- operation - An IO instance containing an IO Exception
- JavaScript
- PHP
import { IOException, catchIO } from 'bingo-functional-js'
const err = catchIO(IOException('An error occurred!'))err.exec()use function Chemem\Bingo\Functional\Functors\Monads\IO\{catchIO, IOException};
$err = catchIO(IOExcpetion('An error occurred!'));$err->exec();readFile#
readFile(file)readFile :: String -> IO String
Reads a file and returns the contents of the file as a string.
Since:
bingo-functional- v1.11.0
Argument(s):
- file - Path to file
use function Chemem\Bingo\Functional\Functors\Monads\mcompose;use Chemem\Bingo\Functional\Functors\Monads\IO;
$io = mcompose(IO\getLine, IO\putStr, IO\readFile);
$io(IO\IO(fn (): string => 'path/to/file'));writeFile#
writeFile(file, contents)writeFile :: String -> String -> IO ()
Writes a string to a file.
Since:
bingo-functional- v1.11.0
Argument(s):
- file - Path to file
- contents - Contents to write to file
use function Chemem\Bingo\Functional\{ Functors\Monads\mcompose, Algorithms\partialRight};use Chemem\Bingo\Functional\Functors\Monads\IO;
const TEXT = 'foo, bar, baz';
$write = mcompose( fn (bool $res) => IO\IO($res ? TEXT : 'A write error occurred'), partialRight(IO\writeFile, TEXT));
$write(IO\IO(fn (): string => 'path/to/file'));appendFile#
appendFile(file, contents)appendFile :: String -> String -> IO ()
Appends a string to a file.
Since:
bingo-functional- v1.11.0
Argument(s):
- file - Path to file
- contents - Contents to append to file
use function Chemem\Bingo\Functional\{ Functors\Monads\mcompose, Algorithms\partialRight};use Chemem\Bingo\Functional\Functors\Monads\IO;
const TEXT = 'foo, bar, baz';
$write = mcompose( fn (bool $res) => IO\IO($res ? TEXT : 'A write error occurred'), partialRight(IO\appendFile, TEXT));
$write(IO\IO(fn (): string => 'path/to/file'));