r/rust • u/ggim76 • Sep 27 '24
Functional Patterns in Rust: Identity Monad
I've been exploring how functional programming concepts like monads can be applied in Rust. Here's my implementation of the Identity Monad which essentially wraps a value and allows for monadic chaining using the >> operator. The code includes an example with the Ackermann function to demonstrate how computations can be structured using this monad.
https://gist.github.com/ploki/9b94a21dbf94e9b24a106fc4df32968c
I'd love to hear your thoughts and any feedback you might have!
57
Upvotes
View all comments
Show parent comment
3
u/ggim76 Sep 28 '24
You'd probably do no very useful work with the Identity Monad. It does nothing except to wrap a value. On the other hand it makes it visible that it looks very much like some let binding and it permits to "emulate" imperative style programming in an functional setting (just one expression in an imperative language).
I'm not yet convinced that a Monad trait has the "span" to cover a maximal range of representable monads in the rust language.
if you're interested in monad transformers, the Parser monad is one of them, as a State Monad transformer of the Non-Determinism Monad. You can find an implementation of it here:
https://gist.github.com/ploki/bc17a7eae422d555113df6e455f6a18b