ekmett/lens

Separate Each into own package

Opened this issue · 3 comments

This is similar change to previous indexed-traversable, strict, assoc extractions. Each is useful on the own, after all it's just a Traversal i.e. definable (and usable!) with just a base.

optics defines slightly different Each which is more like EachWithIndex, which is reasonable thing to have too. So maybe optics could use the same package / instances.

I'll draft the package soon.

A cursorary search https://hackage-search.serokell.io/?q=%5Einstance.*Each+ suggests that this change will mostly break my packages, if optics-core changes its Each to be the same. Otherwise, if lens keeps re-exporting Each type-class, existing instances (in e.g. diagrams-lib, linear) would continue to work without requiring changes.

Just to clarify, what is the type that you propose for the each method in this split-out package? Currently, it is:

class Each s t a b | s -> a, t -> b, s b -> t, t a -> s where
  each :: Traversal s t a b

But I was under the impression that optics used a different Traversal type than lens. Would this cause issues?

class Each s t a b | s -> a, t -> b, s b -> t, t a -> s where
  each :: Traversal s t a b
  -- default each = traverse

class Each s t a b => EachWithIndex i s t a b | s -> i a, t -> i b, s b -> t, t a -> s where
  ieach :: Applicative f => (i -> a -> f b) -> s -> f t
  -- default ieach = itraverse

EachWithIndex doesn't exist in lens, but that is essentially the optics's Each.

Note: recall that lens has IndexedTraversal, which is a bit more general:

itraversed :: TraversableWithIndex i t => IndexedTraversal i (t a) (t b) a b
itraversed = conjoined traverse (itraverse . indexed)

that's so itraversed can be (efficiently?) used as non-indexed traversal too.


Would this cause issues? The name clash will, as each is both a clas member and an the optic name (c.f. traverse and traversed). However having different name for Each-type-class member and the optic name will probably cause more issues.

One of my motivations is to use each independently of lens or optics (as better mono-traversable option).