armcn / maybe Goto Github PK
View Code? Open in Web Editor NEWThe Maybe Monad in R
Home Page: https://armcn.github.io/maybe
License: Other
The Maybe Monad in R
Home Page: https://armcn.github.io/maybe
License: Other
Throw an error if the function provided to map_maybe
returns a maybe value or the function provided to and_then
doesn't return a maybe value.
Add more predicates including:
Example code:
ok <- function(a) {
as_result(list(type = "ok", content = a))
}
err <- function(e) {
as_result(list(type = "err", content = e))
}
as_result <- function(a) {
structure(a, class = "result")
}
result <- function(.f, allow_warning = FALSE) {
function(...) {
on_warning <-
function(w)
if (isTRUE(allow_warning))
ok(.f(...))
else
err(w)
tryCatch(
ok(.f(...)),
error = err,
warning = on_warning
)
}
}
with_default.result <- function(.r, default) {
assert_is_result(.r)
if (is_ok(.m))
from_ok(.m)
else
default
}
is_result <- function(a) {
identical(class(a), "result")
}
is_ok <- function(a) {
and(is_result, function(b) identical(b$type, "ok"))(a)
}
is_err <- function(a) {
and(is_result, function(b) identical(b$type, "err"))(a)
}
result_extract <- function(.r, .f) {
if (is_err(.r))
.f(.r$content)
else
.r$content
}
Hello,
In Haskell there is the fromJust
function. I find it useful. In your package it would be:
from_just <- function(x){
if(is_just(x)){
x[["content"]]
}else{
stop("`x` is not a 'Just' value.")
}
}
Cheers.
I'm working on an experimental package monad to provide generics and associated operators for monads. When looking for existing implementations, I came across your very clean implementation of the Maybe monad here.
Would you be interested in coordinating to use generic versions of fmap()
, bind()
and join()
to get infix pipe operators? Since you already have the method implementations in {maybe} (with the same names even), it should be quite straightforward:
library(monad) # pak::pak("mikmart/monad")
# Signatures are not currently compatible
wrap <- \(fn) \(m, f, ...) fn(m, f, ...)
S7::method(fmap, S7::new_S3_class("maybe")) <- wrap(maybe::fmap)
S7::method(bind, S7::new_S3_class("maybe")) <- wrap(maybe::bind)
S7::method(join, S7::new_S3_class("maybe")) <- \(m) maybe::join(m)
# %>>% = fmap generic operator
# %>-% = bind generic operator
maybe::just(1) %>>% `+`(1) %>>% `*`(2)
#> Just
#> [1] 4
maybe::just(1) %>-% \(x) maybe::nothing()
#> Nothing
maybe::nothing() %>>% `+`(1) %>>% `*`(2)
#> Nothing
maybe::nothing() %>-% \(x) maybe::just(1)
#> Nothing
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.