4

我想写:

minimum $ map _x elems

使用镜头。我想使用minimumOf镜头,但我无法从它的类型中弄清楚如何使用它。

我正在寻找类似的东西

elems ^.. minimumOf x

但它不输入检查:

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

1 回答 1

6

文档在用法上似乎相当清楚——你将如何改进它?

minimumOf不是镜头(或遍历或任何东西)-它需要折叠(或遍历或镜头或其他东西-但将其与镜头一起使用是没有用的,因为您只需要最少的东西)和寻找它在结构中关注的最小值。例如

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

它将 Prelude 函数minimum(总是接受一个列表)推广到一个接受任何类型值的函数,以及从该值中获取一堆东西的指令,并计算这些东西的最小值。

于 2015-01-04T05:48:37.863 回答