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.
#
IOIO(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');
#
putCharputChar(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'));
#
putStrputStr(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 > '));
#
putStrLnputStrLn(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 > '));
#
getChargetChar()
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'));
#
getLinegetLine()
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 > '));
#
interactinteract(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'));
#
IOExceptionIOException(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!');
#
catchIOcatchIO(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();
#
readFilereadFile(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'));
#
writeFilewriteFile(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'));
#
appendFileappendFile(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'));