How to use the "Of" lenses? (Haskell)

yong

I want to write:

minimum $ map _x elems

using lenses. I want to use the minimumOf lens, but I can't figure out how to use it from its type.

I'm looking for something like

elems ^.. minimumOf x

but it does not type 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
shachaf

The documentation seems reasonably clear on the usage -- how would you improve it?

minimumOf isn't a lens (or traversal or anything) -- it takes a fold (or traversal or lens or something -- but it's not useful to use it with a lens, because you're taking the minimum of exactly one thing) and looks for the minimum value that it focuses on in a structure. E.g.

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

It generalizes the Prelude function minimum -- which always takes a list -- to a function which takes any sort of value, along with instructions for getting a bunch of things out of that value, and computes the minimum of those things.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Haskell - Lenses, use of 'to' function

From Dev

How to use IORef with lenses?

From Dev

How to use IORef with lenses?

From Dev

Haskell use first level lenses to create complex lens

From Dev

Haskell, Lenses, Getters, and Setters

From Dev

Idiomatic style with lenses in Haskell

From Dev

Haskell, Lenses, Getters, and Setters

From Dev

Haskell lenses: how to make view play nicely with traverse?

From Dev

How can I use overloaded record fields with lenses?

From Dev

How to use Lenses to traverse and assign to some (but not all) elements in a Map

From Dev

How to work around inability to use lenses with existential types?

From Dev

How to combine lenses in "parallel"

From Dev

Haskell - Combine results from separate lenses

From Dev

Implement choosing over 2 lenses in haskell

From Dev

Implement choosing over 2 lenses in haskell

From Dev

How can I use Lenses to perform read-only monadic operation over a sequence held in some state?

From Dev

How to use lenses to look up a value in a map, increase it or set it to a default value

From Dev

How can I use Monocle's built in law implementations to test my own lenses?

From Dev

How to use (.) in Haskell

From Dev

How to use (.) in Haskell

From Dev

Some potential and difficulties in the use of lenses in MonadState

From Dev

Haskell: Reusing FromJSON instances with lenses, lens-aeson, and nested JSON

From Dev

How to modify using a monadic function with lenses?

From Dev

How do I combine lenses and functors?

From Dev

How to write a PPX rewriter generating lenses for records?

From Dev

Haskell - How to use <- in where clauses

From Dev

How to use Haskell's `HashMap`?

From Dev

Haskell: How to use this data structure

From Dev

How to use 'aux' keyword in Haskell

Related Related

  1. 1

    Haskell - Lenses, use of 'to' function

  2. 2

    How to use IORef with lenses?

  3. 3

    How to use IORef with lenses?

  4. 4

    Haskell use first level lenses to create complex lens

  5. 5

    Haskell, Lenses, Getters, and Setters

  6. 6

    Idiomatic style with lenses in Haskell

  7. 7

    Haskell, Lenses, Getters, and Setters

  8. 8

    Haskell lenses: how to make view play nicely with traverse?

  9. 9

    How can I use overloaded record fields with lenses?

  10. 10

    How to use Lenses to traverse and assign to some (but not all) elements in a Map

  11. 11

    How to work around inability to use lenses with existential types?

  12. 12

    How to combine lenses in "parallel"

  13. 13

    Haskell - Combine results from separate lenses

  14. 14

    Implement choosing over 2 lenses in haskell

  15. 15

    Implement choosing over 2 lenses in haskell

  16. 16

    How can I use Lenses to perform read-only monadic operation over a sequence held in some state?

  17. 17

    How to use lenses to look up a value in a map, increase it or set it to a default value

  18. 18

    How can I use Monocle's built in law implementations to test my own lenses?

  19. 19

    How to use (.) in Haskell

  20. 20

    How to use (.) in Haskell

  21. 21

    Some potential and difficulties in the use of lenses in MonadState

  22. 22

    Haskell: Reusing FromJSON instances with lenses, lens-aeson, and nested JSON

  23. 23

    How to modify using a monadic function with lenses?

  24. 24

    How do I combine lenses and functors?

  25. 25

    How to write a PPX rewriter generating lenses for records?

  26. 26

    Haskell - How to use <- in where clauses

  27. 27

    How to use Haskell's `HashMap`?

  28. 28

    Haskell: How to use this data structure

  29. 29

    How to use 'aux' keyword in Haskell

HotTag

Archive