How to use "Of" lenses? (Haskell)

I want to write:

minimum $ map _x elems

using lenses. I want to use a lens minimumOf, but I cannot figure out how to use it from its type.

I'm looking for something like

elems ^.. minimumOf x

but does not check:

Prelude Control.Lens Data.Map> let elems = [(1,2),(3,4)] :: [(Double, Double)]
Prelude Control.Lens Data.Map> elems ^.. minimumOf _1

<interactive>:62:11:
    Couldn't match type ‘Maybe a0’
                  with ‘[(Double, Double)]
                        -> Const (Data.Monoid.Endo [a]) [(Double, Double)]’
    Expected type: Getting (Data.Monoid.Endo [a]) [(Double, Double)] a
      Actual type: (a -> Const (Data.Monoid.Endo [a]) a) -> Maybe a0
    Relevant bindings include it :: [a] (bound at <interactive>:62:1)
    Possible cause: ‘minimumOf’ is applied to too many arguments
    In the second argument of ‘(^..)’, namely ‘minimumOf _1’
    In the expression: elems ^.. minimumOf _1
+4
source share
1 answer

The documentation seems clear enough to use - how would you improve it?

minimumOf- this is not a lens (or a bypass or something else) - a bend is required (or a bypass or lens or something like that), but it is not useful to use it with a lens because you take at least exactly one thing) and look for the minimum the value that he focuses on in the structure. For instance.

λ> minimumOf (traverse . _1) [(1,'a'),(2,'b')]
Just 1

Prelude minimum - - , , .

+6

Source: https://habr.com/ru/post/1569858/


All Articles