optics-dev / optics-dev/Monocle

Zip on Lens

Open
#1,019 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3.x feature
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 Lens class: lens1.zip(lens2, lens3)
    • On the Lens companion 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.
  • Should we add zipWith that takes an Iso?
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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.