Let's Talk About Lambda Functions!

Carl Banks imbosol at vt.edu
Sat Jul 27 17:45:02 EDT 2002


Alex Martelli wrote:
> Carl Banks wrote:
>        ...
>>>>     def sort_the_right_way (f, a):
>        ...
>> Come on!  Wrapping up a sort in a function is an "ambitious
>> framework"?  I'm talking about writing a single function because a
>> certain sort appears a dozen or so times in the code.  I'm sure you're
>> aware of the advantages of putting oft-repeated code into one place.
> 
> If the SAME sort appears repeatedly, it surely needs to be a
> function.  But you do have to get your parameters right when
> "same" is not exactly "same".  And if it gets to...:
>        ...
>>>>     sort_the_right_way (lambda x:x[1], a)
> 
> ...the lambda suggests imperfect factorization.  If this "same" sort
> does occur often, IS it the case that this is the ONLY place where it 
> needs to be by the 1-th item?  Or, by AN item?
> 
> Statistically, it IS possible that the sort is both frequent enough
> to deserve factoring out AND yet so varied and diverse in the criteria
> to use that a general function is the only effective way to express
> them AND that this one in particular is a freak case never to reoccur,
> so that naming the sortfield-selector function (while not a problem)
> MIGHT be fractionally suboptimal.
> 
> The combination of each of these unlikely factors, all together,
> multiplies down to a VERY small chance.

Not for me.  I have found this "very small chance" you speak of to be
common in my programming, although not for sorting.  I pass functions
around a lot, both by preference, and because it is useful for the
programs I write (a lot of deeply nested and arbitrary structure).

So yes, when I've done this, it has been proper factorization.  It's
certainly been more ideal than repeating lots of similar "global"
operations that vary only in the "local" operation.


[snip]
>>> Somebody ambitious enough to encapsulate DSU should surely proceed
>        ...
>> So now you're advocating building a framework of closures to replace
> 
> No, I'm saying that IF somebody was ambitious enough to encapsulate
> DSU, then the same somebody should be consistent and proceed along
> the same route.  "If the fool would persist in his folly he would 
> become wise" (Blake).  Can't you read?

Evidently not.  Sorry about that.

You're wrong, though.  As I've said, I have good reason to wrap up my
sort functions, but no such good reason to wrap up functions I pass to
it that way.  As I've said, I pass around all kinds of functions, and
that is where I face danger of trying to overgeneralize.


[snip]
>> What I'm saying is, your objection that this use of lambda only is
>> necessary because of over- and under-generalization isn't true for my
>> circumstances.
> 
> I don't know you well enough to use my judgment about your specific,
> individual circumstances, but I *DO* know that I *NEVER* said, implied,
> inferred or let it be suspected that "this use of lambda is
> *NECESSARY*" (my emphasis).

Again, I'm sorry.  I misspoke.  If I replace necessary with tempting,
is that what you were objecting?  (Or were you objecting that the
tempation to use lambda was because of the over- and
under-generalization?)

Well, whatever.  All I'm saying is, for me, with the way I organize
and factor code, the situation where it is tempting to use lambda
comes up a lot, and not because of any over- or under-generalization
on my part.


[snip]
>> Personally, I don't care about saving lines, per se.  For me, lambda
>> helps me to avoid stopping mid-function call, scrolling many or few
>> lines to implement the function as a def, and returning.  I.e.,
>> lambdas save me cursor movement.
> 
> In VIM, I hit ESC, uppercase-O, and I'm in insert mode on a newly
> inserted immediately-previous line with correct indent, ready to
> flow on with def and so on.  I assume more powerful editors and
> IDEs, such as EMACS and the like, must have facilities at least as
> powerful.  Thus, the "saving" seems utterly trivial.

It's not about keystrokes.  Stopping mid-function-invocation to define
a function stops my train of thought, one way or another.  Regaining
my train of thought, and my cursor position, is what I find tiring.


-- 
CARL BANKS
http://www.aerojockey.com



More information about the Python-list mailing list