Skip to content

Instantly share code, notes, and snippets.

@romanofski
Last active October 30, 2019 03:03
Show Gist options
  • Save romanofski/25ab37af8a63c37f6c9c53465b806d70 to your computer and use it in GitHub Desktop.
Save romanofski/25ab37af8a63c37f6c9c53465b806d70 to your computer and use it in GitHub Desktop.
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.Text as T
import Control.Lens
import Data.Functor.Compose
newtype Mailbody = Mailbody [Paragraph]
deriving (Show)
data Line = Line Int T.Text
deriving (Show)
newtype Paragraph = Paragraph [Line]
deriving (Show)
mailbody :: Mailbody
mailbody = Mailbody [ Paragraph [Line 0 "This is a long", Line 0 "line and so forth", Line 0 "bla"]
, Paragraph [Line 0 "And here", Line 0 "is another", Line 0 "long", Line 0 "one blah"]
]
mbBody :: Traversal' Mailbody [Paragraph]
mbBody f (Mailbody xs) = fmap (\xs' -> Mailbody xs') (f xs)
pLines :: Traversal' Paragraph [Line]
pLines f (Paragraph xs) = fmap (\xs' -> Paragraph xs') (f xs)
lNumber :: Lens' Line Int
lNumber f (Line n t) = fmap (\n' -> Line n' t) (f n)
main :: IO ()
main = do
let mb' = iover (indexing (mbBody . traversed . pLines . traversed)) (set lNumber) mailbody
print mb'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment