9 ms·
Lenses in Julia
- eigenspace 11mo agoNice 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)
- verdverm 11mo agoWas hoping this was data lenses, like cambria from ink&switch https://www.inkandswitch.com/cambria/ 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
- laszlokorte 11mo agoYes 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.
- verdverm 11mo agoTo 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/#checkpoints-and-history https://hofstadter.io/getting-started/data-layer/#checkpoint...
- laszlokorte 11mo agoYes 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
- verdverm 11mo agohmm, 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
- versteegen 11mo agoLenses make it more convenient to use immutable structs, which Julia encourages (particularly as they unlock various optimisations).
- antidamage 11mo agoIt's about the annotation triggering a code pre-processor. For example in Lombok, the @Data annotation will create a getter and a setter for every private member, and @Getter and @Setter will do the individual methods respectively. Annotating a class will do every private member, or you can annotate a specific member. A lens is a shortcut to making a getter/setter for something several elements deep, where instead of calling: `parentObject.getChild().setChildAttribute()` you can call: `parentObject.setChildAttributeViaLens()` and not need to write multiple functions in both classes, or even use multiple annotations.
- kazinator 11mo agoCertain aspects of this me of the modf macro for Common Lisp: https://github.com/smithzvk/modf 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.
- binary132 11mo agoI have to admit I don’t really understand the point of doing this instead of just obj.a = 2 or whatever.
- aap_ 11mo agoImmutability is a central concept in functional programming.
- dullcrisp 11mo agoYou can uhhh abstract over the property which seems cool if you’re into abstracting things but also probably shouldn’t be the thing you’re abstracting over in application code. Or on second look the sibling comment is probably right and it’s about immutability maybe.
- laszlokorte 11mo agoSay you want to do obj.child.foo[3].bar += 2 but without mutation, but instead all the data is immutable and you need to do a deep copy along the path. Lenses are an embedded dsl for doing this via syntax that reads similar to to the mutable variant. Additionally it allows to compose many of such transformations.
- o11c 11mo agoThis is equivalent to that for people who are irrationally terrified of mutability, and are willing to abandon performance.
- BoiledCabbage 11mo agoIt's a similar idea to map() but for more complex objects than arrays. When people use "map" in Javascript (or most any other language that supports it) do they do so because "they are terrified of mutability, and are willing to abandon performance?" Your comment reads like the response of someone who is struggling to understand a concept.
- o11c 11mo ago
- Quitschquat 11mo agoIs this like setf in lisp?
- eigenspace 11mo agoIn the simplest case, yes, but in general no, lenses are more general
- max_ 11mo agoGuys, What's you're opinion on Julia? I am thinking of using it for data science work. Any draw backs? or advantages I should know about?
- tomtom1337 11mo agoIf you’re young, new to data science and hoping to get a job in it after some time, then I absolutely recommend learning Python instead of Julia. But if you are just interested in learning a new language and trying it in data science OR are not currently looking to enter the data science job market, then by all means: Julia is great and in many ways superior to Python for data science. It’s just that «everyone» is doing data science in Python, and if you’re new to DS, then you should also know Python (but by all means learn Julia too!).
- Darmani 11mo agoI've worked in about 40 languages and have a Ph. D. in the subject. Every language has problems, some I like, some I'm not fond of There is only one language that I have an active hatred for, and that is Julia. Imagine you try to move a definition from one file to another. Sounds like a trivial piece of organization, right? In Julia, this is a hard problem, and you can wind up getting crashes deep in someone else's code. The reason is that this causes modules that don't import the new file to have different implementations of the same generic function in scope. Julia features the ability to run libraries on data types they were never designed for. But unlike civilized languages such as C++, this is done by randomly overriding a bunch of functions to do things they were not designed to do, and then hoping the library uses them in a way that produces the result you want. There is no way to guarantee this without reading the library in detail. Also no kind of semantic versioning that can tell you when the library has a breaking change or not, as almost any kind of change becomes a potentially-breaking change when you code like this. This is a problem unique to Julia. I brought up to the Julia creators that methods of the same interface should share common properties. This is a very basic principle of generic programming. One of them responded with personal insults. I'm not the only one with such experiences. Dan Luu wrote this piece 10 years ago, but the appendix shows the concerns have not been addressed: https://danluu.com/julialang/ https://danluu.com/julialang/
- jhoechtl 11mo agoIs Julia a general purpose programming language? I mean I did check the web site which contains a "General Purpose" section, yet the articles seem to center around "scientific applications".
- Mikhail_K 11mo agoIn the release 1.12 they finally implemented the ability to create compact executables, so I would say the answer to your question is "yes".
- adgjlsfhk1 11mo agoit is a general purpose language, but it's happy place is math. Most languages (except Fortan Matlab and R) are very much oriented towards writing web servers/compilers etc, so Julia gets lots of wins in science just by virtue of caring more about math. Julia is a completely reasonable general purpose language, but getting people to switch generally requires a ~10x better experience, and Julia can't deliver that for general purpose applications.
- webdevver 11mo agooh... i thought this was going to be about simulating optical lenses and lens physics.
- dopu 11mo agoIs set basically syntactic sugar for deepcopying a struct, mutating the specified field, and then returning that deepcopy? Seems like it could be quite slow.
- pasteldream 11mo agoYes. I’m not sure how slow it is in Julia, but pure functional languages do tend to generate more garbage for this reason. Hopefully the compiler can optimize it away in simple cases. Edit: It’s not deepcopying the whole struct, just the parts that need to point to something new. So if you update a.b.c, it will shallow-copy a and a.b, but nothing else.
- waldrews 11mo agoNow if only the Julia community didn't keep insisting on ligatures and impossible-to-look-up Unicode symbols - the hollow semicolon for compose right to left, seriously?
- Archit3ch 11mo agoCan this be used for Read-Copy-Update (RCU) ?