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!
56
Upvotes
View all comments
Show parent comment
1
u/WormRabbit Sep 28 '24
It isn't possible to write a useful Monad trait. Monads in Haskell would, for example, abstract over Option, Result, iterators and futures. But Option and Result are types, while Iterator and Future are traits themselves. There is no way to define a "trait for traits". And the specific types returned by iterator and future combinators are all different and don't follow any "type constructor" pattern.