Re: Argument for "foreach"

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

I was going to say that for a language that has a flexible sequence as its main
data type that foreach would make a lot of sense, but I already said it in the
thread that you posted. blink

You could always write a foreach routine that takes a routine ID:
procedure foreach(object list, integer rid)
    -- define your called procedure so that it takes an object and an index
    for i = 1 to length(list) do
        call_proc(rid, {list[i], i})
    end for
end procedure -- foreach

You could also define a function called map that does the same thing but calls a
function and returns a modified sequence instead:
function map(object list, integer rid)
    sequence out
    
    out = list -- allocate enough memory to start
    
    -- define your called function so that it takes an object and an index
    for i = 1 to length(list) do
        out[i] = call_func(rid, {list[i], i})
    end for

    return out
end function -- map

There are other sequence operations that I would like to see built in, like
sum(), but what can you do?

--
"Any programming problem can be solved by adding a level of indirection."
--anonymous
"Any performance problem can be solved by removing a level of indirection."
--M. Haertel
j.

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

Search



Quick Links

User menu

Not signed in.

Misc Menu