1. Append or concatenate to each element in sequence

Is there a better/faster way to concatenate or append to each element 
in a sequence than by using a loop, something like illustrated below?


sequence s1

global function seqConcat(sequence srcSeq, object toAppend)
  sequence dstSeq, curElement

  dstSeq = {}
  for i = 1 to length(srcSeq) do
    curElement = srcSeq[i] & toAppend
    dstSeq = append(dstSeq, curElement)
  end for
  return dstSeq
end function  --seqConcat()

s1 = seqConcat({"abc","def","ghi"}, '\n')



My main concern is speed since I will be doing such operations
on some large sequences.

Terry Constant

new topic     » topic index » view message » categorize

2. Re: Append or concatenate to each element in sequence

Terry Constant wrote:
> 
> Is there a better/faster way to concatenate or append to each element 
> in a sequence than by using a loop, something like illustrated below?
> 
> 
> }}}
<eucode>
> sequence s1
> 
> global function seqConcat(sequence srcSeq, object toAppend)
>   sequence dstSeq, curElement
> 
>   dstSeq = {}
>   for i = 1 to length(srcSeq) do
>     curElement = srcSeq[i] & toAppend
>     dstSeq = append(dstSeq, curElement)
>   end for
>   return dstSeq
> end function  --seqConcat()
> 
> s1 = seqConcat({"abc","def","ghi"}, '\n')
> </eucode>
{{{


You don't need to create a new sequence inside the function: you can just use
srcSeq, like this:
sequence s1

global function seqConcat(sequence srcSeq, object toAppend)
  for i = 1 to length(srcSeq) do
    srcSeq &= toAppend
  end for
  return srcSeq
end function  --seqConcat()

s1 = seqConcat({"abc","def","ghi"}, '\n')


--
tommy online: http://users.telenet.be/tommycarlier
Euphoria Message Board: http://uboard.proboards32.com

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

3. Re: Append or concatenate to each element in sequence

Sorry, my previous post had a bug in it: instead of srcSeq &= toAppend, it
should be srcSeq[i] &= toAppend.
sequence s1

global function seqConcat(sequence srcSeq, object toAppend)
  for i = 1 to length(srcSeq) do
    srcSeq[i] &= toAppend
  end for
  return srcSeq
end function  --seqConcat()

s1 = seqConcat({"abc","def","ghi"}, '\n')


--
tommy online: http://users.telenet.be/tommycarlier
Euphoria Message Board: http://uboard.proboards32.com

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

4. Re: Append or concatenate to each element in sequence

> Subject: Append or concatenate to each element in sequence
> 
> 
> posted by: Terry Constant <EUforum at terryconstant.com>
> 
> Is there a better/faster way to concatenate or append to each element 
> in a sequence than by using a loop, something like illustrated below?
> 
> 
> }}}
<eucode>
> sequence s1
> 
> global function seqConcat(sequence srcSeq, object toAppend)
>   sequence dstSeq, curElement
> 
>   dstSeq = {}
>   for i = 1 to length(srcSeq) do
>     curElement = srcSeq[i] & toAppend
>     dstSeq = append(dstSeq, curElement)
>   end for
>   return dstSeq
> end function  --seqConcat()
> 
> s1 = seqConcat({"abc","def","ghi"}, '\n')
> </eucode>
{{{

> 
> 
> My main concern is speed since I will be doing such operations
> on some large sequences.
> 
> Terry Constant

Since the length of dstSeq is known, don't waste time with dynamic mallocs. 
And since your function only knows about a local copy of srcSeq, no need ro do 
any alloc for a temp sequence that will get copied on return:

sequence s1

global function seqConcat(sequence srcSeq, object toAppend)
   for i = 1 to length(srcSeq) do
     srcSeq[i] &= toAppend   --so is it to be appended or to concatenated?
   end for
   return srcSeq
end function  --seqConcat()

s1 = seqConcat({"abc","def","ghi"}, '\n')


I didn't benchmark it, but there are so many reasons for this to be faster...

CChris

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

5. Re: Append or concatenate to each element in sequence

Carlier wrote:
"You don't need to create a new sequence inside the function: you can just use
srcSeq,
like this:"

Thanks for your response. How you suggested is how my actual routine is.
I just used the curElement sequence in my illustration so that it would
be a bit more readable.

Again, thanks for the response. I am hoping (seem to remember reading
something, but cannot find it) for a more built/faster way than this
obvious way.


Terry Constant

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

6. Re: Append or concatenate to each element in sequence

Cuvier wrote:
"--so is it to be appended or to concatenated"

I have not decided yet which I will actually use. Determining now 
(benchmarking and checking usability and so forth) the best option for
for the actual data with which I will be working. Thus I used the same
variable names in my various test routines. Unless someone chimes in
with something really new, it will be concatenation (toConcat) with
the faster routines (not exactly like the one I posted which was for
more for readability) like you, Carlier, and I all agree on. I was
actually hoping for a built-in method that I could not find and am now
concluding does not exist.

Terry Constant

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

7. Re: Append or concatenate to each element in sequence

Christian,

I forgot to say, that your routine initially looks like it is the fastest
and I am going to use it. Thanks again for the help.

Terry Constant

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

8. Re: Append or concatenate to each element in sequence

I did more analysis on my data. I will need to use
both concatenation and appending.

I did some benchmark testing. The two fastest
routines are shown below. An interesting result is
that seqAppend() is a tiny bit faster than seqConcat().
For single character appends/concats they produce
the same results. For string appends/concat I will
have to select which one to use.

A note: my testing was only for short sequences like
the one in the code below.

I am posting this just FYI.


s0 = {"abc","def","ghi","jkl"}

function seqConcat(sequence srcSeq, object toConcat)
   for i = 1 to length(srcSeq) do
     srcSeq[i] &= toConcat
   end for
   return srcSeq
end function  --seqConcat()

function seqAppend(sequence srcSeq, object toAppend)
   for i = 1 to length(srcSeq) do
     srcSeq[i] = append(srcSeq[i], toAppend)
   end for
   return srcSeq
end function  --seqConcat()

s4 = seqAppend(s0, '\n')
s1 = seqConcat(s0, '\n')



Terry Constant

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

Search



Quick Links

User menu

Not signed in.

Misc Menu