3 ms·
So you abstract a function argument behind an interface: .get() will evaluate the argument when the specific value is needed. It might be a literal wrapper. .
by AtlasBarfed 3y ago
So you abstract a function argument behind an interface:
.get() will evaluate the argument when the specific value is needed. It might be a literal wrapper.
.set() will be called when the argument value is set. It either does nothing in the case of a wrapper that is call-by-value, or back-persists the mutation if the set() actually does something.
I get that doesn't have the sexy lazy eval / late binding of functional langs, but in the imperative model, is that the basic idea?
------
as an aside, I've found that in my (I'm a groovy programmer preferentially) scripts, where I'm doing bash-ish things with a lot of CLI execution, I do things like this:
"cd somedir; ls -la".
with { <look for and parse out a file and its date>}
"df -k".
with { <parse out some device we are interested in> }
and I find it very nice to structure the readability, since the meat of these scripts is the command: I define the data, then "push" it to the processing I want to do to it.
Is there any programming language name for this sort of "value first" model? I find it also flows well for chaining operations, whereas something like:
saveResults(parseOutput(runCommand("df -k")))
forces you to mentally pop the stack. As you read from right to left you have to push the operations in your mental model until you get to the core of what you're working on, while
"df -k".
with{ runCommand(it) }.
with{ parseOutput(it) }.
with{ saveResult(it) }
is more like the natural order
- Izkata 3y agoI don't know groovy, but it looks a bit like method chaining, except using an external object/method "with" to chain standalone functions?
- kaba0 3y agoI think what you are getting at is usually called piping. Some languages have it inbuilt (usually as a |> operator).
- AtlasBarfed 3y agoIt is! What langs have it? I mean besides (shudder) bash? Because groovy with assigns it to an explicit car you name at each state, otle the implicit "it" of the closure scope.
- kaba0 3y agoI believe F# is the most well-known, the others are more esoteric ones.