October 30, 2025
Lens flare in the code wars
Lenses in Julia
Julia’s magic data goggles: fans cheer, skeptics ask ‘why not a simple function’
TLDR: Accessors.jl brings “lenses” to Julia, letting you edit deep parts of data while keeping the original unchanged. The community is split: fans praise clean, composable edits and Lisp-like vibes, while skeptics ask why not just assign a value and move on—some expected Cambria-style data lenses.
Julia devs are passing around Accessors.jl like it’s the hottest new gadget, because these “lenses” let you peek into deep, messy data and swap parts out without changing the original. Think: you can change obj.a and get a fresh copy back. It’s tidy, composable, and comes with “lens laws” that promise predictable behavior. And then the comments lit up. One fan, eigenspace, called lenses “super cool and useful” while roasting past explanations for being too Haskell-ified and convoluted. On the other side, binary132 shrugged: why not just write obj.a = 2 and move on? The vibe: elegant functional patterns vs. simple practical edits.
Meanwhile, the comparison crowd arrived with receipts. kazinator said this feels like Common Lisp’s setf/modf world—same “place” syntax energy, but with copies instead of mutations. Quitschquat chimed in: “Is this like setf in lisp?” And verdverm wanted a different kind of lens entirely, hoping for data-migration magic like Cambria, then asked if this is really any different from a regular function. The memes? Folks joked about “magic goggles for structs,” and the lens law “the last set wins” got rebranded as “Reddit rules.” The takeaway: Accessors.jl brings clean, safe edits to complex data, but the crowd is split between functional elegance and “just do the simple thing.”
Key Points
- •Accessors.jl uses lenses to read and update deeply nested parts of objects in Julia without mutation.
- •Lenses can be created with the @optic macro or composed directly using opcompose, ⨟, or ∘.
- •set returns an updated copy of the object; modify applies a function through a lens to transform values.
- •To implement lenses, define pure functions lens(obj) and Accessors.set(obj, lens, val) that satisfy three lens laws.
- •Equality for lens laws requires an appropriate ≅; Float64/NaN needs isequal or a custom equivalence rather than ==.