1. insert(sequence st,sequence what,integer index)

I would suggest changing the logic of insert(), or add a newer insert() and
rename the current one insert_slice().

There are two ways to insert a sequence into another: as an element or as a
slice. Currently, insert() does the latter, and no routine in the stdlib does the
former.

I'd suggest this:
1/ Rename insert() to insert_slice(). Perhaps add to documentation that the
length of the new sequence is length(st)+length(what), with atoms of length 1.
2/ Add a new insert() thus coded:
global function insert(sequence st, object what, integer index)
    if index > length(st) then
        return append(st, what)
    elsif index <= 1 then
        return prepend(what, st)
    end if
    
-- there should be a builtin for the following two statement, 
-- as they needlessly create a new sequence instead of simply shifting 
-- in-place the elements of st, with all the refs and derefs.
    st &= 0
    st[i+1..$] = st[i..$-1]
    st[i] = what
    return st
end function

The length of the new sequence is always length(st)+1.

Note the change from "=1" to "<=1", it should apply to the current insert() as
well.
When what is an atom, insert() and insert_slice() are equivalent.
insert({1,2,3},{0'0},2)={1,{0,0},2,3}
insert_slice({1,2,3},{0,0},2}={1,0,0,2,3}

Another alternative would be to keep insert() as it is now, and coin a new name
for the routine that inserts as an element. I couldn't find a satisfactory one.

CChris

new topic     » topic index » view message » categorize

2. Re: insert(sequence st,sequence what,integer index)

CChris wrote:
> 
> Another alternative would be to keep insert() as it is now, and coin a new
> name
> for the routine that inserts as an element. I couldn't find a satisfactory
> one.
> 
> CChris

Why not insert_element()?



Ken Rhodes
Folding at Home: http://folding.stanford.edu/
100% MicroSoft Free
SuSE Linux 10.0
No AdWare, SpyWare, or Viruses!
Life is Good,  smile

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

3. Re: insert(sequence st,sequence what,integer index)

Kenneth Rhodes wrote:
> 
> CChris wrote:
> > 
> > Another alternative would be to keep insert() as it is now, and coin a new
> > name
> > for the routine that inserts as an element. I couldn't find a satisfactory
> > one.
> > 
> > CChris
> 
> Why not insert_element()?
> 
> 
> Ken Rhodes
> Folding at Home: <a
> href="http://folding.stanford.edu/">http://folding.stanford.edu/</a>
> 100% MicroSoft Free
> SuSE Linux 10.0
> No AdWare, SpyWare, or Viruses!
> Life is Good,  smile

Lengthy.
I just thought about "shoehorn". It inserts /what/ by squeezing it into as
little space as possible, ie 1 element.

CChris

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

4. Re: insert(sequence st,sequence what,integer index)

CChris wrote:
> 
> I would suggest changing the logic of insert(), or add a newer insert() and
> rename the current one insert_slice().
> 

Chris,

This sounds fine. I see you've been committing good changes/fixes to svn, can
you just commit this one too?

Thanks!

--
Jeremy Cowgar
http://jeremy.cowgar.com

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

5. Re: insert(sequence st,sequence what,integer index)

Oh!

On new functions that are not yet documented that you add, can you please add on
top of the function -- TODO: document

Also, if they do not have any unit tests, make it:

-- TODO: test, document

--
Jeremy Cowgar
http://jeremy.cowgar.com

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

Search



Quick Links

User menu

Not signed in.

Misc Menu