Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Oct 04 13:46

    julien-truffaut on gh-pages

    Deploy website Deploy website … (compare)

  • Oct 04 13:38

    xuwei-k on master

    Update sbt-mdoc to 2.3.6 (#1304) (compare)

  • Oct 04 13:38
    xuwei-k closed #1304
  • Oct 04 12:02
    scala-steward-bot synchronize #1304
  • Oct 04 11:10
    scala-steward-bot synchronize #1304
  • Oct 04 10:14
    scala-steward-bot synchronize #1304
  • Oct 04 09:16
    scala-steward-bot review_requested #1304
  • Oct 04 09:16
    scala-steward-bot review_requested #1304
  • Oct 04 09:16
    scala-steward-bot review_requested #1304
  • Oct 04 09:16
    scala-steward-bot opened #1304
  • Oct 03 11:12

    julien-truffaut on gh-pages

    Deploy website Deploy website … (compare)

  • Oct 03 11:05

    xuwei-k on master

    Update sbt to 1.7.2 (compare)

  • Oct 03 11:05
    xuwei-k closed #1303
  • Oct 03 10:13
    scala-steward-bot synchronize #1303
  • Oct 03 09:17
    scala-steward-bot synchronize #1303
  • Oct 03 08:09
    scala-steward-bot synchronize #1303
  • Oct 03 07:31
    scala-steward-bot synchronize #1303
  • Oct 03 06:24
    scala-steward-bot synchronize #1303
  • Oct 03 04:50
    scala-steward-bot review_requested #1303
  • Oct 03 04:50
    scala-steward-bot review_requested #1303
Julien Truffaut
@julien-truffaut
I released 3.0.0-M6 yesterday for Scala 3 but I don't see it Maven central https://search.maven.org/search?q=monocle-core
Naftoli Gugenheim
@nafg
@julien-truffaut are you sure releasing completed successfully?
Julien Truffaut
@julien-truffaut
No, I mean the pipeline was successful: optics-dev/Monocle@5849596
butthere might be an issue somewhere else
So it seems M6 artifacts were created
Carlos Quiroz
@cquiroz
I managed to update so at least someting was publised
But I don't see any _3 artifacts
Maybe the search site indexes aren't updated
Actually if I type into the address bar directly there is _3 too: https://repo1.maven.org/maven2/com/github/julien-truffaut/monocle-core_3/
So it seems like the directory page https://repo1.maven.org/maven2/com/github/julien-truffaut/ hasn't been updated
@julien-truffaut I guess it's all there but eventual consistency at play
Has anyone got @Lenses working on 2.13.6?
2.1.0
Tanay Taneja
@TanayTaneja_twitter

Hi, newbie here!
I am trying to use Monocle, but both
import monocle.Focus AND import monocle.macros.syntax.all._
say they cant resolve symbol Focus, and 'all' respectively.

What am I missing out here ?

Julien Truffaut
@julien-truffaut
Which version of Monocle are you using?
Tanay Taneja
@TanayTaneja_twitter
2.1.0
Julien Truffaut
@julien-truffaut
that's why, it is only in the 3.0.0 milestone
Naftoli Gugenheim
@nafg
Tested on scastie, @Lenses works on 2.13.6
So idk why it stops working when I set my project to 2.13.6 rather than 2.13.5, at least in one particular file
Julien Truffaut
@julien-truffaut
any objection to make 3.0.0-M6, the first RC? Originally, I wanted to .field (optics-dev/Monocle#1125) but it can be added later
Carlos Quiroz
@cquiroz
👍
Tanay Taneja
@TanayTaneja_twitter

I am trying to create a lens, which accesses different fields in a case class, eg: GenLens[Person](_.details.age) , GenLens[Person](_.details.address) , GenLens[Person](_.details.name)

Is there a way I can do this by generalizing the changing field? Something like

val attribute = "name"
GenLens[Person](_.details.$attribute)
Julien Truffaut
@julien-truffaut
You can generate a Lens[Person, Details] and then compose it with GenLens[Details](_.age) or GenLens[Details](_.address)
not shorter though
Tanay Taneja
@TanayTaneja_twitter
I see :/
@julien-truffaut The above lens is part of a few more changes, I was wondering if I could pack it all inside a function, because the only difference there is in that genlens piece, do you have any suggestions on what could be a better way to do it?
Julien Truffaut
@julien-truffaut
not really, either you break it down in small pieces and you compose the optics or you generate the whole optic using macro like focus in Scala 3
fromscalatohaskell
@fromscalatohaskell_gitlab

I'm really stuck on something ... I'm trying to generate at runtime list of lenses to modify a data structure... here's simplified example:

  sealed trait TreeLike
  final case class Tree(id: String, leaves: List[Leaf]) extends TreeLike
  final case class Leaf(i: Int) extends TreeLike

  val tree = Tree(
    "t1", List(Leaf(1), Leaf(2), Leaf(3))
  )

  val treeLens = tree.focus()

  val lenses: List[AppliedLens[Tree, Leaf]] = ??? // list of lenses for leaves where i%2 ==0

how would I go about this? Where I essentially need to fold over runtime values to make lenses
btw kickass library

fromscalatohaskell
@fromscalatohaskell_gitlab
this seems to work, is it "correct" ?
  sealed trait TreeLike
  final case class Tree(id: String, leaves: List[Leaf]) extends TreeLike
  final case class Leaf(num: Int) extends TreeLike

  val tree = Tree(
    "t1", List(Leaf(1), Leaf(2), Leaf(3), Leaf(4))
  )

  val lenses: List[Traversal[Tree, Leaf]] =
    tree.leaves.map {
      x =>
        if (x.num % 2 == 0) {
          val ff = Focus[Tree](_.leaves)
            .andThen(Traversal.fromTraverse[List, Leaf])
            .andThen(
              Prism[Leaf, Leaf] { bi =>
                if (bi == x) Some(bi)
                else None
              }(identity)
            )
          Some(ff)
        } else None
    }.collect { case Some(x) => x }

  val res = lenses.foldLeft(tree) { case (t, lens) =>
    lens.modify(x => Leaf(x.num + 10))(t)
  }
Naftoli Gugenheim
@nafg
best way to know is to test it ;)
Julien Truffaut
@julien-truffaut
It seems that you could use a Traversal (optics that targets many values of the same type), Plated might also be an option, it deals with recursive data structures like your Tree https://github.com/optics-dev/Monocle/blob/master/example/src/test/scala/monocle/JsonExample.scala#L227-L259
fromscalatohaskell
@fromscalatohaskell_gitlab
I use traversal, it's more of a how to decide if we want lens for this field at runtime
the above works but it's weird that inside Prism I check if it's equal to thing the outside map is introducing
the above code would increment Leaf(2) and Leaf(4) because they %2...
but I wonder if there's better way... <== optics noob
Julien Truffaut
@julien-truffaut
if you use the latest version of Monocle, you can use .filter instead of a custom Prism
fromscalatohaskell
@fromscalatohaskell_gitlab
thanks!!! thats a bit nicer
any other tips?
Julien Truffaut
@julien-truffaut
I am not sure why you start with tree.leaves.map {
fromscalatohaskell
@fromscalatohaskell_gitlab
I need to return list of setters for parts of subtree essentially
Julien Truffaut
@julien-truffaut
It seems you could do
Focus[Tree](_.leaves).filter(_.num % 2 == 0)
fromscalatohaskell
@fromscalatohaskell_gitlab
in my real app i have analyser that checks it against some outside rules and returns List(Rule, Setter)
Julien Truffaut
@julien-truffaut
k
fromscalatohaskell
@fromscalatohaskell_gitlab
im not saying there's not better way, that's why I'm asking experts :)
I essentially recrusively validate the rules and zoom into the structure to build up "which rule violated which part of the structure, and setter to change it"
Julien Truffaut
@julien-truffaut
Monocle 3.0.0-RC2 is released with a new organisation (dev.optics)
https://twitter.com/JulienTruffaut/status/1399663531772022785
fromscalatohaskell
@fromscalatohaskell_gitlab
fantastic
Igal Tabachnik
@hmemcpy

Good evening all!

I was wondering how can I improve upon this manual thing I'm doing now?
Suppose I have a case class Person(name: String, age: Int). I receive a PATCH request, containing optional fields that may have changed. What I'm currently doing is manually building a bunch of Person => Person functions, either copying over the new value or returning the given person unchanged, then applying all those "patch" functions one after the other to the initial person.

Surely there's a better way :slight_smile:

case class PatchPersonRequest(name: Option[String], age: Option[Int])

val request = PatchPersonRequest(...)

val patchName: Person => Person = request.name.fold[Person => Person](identity)(n => p => p.copy(name = n))
val patchAge: Person => Person = request.age.fold[Person => Person](identity)(a => p => p.copy(age = a))
...

val updatedPerson = Patch.merge(
    person,
    patchName,
    patchAge
    ...
)

I'm using monocle to update deeply nested properties of person, but even then this is manual and repetitive... and boilerplatey :(

Patch.merge does a foldLeft over the Seq[A => A], applying it to the initial A...