3 ms·
Definitely not as terse but far more general, flexible and powerful. With car and cdr you're restricted to working on cons cells while lenses are generic. Some
by chongli 12y ago
Definitely not as terse but far more general, flexible and powerful. With car and cdr you're restricted to working on cons cells while lenses are generic.
Some examples (from Haskell):
>>> ((((5,4,3,2,1),6),7),8,9) & view (_1 . _1 . _2)
6
>>> ((((5,4,3,2,1),6),7),8,9) & (_1 . _1 . _2) .~ 4
((((5,4,3,2,1),4),7),8,9)
>>> let foo = ((((5,4,3,2,1),6),7),8,9)
>>> foo & view (_1 . _1 . _2)
6
>>> let six = _1 . _1 . _2
>>> foo & view six
6
>>> foo & six .~ 4
((((5,4,3,2,1),4),7),8,9)
>>> foo & six .~ "abc"
((((5,4,3,2,1),"abc"),7),8,9)