2 ms·
I think what they have in mind is a Reader monad. newtype Reader r a = Reader { runReader :: r -> a } instance Monad (Reader r) where pure x =
by dllthomas 6y ago
I think what they have in mind is a Reader monad.
newtype Reader r a = Reader { runReader :: r -> a }
instance Monad (Reader r) where
pure x = Reader (\ _ -> x)
m >>= f = Reader (\ r -> runReader (f (runReader m r)) r)
which allows us to define:
getContext :: Reader r r
getContext = Reader (\ r -> r)
and which spares us threading the context manually.
I don't think that's such a big deal, so long as there's only one thing we're threading.
In some languages, we can be generic over "contexts which provide the thing I want, whether they do other things or not", which is sometimes a much bigger win.