Why do 'append and 'repend return 'head series?
[1/3] from: al::bri::xtra::co::nz at: 10-Aug-2002 16:29
Why do 'append and 'repend return the head of the series? (I'm using latest
Rebol/Core)
>> source append
append: func [
{Appends a value to the tail of a series and returns the series head.}
series [series! port!]
value
/only "Appends a block value as a block"
][
head either only [
insert/only tail series :value
] [
insert tail series :value
]
]
>> x: 'test/foo/bar
== test/foo/bar
>> append x 'ping
== test/foo/bar/ping
>> x
== test/foo/bar/ping
>> x: at x 3
== bar/ping
>> x
== bar/ping
>> append x 'tau
== test/foo/bar/ping/tau
>> x
== bar/ping/tau
Would it be better for 'append and 'repend to return the 'series, instead of
the 'head of the 'series?
In other words, like:
Append: func [
{Appends a value to the tail of a series and returns the series head.}
Series [series! port!]
Value
/Only "Appends a block value as a block."
][
either only [
insert/only tail :Series :Value
] [
insert tail :Series :Value
]
:Series
]
(I've added in ":" to allow use of the above on Rebol/View with path! data
type for the series.)
After all, if one has set the position in a series explicitly, why should a
function return the series at a different position?
Andrew Martin
ICQ: 26227169 http://valley.150m.com/
[2/3] from: carl:cybercraft at: 10-Aug-2002 18:43
On 10-Aug-02, Andrew Martin wrote:
> Why do 'append and 'repend return the head of the series? (I'm using
> latest Rebol/Core)
[snip]
It's the same with current non-beta versions (to save others
checking:)
> After all, if one has set the position in a series explicitly, why
> should a function return the series at a different position?
It does seem slightly odd, though I guess it does have its uses. For
instance, if you want a series to keep its current index, then
just...
append series something
but if you'd like it to reset its index, then...
series: append series something
However, I'd think you'd more likely want to compare the results of an
append with something else than do that, in which case returning at
the index might be the more useful.
Maybe it's just faster to return at the head, and given that with most
uses of append you'd ignore what's returned, (he guessed), going for
speed here might've been the deciding factor.
--
Carl Read
[3/3] from: joel:neely:fedex at: 10-Aug-2002 9:09
Hi, Andrew,
Andrew Martin wrote:
> Why do 'append and 'repend return the head of the series?
>
...
> Would it be better for 'append and 'repend to return the 'series,
> instead of the 'head of the 'series?
>
...
I have a conjecture on that, but let me define my terms first, for
the sake of economy. (Forgive me in advance if I over-explain...)
Functional languages encourage one to think in terms of the value
an expression yields upon evaluation.
Imperative languages encourage one to think in terms of changes to
the state of the system (computer, data, I/O, etc.) that result
from executing commands.
REBOL does both. I have concluded that I must think of *both* the
value
of an expression and its "effect" to understand it fully.
(I say "effect" instead of "side effect" because the latter term
is used by functionally-minded cultures in a pejorative sense.)
With those background ideas...
The effect of APPEND, REPEND, INSERT, etc. is to mutate the
sequence of values underlying a SERIES! argument. We can use
these functions (as with many aspects of REBOL) in a very
imperative-like style. However, REBOL presents itself as an
Expression-Based language, which encourages us to use the value
resulting from an evaluation in a larger expression. This means
(for example) that I can "chain" a group of evaluations together
as in
append append append "fee" ", fie" ", foe" ", fum"
where the result of the rightmost APPEND is suitable as an
argument to the pending one to its left, and so on. In fact,
for this kind of use, APPEND could have returned a series
referencing *any* position within the sequence, as the pending
APPEND still goes to the end.
OTOH, if I wish to chain INSERT expressions, as in
insert insert insert next find "ducks:" "Huey" "Dewey" "Louie"
it is important for the rightmost INSERT to return a series that
is positioned immediately past the newly-inserted value, so that
successive INSERTions deposit values in the same order that we
can see from the original expression.
In short, the value (independently of the effect) appears to be
chosen with a view to suitability for what might be useful within
a larger expression. One might imagine that a natural thing to
do following an APPEND would be to do another, or to check the
length of the accumulating collection, in which case the total
sequence is relevant. Just a guess.
> After all, if one has set the position in a series explicitly,
> why should a function return the series at a different position?
>
Let's distinguish between "value" and "effect" again. In the
case of:
foo: somepositionwithin someseries
append foo bletch
or (more expressionally):
append foo: somepositionwithin someseries bletch
the effect is what we would expect and the value is irrelevant.
Our expression makes it clear that FOO still refers to the same
position after the APPEND has done its work.
If OTOH we had written
foo: somepositionwithin someseries
foo: append foo bletch
or (more expressionally):
foo: append somepositionwithin someseries bletch
then our expression makes it clear that we want FOO to be set
based on the *value* of APPEND *after* the *effect* of APPEND
has been accomplished.
So, REBOL lets us have our cake and eat it too (but we have to
choose on a bite-by-bite basis! ;-)
Just my $0.02 worth of conjecture...
-jn-
--
; Joel Neely joeldotneelyatfedexdotcom
REBOL [] do [ do func [s] [ foreach [a b] s [prin b] ] sort/skip
do function [s] [t] [ t: "" foreach [a b] s [repend t [b a]] t ] {
| e s m!zauafBpcvekexEohthjJakwLrngohOqrlryRnsctdtiub} 2 ]