Haskell Language Learning Note (Lens) (4)

Source: Internet
Author: User

Prisms
data NewTask =  SimpleTask String |  HarderTask String Int |  CompoundTask String [NewTask]  deriving (Show)makePrisms ''NewTask
*Main> a ^? _SimpleTaskJust "Clean"*Main> b ^? _HarderTaskJust ("Clean Kitchen",15)*Main> b ^? _HarderTask._2Just 15*Main> b & _SimpleTask .~ "Clean Garage"HarderTask "Clean Kitchen" 15*Main> b & _HarderTask._2 .~ 30HarderTask "Clean Kitchen" 30
Manual calculation
_Left = prism Left $ either Right (Left . Right)prism bt seta = dimap seta (either pure (fmap bt)) . right'data Market a b s t = Market (b -> t) (s -> Either t a)instance Choice (Market a b) where  right' (Market bt seta) = Market (Right . bt) $ \cs -> case cs of    Left c -> Left (Left c)    Right s -> case seta s of      Left t -> Left (Right t)      Right a -> Right apreview l = getFirst . foldMapOf l (First . Just)foldMapOf l f = getConst . l (Const . f)
Prelude Control.Lens> preview _Left (Left 5)Just 5
Reference links

Haskell Language Learning Notes (Lens) (1)
Haskell Language Learning Notes (Lens) (2)
Haskell Language Learning Note (Lens) (3)
Taking a Close look at Lenses

Haskell Language Learning Note (Lens) (4)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.