I happened to read Nix-select, a pure query language in nix last week, and I was wondering if someone has actually implemented lenses in nix. I did find some van Laarhoven style implementations, but it seems that there’s no profunctor one with good abstractions, so I thought I’d make one from scratch. It turns out to work, though I’m not sure about the correctness. (And if Nix allowed us to define operators, the code would be much nicer…)
5 Likes
That’s a lesser known “feature”, but you can - somewhat.
Having __sub, __lessThan, __mul, __div will override -, *, <, >=, >, >=, / operators, since those are always desugared to primop calls.
Yeah, but I guees redefining those operators is not a good idea.
I also thought about dot syntax, e.g.
set (compose [(attr "deploy") (attr "green") (attr "active")]) true cloudConfig
v.s.
((((Builder.attr "deploy").attr "green").attr "active").set true) cloudConfig
but the parentheses are bit annoying.
In Haskell (with operators available) this could be:
cloudConfig & (attr "deploy") . (attr "green") . (attr "active") .~ true
this reminds me a bit of partial.lenses, which as a JS library was also about transformations in the JSON domain.
still kinda looking for a use-case where transformations get to the point of justifying using this, but looking forward to trying it. ![]()