optics-dev / optics-dev/Monocle
Zip on Lens
Nobody has claimed this yet.
- Dominant language
- Scala
- Stars
- 1.7k
- Forks
- 207
- Avg merge
- 7h 59m
- Merged PRs (30d)
- 5
Description
The idea is to compose n Lenses with the same starting type to obtain a single Lens targeting a tuple.
case class User(name: String, address: Address)
case class Address(streetNumber: Int, postCode: String)
(GenLens[User](_.name), GenLens[User](_.address.streetNumber)).zip
// res: Lens[User, (String, Int)] = …
This operation can break the Lens laws if some Lenses intersect, e.g. two Lenses that point to the same field.
val names = (GenLens[User](_.name), GenLens[User](_.name)).zip
// names: Lens[User, (String, String)] = …
val eda = User(“Eda”, Address(12, “ABC”))
val updated = names.replace((“Bob”, “Alice”))(eda)
// updated: User = User(“Alice”, Address(12, “ABC”))
names.get(updated)
// res: (String, String) = (“Alice”, “Alice”)
API questions
- How many overloaded versions with different numbers of parameters should we create? e.g. zip 2 Lenses, zip 3 Lenses, zip 4 Lenses, …
- Is there a way to generalise this in Scala 3? If yes, should we make this feature Scala 3 only?
- Where should we define zip?
- On the
Lensclass:lens1.zip(lens2, lens3) - On the
Lenscompanion object:Lens.zip(lens1, lens2, lens3) - On an extension method to tuples,
(lens1, lens2, lens3).zip. This works like cats but it requires a separate import to get the extension.
- On the
- Should we add
zipWiththat takes anIso?
case class Pair[A](first: A, second: A)
val tupleToPair: Iso[(String, String), Pair[String]] = GenIso[Pair[String]].fields.reverse
(
GenLens[User](_.name),
GenLens[User](_.address.postcode)
).zipWith(tupleToPair)
// res: Lens[User, Pair[String]] = ...
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No files, tests, or entry points are named. Review the existing Lens and tuple-extension APIs, then investigate the Scala-version constraints and Lens-law implications described in the examples. Done means the API shape, supported arities or generalization, and behavior for intersecting lenses are decided.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- scala
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100