Say I have a JSON ByteString that looks something like
{
messages: [
{...},
{...}
]
}
I'd like to use lens to get a list/vector of messages out of it. I have a function toMessage
that can turn a Value
into a Maybe Message
.
I've tried this composition key "messages" . values . to toMessage
(to
is from Control.Lens.Getter
but the result is Maybe Message
and it simply becomes Nothing
.
Currently I'm doing this
msgsJson <- c ^? key "messages"
let msgs = toList $ mapMaybe message $ msgsJson ^.. values
(mapMaybe
is from witherable
, toList
is to convert the Vector
into a list)
but I'd like to know if there's a way to compose various lenses to get a single lens that does this.