-
Notifications
You must be signed in to change notification settings - Fork 73
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
Document Polysemy.Reader.local
#441
Comments
In the traditional > runReader ask 5
5
> runReader (local succ ask) 5
6
> runReader ask [1,2]
[1,2]
> runReader (local (4:) ask) [1,2]
[4,1,2]
> runReader (local (4:) (asks (42:))) [1,2]
[42,4,1,2] It's applicable when you want to use a "state-like" effect, but you don't care about the state persisting after your action runs. My go-to examples are getting the depth of a tree in a contrived/overcomplicated way: import Control.Monad.Trans.Reader
data Tree a
= Empty
| Node (Tree a) a (Tree a)
depth :: Tree a -> Int
depth t = runReader (go t) 0
where
go :: Tree a -> Reader Int Int
go Empty = ask
go (Node l _ r) = do
l' <- local succ $ go l
-- note how we also need to local succ for the right subtree,
-- because the succ from above doesn't "carry over" to here
r' <- local succ $ go r
pure $ max l' r' and working with nameless lambda terms, where when you enter a lambda, you want to shift all the variables inside the body of the lambda up by one (in a similar manner with something like |
I think the takeaway of this might be that we should include documentation for effects even if they're just replications of common functionality from |
How to use
local
function? Googling was also not sufficient.The text was updated successfully, but these errors were encountered: