Live data from Hacker News

ADT through the looking glass – lenses, prisms and other optics in Scala

kubuszok.com

11–17 of 17 posts

Re: ADT through the looking glass – lenses, prisms and other optics in Scala

#11
post #4

I like working in Scala quite a bit, but always wince at the "instead of using threading primitives consider this 10x more exotic immutable approach" lead-in. Does anyone here really work that way?

What matters is the complexity of the systems built out of these things. Threading primitives are simpler to understand in isolation, but lead to non-deterministic behavior.

I've only used optics in a couple small situations, but even if I find some big downside, I'll still be open to "exotic immutable approaches".

Re: ADT through the looking glass – lenses, prisms and other optics in Scala

#13
I find the Monocle library does a good job of describing the motivation for type optics: http://julien-truffaut.github.io/Monocle/

Where I've used this in real code is via circe-optics (json parsing). An example: https://stackoverflow.com/questions/36724068/how-to-parse-a-...

Re: ADT through the looking glass – lenses, prisms and other optics in Scala

#14

Takes a long time to parse because it the text has a lot of typos.

>> " Takes a long time to parse because it the text has a lot of typos. " Your comment has typos too :-)

You comment has unbalanced parens! (;

Re: ADT through the looking glass – lenses, prisms and other optics in Scala

#15

I mainly program in low-level imperative languages (mostly C/C++), and occasionally dabbled in some functional languages (like Ocaml and Elm). But I don’t really understand why you would need such a convoluted system to get/mutate some values in a data structure in Scala. Doesn’t Scala allow mutability throughout its language already (unlike Haskell)?

But I don’t really understand why you would need such a convoluted system to get/mutate some values in a data structure in Scala.

It allows you to work with deep data structures in a safe immutable way.

Scala is a nice language for working with data, the standard library, techniques such as lenses and ecosystems such as Spark all contribute to this.

Re: ADT through the looking glass – lenses, prisms and other optics in Scala

#16
Monocle also ships with the macro annotation monocle.macros.Lenses that cuts out even more of the boilerplate and works with intellij's autocomplete

  import monocle.macros.Lenses

  @Lenses
  case class Foo(bar: Int)

  val x = Foo(1)
  assert(Foo.bar.get(x) == 1)

Re: ADT through the looking glass – lenses, prisms and other optics in Scala

#17

Takes a long time to parse because it the text has a lot of typos.

>> " Takes a long time to parse because it the text has a lot of typos. " Your comment has typos too :-)

Welp, you're right, that was hypocritical of me.
Post reply on HN