1. delegate usage and new "constructs"

delegate usage and creating new "constructs"
when it's integrated better with the language, it allows a high level of abstraction and usually more concise code...
just something to think of.

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) 
new topic     » topic index » view message » categorize

2. Re: delegate usage and new "constructs"

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 message » categorize

3. Re: delegate usage and new "constructs"

What is it with the overloading of 'count'?

This could be misleading.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu