Re: better flatten()?

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

Independently of Greg, and with the now recursive passing of delim, I also realised that strings needed a bit of help.

This is my latest version (I'll take a closer look at Greg's in a minute, and (btw) I'm just using the Phix builtin string() function here)

global function flatten(sequence s, object delim = "") 
sequence res = "" 
object si 
 
    for i=1 to length(s) do 
        if i!=1 then 
            res &= delim 
        end if 
        si = s[i] 
--minor edit (replaced with next 2 lines): 
--      if atom(si) then 
--          res = append(res,si) 
--      elsif string(si) then 
        if atom(si) 
        or string(si) then 
            res &= si 
        else 
            res &= flatten(si,delim) 
        end if 
    end for 
    return res 
end function 

This makes

s = flatten({"one", "two", "three"}, '\n') 

produce

-- s is "one\ntwo\nthree" 

rather than (ugh)

-- s is "o\nn\ne\nt\nw\no\nt\nh\nr\ne\ne" 

Pete

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

Search



Quick Links

User menu

Not signed in.

Misc Menu