Why the prism _Show does not work for a list?

79 views Asked by At

How to explain the 3rd result? Why is the returned list empty?

>[Just 1, Just 2, Nothing, Just 3]^..folded._Just
[1,2,3]

>"1"^?_Show::Maybe Int
Just 1

>["1", "2", "x"]^..folded._Show :: [Maybe Int]
[]

_Just and _Show are prisms:

>:t _Just
_Just :: Prism (Maybe a) (Maybe b) a b
>:t _Show
_Show :: (Read a, Show a) => Prism' String a

But _Show does not work as I would expect. How to get the result [Just 1, Just 2] with lens/prisms? Sure, I can use to readMaybe but I thought I can use existing prism instead of the helper to.

1

There are 1 answers

0
snak On BEST ANSWER

["1", "2", "x"]^..folded._Show :: [Maybe Int] tries to read each element as Maybe Int, so the list needs to be something like ["Just 1", "Just 2", "x"].

You can parse each element as Int, then wrap it in Just to get [Just 1, Just 2].

["1", "2", "x"] ^.. folded . _Show @Int . re _Just