4 ms·
This kind of thing is always a balancing act. There's the argument that implementation details should be hidden, therefore separate named functions should be d
by chriswarbo 10y ago
This kind of thing is always a balancing act.
There's the argument that implementation details should be hidden, therefore separate named functions should be defined and their behaviour should be clear from the name. In my experience, those who go too far in this direction may not be used to first class functions; for example, I worked on a PHP project which dated way before the addition of closures, and even their JS had no lambdas: all functions, even trivial one-liners which were only used once, were given top-level, globally-scoped names, defined all together in a separate file.
There's the argument that each expression shouldn't do too much, hence pulling a lambda out into a (locally scoped) name.
There's the argument that variables which are only used once should be written inline to avoid the mental cost of indirection.
There's the argument that values should have the tightest possible scope.
There's the argument that common 'utility functions' should be pulled out to a separate place.
In situations like this, I usually favour doing the 'real work' in a locally scoped function, and writing the argument-plumbing inline. That lies somewhere in between the author's "labelled_lambda" (tightly scoped definitions, but conflates processing with argument juggling) and "isolated_functions" (separates processing from argument juggling, but has a wider scope).
PS: Having only dabbled in Erlang, to me the real problem exposed by those examples is the lack of a combined lambda/case, e.g. as (eventually) found in Haskell https://unknownparallel.wordpress.com/2012/07/09/the-long-and-epic-journey-of-lambdacase-2/ https://unknownparallel.wordpress.com/2012/07/09/the-long-an...
- geocar 10y ago> PS: Having only dabbled in Erlang, to me the real problem exposed by those examples is the lack of a combined lambda/case, All lambda in Erlang is case: do_whatever(Keys, SomeParameter) -> Lookups = lists:map(fun external_lookup/1, Keys), lists:foreach(fun ({ok, V}) -> do_side_effecty_thing(V, SomeParameter); ({error, R}) -> report_some_failure(R) end, Lookups).
- chriswarbo 10y agoAh, sorry you're right; I mustn't have played with Erlang enough to fade out the punctuation (unlike Lisp, where https://github.com/tarsius/paren-face https://github.com/tarsius/paren-face can do it for me ;) ).
- strmpnk 10y agoI came away with a similar reaction, but there is no need to map separately first: https://news.ycombinator.com/item?id=13136056 https://news.ycombinator.com/item?id=13136056 Sometimes I do wish Erlang had something like F#'s active patterns because it'd make this sort of thing trivial to add to standard pattern expressions. Maybe someday I'll figure out how to add it myself.