Re: RosettaCode

new topic     » goto parent     » topic index » view thread      » older message » newer message
PeteE said...

Still, this doesn't seem to be in the spirit of function composition. This compose function returns a "compose-id" which isn't compatible with routine-ids. With this you can't further compose two composed functions, although you've helpfully allowed any number of functions to be composed which should be equivalent. You still can't use a compose-id with anything expecting a routine-id.

Yes, it's not as pure as what you could get if euphoria implemented more functional capabilities. Given more time, I suppose I could come up with a more general implementation, though that's already more generic than some of the implementations given at Rosetta Code.

I suppose we could put compositions into sequences or something, and then we'd know how to compose arbitrary routine ids and compositions.

sequence compositions = {}  
function compose( sequence ids ) 
	sequence comp = {} 
	for i = 1 to length( ids ) do 
		if sequence( ids[i] ) then 
			comp &= compositions[ids[i][1]] 
		else 
			comp &= ids[i] 
		end if 
	end for 
	compositions = append( compositions, ids )  
	return { length( compositions ) } 
end function  
 
function call_composition( sequence comp, sequence x ) 
	comp = compositions[comp[1]] 
	for i = length( comp ) to 1 by -1 do  
		if sequence( comp[i] ) then 
			x = call_composition( comp[i], x ) 
		else 
			x = call_func( comp[i], x ) 
		end if 
	end for  
	return x 
end function  
 
function F( integer a, integer b ) 
	return { a + b, a - b } 
end function 
 
function G( integer a, integer b ) 
	return {a * 2, b * 3 } 
end function 
 
constant 
	f = routine_id("F"), 
	g = routine_id("G"), 
	fg = compose( { f,  g } ), 
	ffg = compose( {f, fg } ) 
 
sequence s = call_func( f, call_func( f, call_func( g, { 1, 2 } ) ) ) 
? s 
? call_composition( ffg, { 1, 2 } ) 

Matt

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu