🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.misc
Article #2556

Re: Look, Ma, I can dynamically compose functions in C!

#2556
From: Kristoffer Eriksson
Date: Sat, 29 Dec 1990 09:43
73 lines
3391 bytes
In article <553@ssp9.idca.tds.philips.nl> dolf@idca.tds.philips.nl (Dolf Grunbauer) writes:
>In article <19468:Dec2100:23:0790@kramden.acf.nyu.edu> brnstnd@kramden.acf.nyu.edu (Dan Bernstein) writes:
>>
>>In article <1990Dec19.222059.6878@mathrt0.math.chalmers.se> augustss@cs.chalmers.se (Lennart Augustsson) writes:
>>> To see the difference just try to write a function that does function
>>> composition. I claim that this is impossible to do in a portable way.
...
>I think this is not what Lennart wanted. He wanted something like:
>
>  extern int f(), g();
>  int (*fg)();
>  ...
>  fg = f;
>  (*fg)(x);            /* executes f(x) */
>  fg = compose (f,g);
>  (*fg)(x);            /* should execute f(g(x)) */

Just for the fun of it, I'll give you _exactly_ that:

/* -------- Composition package begins ---------- */
int null_fn(int x)
	{ return x; }

struct fun_descr { int (*f1)(), (*f2)(); };

#define decl_composed_fun(name)						\
	struct fun_descr name ## _data = { null_fn, null_fn };		\
	int name(int x)							\
		{ return(name ## _data.f1( name ## _data.f2(x) )); }

#define compose(name, fun1, fun2)	\
	name ## _data.f1 = fun1;	\
	name ## _data.f2 = fun2

/* --------- Composition package ends ----------- */
/* --------- Usage example begins --------------- */

int f(int x)	{ return(x+x); }	/* extern int f();	*/
int g(int x)	{ return(x+1); }	/* extern int g();	*/
decl_composed_fun(fg);			/* int (*fg)();		*/

main() {
  compose(fg, f, null_fn);		/* fg = f;		*/
  printf("%d\n", fg(5));		/* (*fg)(x);		*/
  compose(fg, f, g);			/* fg = compose(f,g)	*/
  printf("%d\n", fg(5));		/* (*fg)(x);		*/
}

/* --------- Example ends ----------------------- */

I've used an ANSI compiler.

>That is: he just wants to use the result of compose just like any other
>pointer to function, so one needs not to known whether it is a real function
>or a composed one.

I made the result of compose just a function in stead of a pointer to a
function, since that gave the shortest implementation, but there's no
problem taking the address of this function. Either way, the composed
function is used just like any other function.

If I wanted to get rid even of the special declaration of composed
functions and allocate composed functions dynamically and make compose()
itself a function in stead of a macro, I could set up a pool of pre-
declared composed functions and have compose() allocate out of that pool.

(I won't mention all the even more powerful and fairly easy non-portable
and mostly-portable solutions that come to mind.)

-- 
Kristoffer Eriksson, Peridot Konsult AB, Hagagatan 6, S-703 40 Oerebro, Sweden
Phone: +46 19-13 03 60  !  e-mail: ske@pkmab.se
Fax:   +46 19-11 51 03  !  or ...!{uunet,mcsun}!sunic.sunet.se!kullmar!pkmab!ske
Technical Details
Message-ID: <4700@pkmab.se>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!snorkelwacker.mit.edu!bloom-beacon!eru!hagbard!sunic!kullmar!pkmab!ske
References: <1990Dec19.222059.6878@mathrt0.math.chalmers.se> <19468:Dec2100:23:0790@kramden.acf.nyu.edu> <553@ssp9.idca.tds.philips.nl>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!snorkelwacker.mit.edu!bloom-beacon!eru!hagbard!sunic!kullmar!pkmab!ske
From: ske@pkmab.se (Kristoffer Eriksson)
Newsgroups: comp.lang.misc
Subject: Re: Look, Ma, I can dynamically compose functions in C!
Message-ID: <4700@pkmab.se>
Date: 29 Dec 90 09:43:30 GMT
References: <1990Dec19.222059.6878@mathrt0.math.chalmers.se> <19468:Dec2100:23:0790@kramden.acf.nyu.edu> <553@ssp9.idca.tds.philips.nl>
Organization: Peridot Konsult i Mellansverige AB, Oerebro, Sweden
Lines: 73