Lenses, Prisms and Optics in Scala | Rock the JVM

Поділитися
Вставка
  • Опубліковано 2 лют 2025

КОМЕНТАРІ • 44

  • @M4rvin66
    @M4rvin66 4 роки тому +12

    I have to say your videos are top content. To the point, clear, concise. Usually I run videos 1,5 and skip through them. No need to that with your videos. Keep it up!

  • @masonedmison7117
    @masonedmison7117 3 роки тому

    I’m convinced this is the best programming channel on UA-cam. Real content, not bs “how I got a job at google” crap. Thanks for the quality content. :)

  • @grooooot
    @grooooot 4 роки тому +1

    This is so cool.... at first I thought lenses are just too much, but with the prism, nice compositions, thanks for the usually clear explanations.

  • @camilongii
    @camilongii 2 роки тому

    I always come back to this video, just as you advise, I fell in love with monocle. Also in the newest versions, we must to use andThen instead composeLens and composePrism. Thanks Daniel!

  • @andreacesaro8721
    @andreacesaro8721 4 роки тому +6

    Great video, as always!

  • @PremSagar-jy5he
    @PremSagar-jy5he 4 роки тому +1

    As usual fantastic and very nicely done

  • @yaroska
    @yaroska 4 роки тому +1

    Nice and simple explanation! THANK YOU!

  • @ali-firatkilic6879
    @ali-firatkilic6879 4 роки тому +1

    Really great video and clear explanations !

  • @TheSunscratch
    @TheSunscratch 4 роки тому +2

    Thanks for the video. I'm wondering if there any performance implications when using optics.

    • @rockthejvm
      @rockthejvm  4 роки тому +1

      The runtime perf degradation should be negligible. The compile time might grow, though, depending on the macro-based optics you use.

  • @Sleight94
    @Sleight94 4 роки тому

    Really nice explanation. At first, I was not sure if lenses are useful, since one can create a similar abstraction relatively easily. But once you combined prisms with lenses, it clicked.
    I think they are called prisms, since prisms in physics are used to split a bundled light stream apart, to observe its components. In this case it is a parent type which gets split apart and we can see what is beneath.

  • @svendvanderveken4024
    @svendvanderveken4024 3 роки тому +1

    Thanks for the very clear explanation. I understand how these constructs can be useful when we want to zoom in one specific part of the data structure. Do they also make sense when we want to zoom in to all the parts of such a structure? Say for example I'm writing some kind of transformer from BrandIdentity to something, I need to traverse the whole nested structure and cover all the types of each trait, not just one specific one. Would it be a good idea to code this a lot of lenses and prisms and compose them together or would it become bloated and defeat the point of Lenses zooming on one particular part?

    • @rockthejvm
      @rockthejvm  3 роки тому +1

      Nice question. It will ultimately depend on how many different things you want to access, and (much more importantly) how often you reuse this access. If you use it once, then lenses aren't that useful for the effort involved.

  • @mourikogoro9709
    @mourikogoro9709 4 роки тому +1

    Nice tutorial again! worth 30k+ views.

    • @rockthejvm
      @rockthejvm  4 роки тому

      Thank you! Will get to that in time...

  • @zone66
    @zone66 Рік тому

    hm, i wonder if i can use the Prisms together with Chimney library, to make Prisms that help me Transform API-Models to Database-Models and Vice versa, without boilerplate code.

    • @zone66
      @zone66 Рік тому

      ah, i mean Iso not Prism.

  • @maciejszklarzewski5764
    @maciejszklarzewski5764 4 роки тому +2

    That's so cool! ✌️😎

  • @DanEllis
    @DanEllis 4 роки тому

    Unless I'm missing something, this seems underwhelming. A prism just replaces a two-branch match expression? And if I want to access a deeply nested field just once, a lens doesn't save me anything?
    I've been seeing mentions of lenses for a long time, so I was hoping to discover something I could put to good use.

    • @rockthejvm
      @rockthejvm  4 роки тому

      The examples are pretty small, but this tool is starting to become the more valuable the more complex your data structures are.

    • @CoreyMinter
      @CoreyMinter 4 роки тому

      These examples opened my eyes since I had heard the term but never taken time to look into it.
      To me it is all win-win: composable, enables DRY principle for this task (vs. WET - write everything twice+), and simple to use. Even at two levels it avoids repeating those growing nested accesses.
      Those examples were intentionally showing each step. But, when you don't need to reuse those intermediate lenses, code simplifies even more. The middle explicit types can be skipped as they are inferred:
      val brandCircleRadiusOptics = GenLens[BrandIdentity](_.icon).composeLens(GenLens(_.shape)).composePrism(Prism {
      case Circle(r) => Some(r)
      case _ => None
      }{ r => Circle(r) })
      val aBrand2 = brandCircleRadiusOptics.modify(_ * 2)(aBrand)

    • @CoreyMinter
      @CoreyMinter 4 роки тому

      my mistake. I think the explicit types are still needed. E.g.
      val brandCircleRadiusOptics2 = GenLens[BrandIdentity](_.icon).composeLens(GenLens[Icon](_.shape)).composePrism(Prism[Shape,Double] {
      case Circle(r) => Some(r)
      case _ => None
      }{ r => Circle(r) })

  • @harishgontu9285
    @harishgontu9285 4 роки тому +1

    Wonderful

  • @hafgrim
    @hafgrim 4 роки тому

    In all cases i do find pattern matching and deconstructing to be nice solutions.
    def fixSpaces(band:RockBand):RockBand = band match {
    case RockBand(a, Guitarist(b, Guitar(c, d))) => RockBand(a, Guitarist(b, Guitar(c, d.replace(" ", "-"))))
    }
    def modifyCircleIcon(brand: BrandIdentity, mod: Double => Double) : BrandIdentity = brand match{
    case BrandIdentity(Logo(c1), Icon(c2, Circle(r))) => BrandIdentity(Logo(c1), Icon(c2, Circle(mod(r))))
    case BrandIdentity(Logo(c1), Icon(c2, a)) => BrandIdentity(Logo(c1), Icon(c2, a))
    }
    Maybe it's just that i have not worked that much with heavy nested structures. If you could, i would love if you would elaborate on why or when monocl is worth it.

    • @rockthejvm
      @rockthejvm  4 роки тому

      Pattern magic is awesome! The problem is when you have to repeat the deconstruction over and over again, which is where libraries like Monocle become useful.

    • @kotobotov
      @kotobotov 3 роки тому

      i prefer this way -> cuz it's super clear and easy to understend, also don't have any unnesesary dependency

  • @dhirendrapandit5530
    @dhirendrapandit5530 4 роки тому +1

    Wooo

  • @DanEllis
    @DanEllis 4 роки тому +4

    Weird guitar. It only has two strings.

  • @gigik64
    @gigik64 3 роки тому +1

    Great video, but Metallica's lead guitarist will always be Dave Mustane.

    • @rockthejvm
      @rockthejvm  3 роки тому +1

      I heard he started a new band.

    • @gigik64
      @gigik64 3 роки тому

      @@rockthejvm Megadeath 😃
      But yeah, for me Metallica ended with him leaving.
      My opinion though.