Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

haskell - Using a Lens to read multiple fields

Given the types

data Prisoner = P { _name   :: String
                  , _rank   :: Int
                  , _cereal :: Cereal }

data Cereal = C { _number             :: Int
                , _percentDailyValue  :: Map String Float
                , _mascot             :: String }

I could extract someone's name, rank, and cereal number via pattern matching:

getNameRankAndCerealNumber_0 :: Prisoner -> (String, Int, Int)
getNameRankAndCerealNumber_0 (P { _name=name
                                , _rank=rank
                                , _cereal = C { _number=cerealNumber }}
                             ) = (name, rank, cerealNumber)

Alternately, I could use lenses to extract each part separately

makeLenses ''Cereal
makeLenses ''Prisoner

getNameRankAndCerealNumber_1 :: Prisoner -> (String, Int, Int)
getNameRankAndCerealNumber_1 p = (p ^. name, p ^. rank, p ^. cereal.number)

Is there a way to extract all three simultaneously in a single traversal of the data structure?

Some way to combine Getters, Getter s a -> Getter s b -> Getter s (a,b)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

We can use the Applicative instance of the ReifiedGetter newtype from Control.Lens.Reified:

runGetter $ (,) <$> Getter number <*> Getter mascot

In general, the newtypes in Control.Lens.Reified offer a lot of very useful instances for getters and folds.

Note#1: Notice that we are combining the lenses as getters, and getting a getter in return. You can't obtain a composite lens in this way, as there would be problems if their "focuses" overlap. What could be the proper setter behaviour in that case?

Note#2: The alongside function lets you combine two lenses, getting a bona-fide lens that works on the two halves of a product. This is different form the previous case because we can be sure the lenses don't overlap. alongside comes in handy when your type is a tuple or has an isomorphism to a tuple.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...