Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate Each into own package #1050

Open
phadej opened this issue Oct 12, 2023 · 3 comments
Open

Separate Each into own package #1050

phadej opened this issue Oct 12, 2023 · 3 comments

Comments

@phadej
Copy link
Collaborator

phadej commented Oct 12, 2023

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.

@phadej
Copy link
Collaborator Author

phadej commented Oct 12, 2023

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.

@RyanGlScott
Copy link
Collaborator

RyanGlScott commented Oct 12, 2023

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?

@phadej
Copy link
Collaborator Author

phadej commented Oct 12, 2023

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants