Live data from Hacker News

Lenses in Julia

juliaobjects.github.io

1–10 of 68 posts

Re: Lenses in Julia

#2
Nice to see this passed around on Hacker News. I think the whole concept of lenses is super cool and useful, but suffered from the usual Haskellificiation problems of being presented in an unnecessarily convoluted way.

I think Accessors.jl has a quite nice and usable implementation of lenses, it's something I use a lot even in code where I'm working with a lot of mutable data because it's nice to localize and have exact control over what gets mutated and when (and I often find myself storing some pretty complex immutable data in more 'simple' mutable containers)

Re: Lenses in Julia

#4
Certain aspects of this me of the modf macro for Common Lisp:

https://github.com/smithzvk/modf

You use place syntax like what is used with incf or setf, denoting part of some complex object. But the modification is made to the corresponding part of a copy of the object, and the entire new object is returned.

Re: Lenses in Julia

#5
post #3

Was hoping this was data lenses, like cambria from ink&switch https://www.inkandswitch.com/cambria/ Not sure how "A Lens allows to access or replace deeply nested parts of complicated objects." is any different from writing a function to do the same? Julia curious, very little experience

Yes lenses are pairs of functions that allow bidirectional data transformations. One function acts like a getter and one function acts like a setter. The signatures of the functions are designed to compose nicely. This allows to compose complex transformations from a few simple building blocks.

In the end it is really just function composition but in a very concise and powerful way.

In your cambria example the lens is defined as yaml. So this yaml needs to be parsed and interpreted and the applied to the target data. The rules that are allowed to be used in the yaml format must be defined somewhere. With pure functional lenses the same kind of transformation rules can be defined just by function composition of similar elemental rules that are itself only pairs of functions.

Re: Lenses in Julia

#6
post #3

Was hoping this was data lenses, like cambria from ink&switch https://www.inkandswitch.com/cambria/ Not sure how "A Lens allows to access or replace deeply nested parts of complicated objects." is any different from writing a function to do the same? Julia curious, very little experience

Yes lenses are pairs of functions that allow bidirectional data transformations. One function acts like a getter and one function acts like a setter. The signatures of the functions are designed to compose nicely. This allows to compose complex transformations from a few simple building blocks. In the end it is really just function composition but in a very concise and powerful way. In your cambria example the lens i…

To be clear, cambria is not mine

> So this yaml needs to be parsed and interpreted and the applied to the target data. The rules that are allowed to be used in the yaml format must be defined somewhere.

I wasn't trying to get into the specific technology. The Julia still needs to be parse, and while Yaml has them separate, CUE does not (which is where I write things like this and have building blocks for lenses [1], in the conceptual sense)

In the conceptual sense, or at least an example of one, lenses are about moving data between versions of a schema. It sounds like what you are describing is capable of this as well? (likely among many other things both are capable of)

[1] https://hofstadter.io/getting-started/data-layer/#checkpoint...

Re: Lenses in Julia

#8
post #6

Earlier quoted context omitted.

Yes lenses are pairs of functions that allow bidirectional data transformations. One function acts like a getter and one function acts like a setter. The signatures of the functions are designed to compose nicely. This allows to compose complex transformations from a few simple building blocks. In the end it is really just function composition but in a very concise and powerful way. In your cambria example the lens i…

To be clear, cambria is not mine > So this yaml needs to be parsed and interpreted and the applied to the target data. The rules that are allowed to be used in the yaml format must be defined somewhere. I wasn't trying to get into the specific technology. The Julia still needs to be parse, and while Yaml has them separate, CUE does not (which is where I write things like this and have building blocks for lenses [1],…

Yes functional lenses are very good at transforming between between schematas.

You can think of it as an functional programming based embedded domain specific language for transforming immutable data structures into each other. Sure there are other ways to do it but its like generalized map/filter/reduce class of functions vs doing the same imperatively by hand or in other ways

Re: Lenses in Julia

#9
post #6

Earlier quoted context omitted.

To be clear, cambria is not mine > So this yaml needs to be parsed and interpreted and the applied to the target data. The rules that are allowed to be used in the yaml format must be defined somewhere. I wasn't trying to get into the specific technology. The Julia still needs to be parse, and while Yaml has them separate, CUE does not (which is where I write things like this and have building blocks for lenses [1],…

Yes functional lenses are very good at transforming between between schematas. You can think of it as an functional programming based embedded domain specific language for transforming immutable data structures into each other. Sure there are other ways to do it but its like generalized map/filter/reduce class of functions vs doing the same imperatively by hand or in other ways

hmm, that makes it sound closer to CUE, where all values are immutable

CUE is in the logical family with Prolog and is not Turing Complete

Re: Lenses in Julia

#10

I have to admit I don’t really understand the point of doing this instead of just obj.a = 2 or whatever.

Immutability is a central concept in functional programming.
Post reply on HN