4 ms·
There is quite widespread use of monads in the standard library (albeit with a very targeted effort to hide their nature and make them very concrete).
by nv-vn 10y ago
There is quite widespread use of monads in the standard library (albeit with a very targeted effort to hide their nature and make them very concrete).
- kibwen 10y agoCan you give an example? I can't think of anything in the Rust standard library that I consider a "monad".
- nv-vn 10y agoIterators are monadic (since they have a flat_map method), Option and Result have the and_then method. Both of these are equivalent to >>=/bind. The fact that they don't implement pure/return doesn't change the fact that they have monadic interfaces because that would only be useful once it was generalized to its own trait, and such a function would be trivially implemented for any of these types. For example, Some(x) works as the return/pure function for Option and the fact that the name differs for each type is not an issue because there's no way to make code generic over all monads as it is. It would be like having a unique trait for each type where there's `bind_option` and `bind_result`, etc.
- kibwen 10y ago`Option` et al may have vaguely monadic interfaces, but don't you need `return` in order to satisfy the monad laws? Regardless of what we determine there, I don't think that matters in the broader context of this thread, which concerns the notions that Rust has the potential to scare people away by including unfamiliar "functional stuff". But seeing an `.and_then` method doesn't scream "functional programming"; method chaining has been long popularized in Javascript, Ruby, etc. Furthermore, AFAIK there's nothing about Rust's pseudo-monadic interfaces on iterators that hasn't already been popularized by LINQ in C#.