RWST monad transformer #1382
louthy
announced in
Announcements
Replies: 1 comment
-
|
Can't wait for more articles! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
The new
RWST<R, W, S, M, A>monad-transformer is now fully-featured and ready for real-world use.For the uninitiated, the
RWSTmonad-transformer combines all of the effects of theReader,Writer,State, andMmonads into a single monad. You could imagine a type like this:Which stacks three monad-transformers and the monad
Minto one type. The problem with that is too much transformer stacking leads to lots of nested lambdas. TheRWSTmonad-transformer smushes the Reader/Writer/State into a single layer making it more performant.You can use
Unitfor any of the type parameters if you only need two of the three capabilities. For example, if you only need reader and state effects:Or, reader and writer effects, but not state:
etc.
There's next to no overhead for doing so.
It's also worth noting that
RWSTis a very common mega-monad for constructing domain-specific monads for applications. And so, even thought it's generics heavy, you would normally wrap it up in a type that reduces the generics overhead.Let's say we wanted to create an
Appmonad-transformer. Something that carries app-config, app-state, but can also lift other monads into it.First, create some records to hold the config and the state:
Then create your
Appmonad-transformer type. It is simply a record that contains aRWSTmonad-transformer:Then add some extensions to convert from the
Ktype to the concrete type and toRuntheAppmonad-transformer:This also drops the
Unitoutput from theRWST.Then implement the traits for
App<M>. It should be aMonadTbecause it's a monad-transformer; aReadablebecause it's got anAppConfigwe can read withask; andStatefulbecause it's got anAppStatethat weget,gets,modify, andput:Every member is simply a wrapper that calls the underlying
RWSTmonad-transformer (which does all the hard work).Then we can use our new
Appmonad-transformer:This leverages the
Readabletrait to get at theAppConfig, the leverages theStatefultrait to modify theAppState, and finally does someIO(the liftedMmonad), by callingwriteLine.That's great and everything, but we want to make the underlying type disappear completely. So, if we then wrap up the
Readable.askandStateful.modifyinAppspecific functions:Then we can make the resulting code completely
App-centric:So, by simply wrapping up the
RWSTmonad-transformer you can gain a ton of functionality without worrying how to propagate state, log changes (if you use the Writer part of RWST), or manage configuration. Very cool.Just for completeness, this is what
writeLinelooks like:I'll cover this topic in more detail in the next article of my Higher Kinds series on my blog.
This discussion was created from the release RWST monad transformer.
Beta Was this translation helpful? Give feedback.
All reactions