-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should magrittr handle monadic binding? #75
Comments
The changes to magrittr are in the following commits: https://github.com/lionel-/magrittr/commits/monads It evaluates piped functions to check if they have class "monadic". If they do, the |
I'll look forward to looking at it—looks interesting. Not sure my initial enthusiasm for this is shared by my coauthor (maybe with good reason; I know little about the theory here).. In any case I'm sure we will be wiser soon :) |
And I'm quite curious to see how you thought about going about it. ;) but will most likely not have time before after Easter. :( |
and I'm quite curious to see what you guys think about it because I'm not sure what I did makes perfect sense ;) |
I'd want to see an example with a monad not related to errors, because I think in R, it's better to deal with errors using the condition system. |
The idea I think is not to replace R's condition system but rather to complement it. Ultimately, when it's time to process the errors, we'd use Other than that, I'm still struggling to figure out what monads are useful for! The Reader monad looks interesting and if I manage to use it in my work I'll add an example to the gist. |
I can't run it; I get a stack overflow error... Still not really sure what I'd want in general |
weird, not sure why you would get such an error. I noticed I used |
I noticed that one, loading purrrr got me past that.. |
I think this is a very good idea. Anything that moves us in the Haskell direction is very welcome by me. |
That demo is super cool! |
I was also wondering about how useful monads really are without strong typing. But it seems they are used in Clojure so they should be useful in R too. Ultimately, I think we would need someone experimented in monadic programming to implement a library demonstrating how to program with monads in R. |
@hadley and I have had conversations about introducing a monadic bind operator for R. The main usages I had in mind were for reactives (from Shiny) and promises. Promises already have the Reactive expression example w/o bind: rand <- reactive({ invalidateLater(1000); runif(1, max=100) })
rand_int <- reactive({ rand() %>% round }) With bind ( rand <- reactive({ invalidateLater(1000); runif(1, max=100) })
rand_int <- rand %$>% round Quite different than what you were discussing here I think. In fact I don't even think it's really monadic, just functor-y? |
I think so. Are functors sufficient for promise chaining? My feeling is that an operator would be too much syntax for fmap. Functor application is already provided by rand %>%
modify(round) %>%
modify(`*`, 10) %>%
modify(~ . + 42)
# Or equivalently
chain <- . %>% round() %>% { . * 10 + 42 }
rand %>% modify(chain) This would have the advantage of reusing existing syntax. |
I mean, technically, I don't think so. The steps in the promise chain need to be able to return objects that are either promises, or not promises, so promises need to OTOH I wasn't thinking that chains of reactive expressions would do any flattening at all, so just |
@hadley in Haskell there are some examples.
Now, one could write something like this in R:
I think there are cases where what simply a pipable purrr::compose could help:
Another option, here:
Bonus: I think it's worth to mention https://github.com/b-rodrigues/chronicler by @b-rodrigues |
e.g. this experiment with monadic error handling:
https://gist.github.com/lionel-/a9aee3edb45a60a6e393
The text was updated successfully, but these errors were encountered: