I can modify Map using focus and index just fine. Is there a similar functionality in Monocle to update a sequence at given index?
import monocle.syntax.all._
case class C(member: Int)
val map = Map(0 -> C(0), 1 -> C(1))
case class MapContainer(seq: Map[Int, C])
val containedMap = MapContainer(map)
containedMap.focus(_.seq).index(0).replace(C(10)) // works fine
val seq = IndexedSeq(C(0), C(1), C(2))
case class Container(seq: IndexedSeq[C])
val containedSeq = Container(seq)
containedSeq.focus(_.seq).index(0).replace(C(10)) // does not compile
The error is Could not find an instance of Index[IndexedSeq[C],Int,A1].
Is such functionality available for Monocle? I was using Quicklens previously and it used at for both maps and sequences. Unfortunately Quicklens Scala 3 support is not very good at this point, therefore I am exploring alternatives.
From what I see
.indexrelies onIndexinstance which should be available for the type that you want to apply index to.If you modify your code to this:
it does compile.
So the issue is lack of instance for
IndexedSeq. Among the build-in instances for standard data types you can see:but from what I see
seqIndexwas added 2 months ago and not released yet (3.1.0 was released a year ago).So your best bet is to use some specific data structure or implement this implicit yourself.