Re: delegate usage and new "constructs"

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

here is one example:

-- count.ex 
 
function count(sequence seq, sequence predicate_funcname, object findme) 
   integer count = 0 
   integer func = routine_id(predicate_funcname) 
    for i = 1 to length(seq) do 
        if call_func(func, {seq[i], findme}) then  
            count += 1 
        end if 
    end for 
    return count 
end function 
 
function rem(object a, object b) 
    return remainder(a,b) = 0 -- or could be: a contains b, etc.. 
end function  
 
 
? count({1,2,3,4,5,6,7,8,9,10}, "rem",3) 
-- (the result is 3) 

Although that works, it would be better to have the routine_id() call at the point of calling count() rather than have it inside the count routine. The reason is that when a program is translated or bound, unused routines are removed from the resulting program. However when Euphoria finds routine_id(variable) it must keep all routines in the result because your call to routine_id() could potentially use any routine and so it can't get rid of any. When you use routine_id("literal") it knows exactly which routines it can keep.

-- count.ex 
 
function count(sequence seq, integer predicate_func, object findme) 
   integer count = 0 
    for i = 1 to length(seq) do 
        if call_func(predicate_func, {seq[i], findme}) then  
            count += 1 
        end if 
    end for 
    return count 
end function 
 
function rem(object a, object b) 
    return remainder(a,b) = 0 -- or could be: a contains b, etc.. 
end function  
 
 
? count({1,2,3,4,5,6,7,8,9,10}, routine("rem"),3) 
-- (the result is 3) 

Of course, if the program is only ever going to be interpreted, it doesn't matter.

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

Search



Quick Links

User menu

Not signed in.

Misc Menu