Re: better flatten()?
- Posted by petelomax Jul 27, 2015
- 1543 views
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