rmonad: rmonad: handling pipes, errors, and everything with monads

Description Basic Operators Operators targeted for deprecation x to monad functions monad to monad functions monad to x functions Examples

Description

Rmonad merges blocks of code into a graph containing the history of all past operations, and optionally their values. It consists mainly of a set of monadic bind operators for controlling a pipeline and handling error. It also contains functions for operating on monads, evaluating expressions into monads, and extracting values from them. I will briefly introduce the most useful of these here. For more information see the introduction vignette.

Basic Operators

%>>%

monadic bind: applies rhs function to the lhs value

%v>%

monadic bind: store intermediate result

%*>%

bind lhs list as arguments to right. The lhs may be a literal list or a monad bound list.

%>_%

perform rhs action, discard result, pass the lhs

%>^%

Bind as a new branch, pass input on main. This differs from %>_% in that future operations do not depend on its pass/fail status. Use unbranch to extract all branches from an Rmonad object.

%||%

if input is error, use rhs value instead

%|>%

if input is error, run rhs on last passing result

%__%

keep parents from the lhs (errors ignored). This allows chaining of independent operations.

Operators targeted for deprecation

%^>% Monadic bind and record input in monad. Perform rhs operation on lhs branches. I may deprecate this operator.

x to monad functions

evalwrap - evaluate an expression into a monad (capturing error)

funnel - evaluate expressions into a list inside a monad

monad to monad functions

forget - erase parents from a monad

combine - combine a list of monads into a list in a monad

monad to x functions

esc - extract the result from a computation

mtabulate - summarize all steps in a pipeline into a table

missues - tabulate all warnings and errors from a pipeline

unbranch - extract all branches from the pipeline

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# chain operations
cars %>>% colSums

# chain operations with intermediate storing
cars %v>% colSums

# handle failing monad
iris %>>% colSums %|>% head
cars %>>% colSums %|>% head

# run an effect
cars %>_% plot %>>% colSums

# return first successful operation
read.csv("a.csv") %||% iris %>>% head

# join two independent pipelines, preserving history
cars %>>% colSums %__% cars %>>% lapply(sd) %>>% unlist

# load an expression into a monad, catching errors
evalwrap(stop("instant death"))

# convert multiple expressions into a list inside a monad
funnel(stop("oh no"), runif(5), sqrt(-1))

arendsee/rmonad documentation built on Dec. 19, 2020, 9:06 p.m.