Giter Site home page Giter Site logo

maybe's People

Contributors

armcn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

mikmart

maybe's Issues

Add result type

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
}

add `from_just`

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.

Generic infix operators?

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.