How to remove the last item of a serie ?
[1/13] from: rebol665:ifrance at: 5-May-2002 14:00
Hi, rebollers !
My actual problem is to remove the last character of a string. Has anyone a
better way ?
; way 1
test: "abcde"
reverse test
remove test
reverse test
test
; way 2
test: "abcde"
remove back tail test
test
Patrick
[2/13] from: greggirwin:mindspring at: 5-May-2002 9:04
Hi Patrick,
<< My actual problem is to remove the last character of a string. Has
anyone a
better way ? >>
Your second example is obviously preferable to the first. You could use
CLEAR in place of REMOVE, but in this case I don't think there's a
significant difference.
--Gregg
[3/13] from: jason:cunliffe:verizon at: 5-May-2002 12:15
Python has a really nice tail syntax for ends of list, strings, tuples etc.
There must be an easy way to do this as cleanly in REBOL...
What do you all think about this essential idioms?
[Python]
>>> test = "abcde"
'abcde'
>>> test[0]
'a'
>>> test[4]
'e'
>>> test[-1]
'e'
>>> test[-2]
'd'
It works with ranges too
[Python]
>>> test[1:3]
'bc'
>>> test[0:-2]
'abc'
REBOL
>> test: "abcde"
== "abcde"
>> test/5
== #"e"
>> test/-2
== none
any ideas?
./Jason
Note: Python uses zero indexing instead of REBOL's more natural common counting
sense "1", also ":" is a 'range' separator
[4/13] from: g:santilli:tiscalinet:it at: 5-May-2002 20:58
Hi Jason,
On Sunday, May 05, 2002, 6:15:42 PM, you wrote:
JC> any ideas?
>> test: tail "abcde"
== ""
>> test/-2
== #"d"
also:
>> test: head test
== "abcde"
>> pick tail test -2
== #"d"
Picking a range means copying, so it's better to have that done
explicitly instead of implicitly.
Regards,
Gabriele.
--
Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer
Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r
[5/13] from: jason:cunliffe:verizon at: 5-May-2002 15:30
Hi Gabriele
> >> test: head test
> == "abcde"
> >> pick tail test -2
> == #"d"
cool.. So why can't I also use a similar syntax to do
>> remove tail test -2
> Picking a range means copying, so it's better to have that done
> explicitly instead of implicitly.
Can you elaborate on that a little please.
Thanks.
./Jason
[6/13] from: greggirwin:mindspring at: 5-May-2002 15:23
Hi Jason,
<< Python has a really nice tail syntax for ends of list, strings, tuples
etc.
There must be an easy way to do this as cleanly in REBOL...
What do you all think about this essential idioms?
[Python]
>>> test = "abcde"
'abcde'
>>> test[0]
'a'
>>> test[4]
'e'
>>> test[-1]
'e'
>>> test[-2]
'd' >>
That syntax doesn't grab me as being very natural. It kind of makes it look
like series! are circular as well, which they aren't. Anyway, in general I'm
fine with TAIL, SKIP, and BACK. In this particular case, you could write a
remove-tail function which would be even better.
--Gregg
[7/13] from: jason:cunliffe:verizon at: 5-May-2002 18:31
> >>> test[-2]
> 'd' >>
>
> That syntax doesn't grab me as being very natural. It kind of makes it look
> like series! are circular as well, which they aren't. Anyway, in general I'm
Well granted it looks daft with trivial "abcde" as my example. But in Python
when you have long strings, nested lists, tuples and and dictionaries [Python's
essence], it is very nice to be able to count backwards in the same way one
counts forwards. When appending stuff as is common, this make tons of sense.
But as Gabriele pointed out, REBOL lets one do the same partially.
>> test: head test
== "abcde"
>> pick tail test -2
== #"d"
> fine with TAIL, SKIP, and BACK. In this particular case, you could write a
> remove-tail function which would be even better.
So what are nice efficient REBOL idioms for selecting and removing items, and
ranges of items, counting from the tail of big complex series?
./Jason
[8/13] from: greggirwin:mindspring at: 5-May-2002 18:12
Hi Jason,
<< So what are nice efficient REBOL idioms for selecting and removing items,
and
ranges of items, counting from the tail of big complex series? >>
Look at view-ref for all the series related functions. Here are some that
relate directly to this discussion:
AT CLEAR COPY FIND INDEX? INSERT OFFSET? PICK POKE REMOVE SELECT SKIP
SKIP can be used with negative values, which is how I normally move
backwards by more than one position.
--Gregg
[9/13] from: jason:cunliffe:verizon at: 5-May-2002 22:09
> SKIP can be used with negative values, which is how I normally move
> backwards by more than one position.
Gregg
Yay thanks - that's the one :-)
./Jason
[10/13] from: g:santilli:tiscalinet:it at: 6-May-2002 11:05
Hi Jason,
On Sunday, May 05, 2002, 9:30:04 PM, you wrote:
JC> cool.. So why can't I also use a similar syntax to do
>>> remove tail test -2
remove skip tail test -2
JC> Can you elaborate on that a little please.
Most scripting languages are rather slow even if they can be
compiled. Java is rather slow even with the JIT compiler. Why?
Because there's a lot of implicit copying of data.
I ask you, what happens when you write:
$s .= "append";
in PHP? What happens when you do assignments in a lot of
languages? I'll tell you: a lot of data are being copied around.
Without you even realizing, and worse, without you being able to
do anything with it.
REBOL is very different here. Data is not usually copied
implicitly; i.e. something like:
a: b
is always O(1) whatever datatype B is. (The only "implicit"
copying occurring in REBOL is during reallocation, when you grow a
series out of its limits. Even in this case, anyway, you are able
to avoid it, by preallocating the right amount of memory for your
series.)
This means that in REBOL you are really able to control the
complexity of your algorithms, if you are willing to. Most other
scripting languages I know are doing too much things behind the
scenes to make this easy/feasible.
I really think simplicity has too much advantages. :-)
Regards,
Gabriele.
--
Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer
Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r
[11/13] from: jason::cunliffe::verizon::net at: 6-May-2002 8:56
Implicit Copying: WAS => Re: Re: How to remove the last item of a serie
Gabriele
> remove skip tail test -2
:-))
[...snip...]
> REBOL is very different here. Data is not usually copied
> implicitly; i.e. something like:
<<quoted lines omitted: 4>>
> to avoid it, by preallocating the right amount of memory for your
> series.)
What does 0(1) mean?
when you grow a series out of its limits
..Does that mean every time you add/append to it?
In general when I do
>> test: ""
== ""
>> append test {hello }
== "hello "
>> append test {hello }
== "hello hello "
>> append test {hello }
== "hello hello hello "
..is REBOl copying 'test' internally each time?
A real example is when HTML pages being built in Vanilla.
By default they start off with something like:
htmlpage: make string! 1024
htmlhead: {<html><head><title>PageTitle</title></head><body>}
append htmlpage rejoin [htmlhead {<h1>Welcome to this example</h1>}]
; then adds lots more html dynamically before returning the completed page.
Once 'htmlpage' is over 1024, does REBOL also starts copying it in the
background for each addition?
We need to build large image hyperlink matrices in HTML and want REBOL to return
the result string as fast as possible.
> This means that in REBOL you are really able to control the
> complexity of your algorithms, if you are willing to. Most other
> scripting languages I know are doing too much things behind the
> scenes to make this easy/feasible.
>
> I really think simplicity has too much advantages. :-)
Yes Good post Gabriele. Thank you.
./Jason
[12/13] from: g::santilli::tiscalinet::it at: 6-May-2002 21:01
Re: Implicit Copying: WAS => Re: Re: How to remove the last item of a se
Hi Jason,
On Monday, May 06, 2002, 2:56:53 PM, you wrote:
JC> What does 0(1) mean?
In few words, that the algorithm requires constant time (not
dependent on the size of the input). It's the common notation to
express the upper bound to the complexity of an algorithm.
JC> ..Does that mean every time you add/append to it?
Not every time. Only if you go beyond the allocated space.
I.e. when you write:
str: make string! 1024
you get an empty string, but REBOL actually allocates the space
needed for 1024 characters. So you can append to it without REBOL
needing to reallocate the string, until to get to more than 1024
characters. (REBOL will not respect the number you give precisely.
It only guarantees that it is allocating AT LEAST that amount of
space.)
JC> In general when I do
>>> test: ""
JC> == ""
>>> append test {hello }
JC> == "hello "
>>> append test {hello }
JC> == "hello hello "
>>> append test {hello }
JC> == "hello hello hello "
JC> ..is REBOl copying 'test' internally each time?
To answer your question, one should know how much space did REBOL
allocate when you wrote:
test: ""
(You can look at system/stats or use some of the tools on the REB
to discover it.) Also, it might depend on the OS too, since a
smart OS might just increase the allocated space without having to
move any data if there is space available after the end of the
allocated chunk of memory.
JC> Once 'htmlpage' is over 1024, does REBOL also starts copying it in the
JC> background for each addition?
I think it increases it by a factor of 2 each time, up until a
limit (whose value I don't recall), after which it increases
linearly.
JC> We need to build large image hyperlink matrices in HTML and want REBOL to return
JC> the result string as fast as possible.
If you know in advance how much space you'll need, you can
preallocate it. If you don't, you can only make guesses, or use a
trick such as:
my-func: func [a b c /local result] [
result: clear ""
; code that appends to result
copy result
]
so that after calling my-func some times, result (which is not
created each time, but reused) will get to the needed size, and
the copying happens only once when returning the value (copying
that can be avoided in some cases). Of course noone stops the GC
from freeing the used space every time you clear it, but I don't
think it is really doing it, in the current version at least.
Regards,
Gabriele.
--
Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer
Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r
[13/13] from: riusa:email:it at: 7-May-2002 9:42
Re: How to remove the last item of a serie ?
Hi Gabriele,
what you say is one of many other useful information found in this
mailing-list. Why nobody writes a technical book (HTML?) about these
advanced information?! If Rebol has these great features, but nobody
will list and explain them, these features will be lost!
If I project a new car, with many enhancements, many features, in order
to better sell my new "creature", I must publicize these features! Air
bags, ABS, etc...
bye!
--Alessandro--
> Hi Jason,
> On Sunday, May 05, 2002, 9:30:04 PM, you wrote:
<<quoted lines omitted: 33>>
> [rebol-request--rebol--com] with "unsubscribe" in the
> subject, without the quotes.
++++++++++++++++++++++++++++++++++++++++++++
Alessandro Manotti
Presidente dell'Associazione "RIUSA"
Sito web: http://riusa.apritisesamo.net
email: [riusa--email--it]
mailing-list: [riusa-ml--yahoogroups--com]
Telefono: 347.63.43.231
--
Prendi GRATIS l'email universale che... risparmia: http://www.email.it/f
Sponsor:
Vorresti trasformare il tuo salotto in un cinema?
Clicca qui: http://adv2.email.it/cgi-bin/foclick.cgi?mid=402&d=7-5
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted