假设我有以下内容:
{-# LANGUAGE TemplateHaskell #-}
import Control.Lens
data Book = Book {
_author :: String,
_title :: String
} deriving (Show)
makeLenses ''Book
data Location = Location {
_city :: String,
_state :: String
} deriving (Show)
makeLenses ''Location
data Library = Library {
_location :: Location,
_books :: [Book]
} deriving (Show)
makeLenses ''Library
lib :: Library
lib = Library (Location "Baltimore" "MD") [Book "Plato" "Republic", Book "Aristotle" "Ethics"]
我试图通过构成镜头来了解通过多层向下延伸的各种方法。我知道如何进行这些操作:
-- returns "Baltimore"
lib ^. location . city
-- returns a copy of lib with the city replaced
set (location . city) "Silver Spring" lib
但是,如果我想更改书名怎么办?也许我想用 来改变它们map
,或者我只想用 改变第三个!! 2
?看来我应该为此制作一个新镜头。我想我应该用一个中间函数来组成books
andtitle
镜头,即map
or !!
。
books . (!! 2) . title
-- or
books . map . title
我该怎么做呢?