Skip to content

Instantly share code, notes, and snippets.

@Steell
Last active June 21, 2017 21:56
Show Gist options
  • Save Steell/5c8f6b2f6d2f5b62c8e7e70f711fca2e to your computer and use it in GitHub Desktop.
Save Steell/5c8f6b2f6d2f5b62c8e7e70f711fca2e to your computer and use it in GitHub Desktop.
MonadReader + Lens
data Foo a = Foo { _unFoo :: a }
makeLenses ''Foo
newtype Newline = Newline { unNewline :: String }
instance Monoid Newline where
mempty = Newline []
mappend (Newline []) b = b
mappend a (Newline []) = a
mappend (Newline a) (Newline b) = Newline $ a `mappend` "\n" `mappend` b
printAll :: ReaderT (Foo (Seq (Foo String))) IO
printAll =
putStrLn =<< unNewline <$> view (unFoo.folded.unFoo.to Newline)
@glguy
Copy link

glguy commented Jun 21, 2017

unlinesOf :: Getting (Endo [String]) s String -> s -> String
unlinesOf l s = unlines (toListOf l s)

printAll :: ReaderT (Foo (Seq (Foo String))) IO ()
printAll = lift . putStr . unlinesOf (unFoo . folded . unFoo) =<< ask

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment