4 ms·
Please post the lens code here so that we may judge how expressive it is :)
by blub 6y ago
Please post the lens code here so that we may judge how expressive it is :)
- lmm 6y agoWith zero effort cfg.lens(tabs).composeOptional(index(2)).lens(_.title) set "Suspended" You could certainly define a shortcut to simplify the `composeOptional` part, but doing it explicitly like this makes it clear that we actually have to make an important choice: what do you want to happen when there is nothing at index 2?
- blub 6y agoThank you. To me that seems significantly more verbose, is it possible at least to pack it into a generic function and apply it to most/all assignments?
- lmm 6y agoThe mixing of the array access (which is another piece of language-level special-case syntax) with the properties is what makes it verbose - "normally" you could just do something like config.lens(tabs[2].title) set "Suspended" Admittedly that relies on a macro, but the macro is pretty lightweight syntax sugar - if you wanted to do it in 100% vanilla code you'd need a .lens at every step and a slightly more explicit way to name the "properties", i.e. config.lens(_.tabs).lens(_(2)).lens(_.title) set "Suspended" > is it possible at least to pack it into a generic function and apply it to most/all assignments? I don't quite understand? You can certainly write generic functions that work for any lens whose "target" is a given type.
- nyanpasu64 6y agoI've heard about lenses, but never actually worked with them. This syntax seems to construct a "path" of sorts from the root to a field. What language is this? What type does the `set` operator/keyword return?
- lmm 6y agoIt's Scala with Monocle (and it's off the top of my head, so apologies for any mistake). `set` usually returns a function for transforming values of the root type, but this simplified syntax will apply it immediately to `config`, so it'll return a copy of config with the modification applied to it.