Topic: [comp.std.c++] lambda functions proposal


Author: Jens Kilian <Jens_Kilian@bbn.hp.com>
Date: 1998/12/01
Raw View
> From: "David J. Littleboy" <davidjl@gol.com>
> Date: 1 Dec 1998 11:09:26 GMT
>
> Lambda in Scheme creates a snapshot of the state of the context at the point
> the lambda is encountered. This could be matched in C++ by using not
> reference variables but normal variables initialized to the current values
> of their corresponding enclosing context variables, at the obvious cost of
> copying the objects.

STL containers copy their elements; so lambda should be able to do the same.

> [...] lambda would create an object with an operator() of the right type. Of
> course, this is a temporary object and where and how it gets destroyed is
> problematic. (In general, Scheme, having a garbage collector, makes the use
> of such objects painless, whereas in C++, at some point the program would
> have to take care of destroying function objects that get passed back or
> stored in other objects. That makes lambda less attractive in C++ than in
> Scheme.)

Give the functional object a copy constructor and pass it around by value.
This will only be a problem if the lambda needs a lot of local context,
in which case it would probably be better to not use functional objects
anyway.

> I suspect that these two problems (leaking lambda-created function objects
> and the need for copying the free variables in the lambda body (closure
> variables)) mean that this sort of notation may not be reasonable for C++.

But people are doing the EXACT SAME THING with functional objects in the STL,
in MUCH less readable code (excuse the shouting, but this gets tiring).

> Even worse, this is nothing more than a notational convenience: an
> externally declared and defined class with an appropriate operator() and a
> constructor that takes the required free variables as arguments would be
> exactly equivalent. E.g.,
>
> sort(arr, user_comparator(loc));
>
> Is already perfectly good C++.

Yes.  But you left out the declaration of "user_comparator", so this is not
a fair comparison.  The reason I'm in favor of lambda is that it would make
it much easier to use functional objects - not because of some inherent magic,
but because it shifts the burden of mechanically declaring and constructing
functional object classes from the programmer to the compiler.

(Just for the hell of it, I might try to write a preprocessor for transforming
lambdas into functional objects.  I'll then use Bjarne Stroustrups tried and
tested strategy and make the preprocessor widely available.  I'll just need
to get Microsoft to add it to their compiler, and I'll be a large step on my
way to world domination, muahhahah har har hee hee [oops]).

Bye,
 Jens.
--
mailto:jjk@acm.org                 phone:+49-7031-14-7698 (HP TELNET 778-7698)
  http://www.bawue.de/~jjk/          fax:+49-7031-14-7351
PGP:       06 04 1C 35 7B DC 1F 26 As the air to a bird, or the sea to a fish,
0x555DA8B5 BB A2 F0 66 77 75 E1 08 so is contempt to the contemptible. [Blake]


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]