ruben.moor
ruben.moor

Reputation: 1965

how to use the choosing and chosen lenses?

Consider these datatypes:

data SomeNestedData = SomeNestedData { sndValue :: Int }
data OtherNestedData = OtherNestedData { ondValue :: Int }
data Foo = Bar SomeNestedData | Baz OtherNestedData

How about constructing a lens Lens' Foo Int that would get and set a value of type Int regardless of the value of some foo :: Foo?

There is choosing and assuming canonical lenses (and prisms) for the above datatypes we can do:

lensFooInt :: Lens' (Either Foo Foo) Int
lensFooInt = (_Bar . _sndValue ) `choosing` (_Baz . ondValue)

But look at the type of lensFooInt. That's not what I expected.

Leading to two questions:

Edit: I can actually do this:

lensFooInt' = re _Right . ((_Bar . _sndValue ) `choosing` (_Baz . ondValue))

Upvotes: 3

Views: 129

Answers (0)

Related Questions