Skip to contents

The built-in list type is a monad with element-wise function application as fmap() and flattening as join(). It follows that fmap() is a map operator and bind() is a "flat map" operator.

See also

purrr::map() which implements fmap() for list

purrr::list_flatten() which implements join() for list.

Other monads: maybe

Examples

# The fmap operator corresponds to purrr::map().
list(1, 2) %>>% `+`(1)
#> [[1]]
#> [1] 2
#> 
#> [[2]]
#> [1] 3
#> 

# The bind operator is a "flat-map".
list(1, 2) %>-% \(x) list(x * 2, x / 2)
#> [[1]]
#> [1] 2
#> 
#> [[2]]
#> [1] 0.5
#> 
#> [[3]]
#> [1] 4
#> 
#> [[4]]
#> [1] 1
#>