I'm confused with the definitions of gets and modify in monad state.
Somewhere says:
gets: "Gets specific component of the state, using a projection function supplied".
modify: Maps an old state to a new state inside a state monad. The old state is thrown away.
That means we can use modify to directly assign a value to a component of state, like here.
But somewhere else says we can supply a function to state and result using modify and gets.
It seems both works, but it's not clear to me which one works where!
You never quoted from the second citation so I'll ignore it in this answer. If you clarify that part of the question then I'll try to answer it.
Your cited source one says:
gets :: MonadState s m => (s -> a) -> m a Source #
Gets specific component of the state, using a projection function supplied.
and
modify :: MonadState s m => (s -> s) -> m () Source #
Monadic state transformer.
Maps an old state to a new state inside a state monad.
So for gets
you can supply a function s -> a
and it will return the a
after applying that function to the current state. For modify
you supply a function s -> s
and the new state is the result of your function applied to the old state.