Posix.IO

IO Monad

type alias IO a =
IO a
return : a -> IO a
map : (a -> b) -> IO a -> IO b
do : IO a -> (a -> IO b) -> IO b

Compose IO actions, do-notation style.

do (File.open "file.txt" |> exitOnError identity) <| \fd ->
do (File.write fd "Hello, World")
andThen : (a -> IO b) -> IO a -> IO b

Compose IO actions, andThen style

File.open "file.txt"
    |> exitOnError identity
    |> andThen
        (\fd ->
            File.write fd "Hello, World"
        )
combine : List (IO a) -> IO (List a)

Perform IO in sequence

exitOnError : (error -> String) -> IO (Result error a) -> IO a

Print to stderr and exit program on Err

Create IO Program

type alias Process =
{ argv : List String
, pid : Int
, env : Dict String String
}
program : (Process -> IO ()) -> PosixProgram
module HelloUser exposing (program)

import Dict exposing (Dict)
import Posix.IO as IO exposing (IO, Process)
import Posix.IO.File as File

helloUser : Process -> IO ()
helloUser process =
    let
        userName =
            Dict.get "USER" process.env
                |> Maybe.withDefault "Unknown"
    in
    File.write File.stdOut userName

program : IO.PosixProgram
program =
    IO.program helloUser
type alias PosixProgram =
PosixProgram