anonymous functions
Wayne A. Throop
throopw at xyzzy.UUCP
Wed May 11 07:55:53 AEST 1988
> chris at mimsy.UUCP (Chris Torek)
Once again, Chris writes the exact article I had in mind, only better
than I might have done it. I have only one nit to pick. In telling
"what good are they" of anonymous functions, Chris says:
> As for uses, anonymous functions are much like anonymous aggregates:
> you use them to pass to other functions, or to set local variables
> (in C, pointers to functions).
... which is exactly correct, but he omits giving a convincing example,
like to supply a signal handler routine that just sets an external bit,
or a compare or swap routine for a qsort, or a hash routine for a table
manipulation routine, or whatever. The point is, why name this little
bit of code that is required to be a function because of the way the
library is organized? When extremely short as in these examples, giving
it a name is silly and wastefull.
Example invocations:
signal( SIGINT, void (int sig){ interrupted = 1; } );
qsort( (void *)tbl, n_elt, n_elt*sizeof(tbl_elt_t),
int (void *a, void *b){
return( strcmp( (tbl_elt_t *)a->name,
(tbl_elt_t *)b->name ) ); } );
/* crummy hash function, but good enough for whatever purpose: */
hfind( name, table, int (char *s){ return(*s); } );
--
He wonders if he too might have made a similar mistake.
--- "Seen and Not Seen" by Talking Heads from "Remain in Light"
--
Wayne Throop <the-known-world>!mcnc!rti!xyzzy!throopw
More information about the Comp.lang.c
mailing list