r3wp [groups: 83 posts: 189283]
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r3wp

[Core] Discuss core issues

Geomol
2-Jul-2010
[17181]
As a side note, I noticed, TAIL? and EMPTY? is the same. So you can 
also write the above as:

and series? s empty? s


Is it wise, that TAIL? and EMPTY? does the same? Maybe EMPTY? should 
return false, if the series holds elements, even if you're at the 
tail.
BrianH
2-Jul-2010
[17182x8]
AND is an op, thus infix, and doesn't have shortcut evaluation. This 
means that you should use AND~ instead of AND if you want prefix, 
but it will still fail if s is a datatype that is not compatible 
with EMPTY? because EMPTY? s will still be called even if SERIES? 
s is false.
Henrik, can you post some code that will make a series reference 
that is past the end of a series? I'm having trouble making one that 
works consistently, and neither your INDEX? method or EMPTY? will 
work. REBGL keeps adjusting the index returned by INDEX?.
>> b: next a: "a"
== ""
>> index? b
== 2
>> remove a
== ""
>> index? b
== 1  ; adjusted
>> insert a "1"
== ""
>> index? b
== 2 ; and back again
In R3 they stay consistent, and there is an action PAST? to tell 
whether an index is past the end.
>> b: next a: "a"
== ""
>> remove a
== ""
>> index? b
== 2  ; not adjusted
>> past? b
== true


In R2 I don't even see how to write this in mezzanine without temporarily 
appending something on the end of the series (a single element will 
do) and seeing if the index adjusts, then removing what you appended. 
Like this:
>> b: next next next a: "abc"
== ""
>> clear a
== ""
>> also (index? b) < (append b "1" index? b) clear back tail b
== true  ; past end
>> b: ""
== ""
>> also (index? b) < (append b "1" index? b) clear back tail b

== false  ; not past end, but Henrik's method, and EMPTY? will both 
return true.
Still, the temporarily appending method would be sufficient to backport 
a PAST? workaround, so I'll write one for R2/Forward.
Here's the source to the backport:
past?: func [
	"Returns TRUE if a series index is past its tail."
	series [series!] ; gob! port!
][
	also (index? :series) < (insert tail :series "1" index? :series)
		clear back tail :series  ; Undo the modification
]

; Note: Native in R3, and non-modifying. No ports because you can't 
undo the

; insert. INDEX? doesn't stay consistent with past-tail references 
in R2.
Submitted to R2/Forward.
Thanks, I had forgotten PAST? :)
Henrik
2-Jul-2010
[17190x2]
I'm not sure I would want the series to be modified as the case where 
PAST? is true could be for an indication of the series in a debug 
UI, rather than the actual use of the series. Interesting that you 
had so much trouble producing the problem.
ah, you had trouble, because you were removing an element before 
the INDEX?, not after the INDEX?.

>> b: "abc"
== "abc"
>> b: next next b
== "c"
>> clear back b ; does not modify INDEX?
== ""
>> b
== ** Script Error: Out of range or past end
** Near: mold b
>> index? b
== 2 ; works every time
BrianH
2-Jul-2010
[17192]
You are also removing an element before the index, but not from the 
beginning. Apparently clearing from somewhere other then the beginning 
of the series breaks the auto-adjusting (which shouldn't be happening 
anyways). PAST? works the same either way.
Ladislav
3-Jul-2010
[17193x10]
It was unfortunate to adjust the result of INDEX? function, since 
we actually obtain a wrong value this way. Auto-adjusting is not 
broken in the case Henrik posted.
>> b: "abc"
== "abc"
>> b: next next b
== "c"
>> index? b
== 3
>> clear back b
== ""
>> index? b
== 2
Nevertheless, as said above, I consider the auto-adjustment idea 
"broken".
past?: func [
	"Returns TRUE if a series index is past its tail."
	series [series! gob! port!]
][
	(index? :series) = (index? back :series)
]

; Note: INDEX? doesn't stay consistent with past-tail references 
in R2.
correction:

past?: func [
	"Returns TRUE if a series index is past its tail."
	series [series! gob! port!]
][
	and~ not same? head? :series :series
		(index? :series) = (index? back :series)
]

; Note: INDEX? doesn't stay consistent with past-tail references 
in R2.
Yet another variant:

past?: func [
	"Returns TRUE if a series index is past its tail."
	series [series! gob! port!]
][
	and~ (index? :series) = (length? head :series)
		not same? tail? :series :series
]

; Note: INDEX? doesn't stay consistent with past-tail references 
in R2.
past?: func [
	"Returns TRUE if a series index is past its tail."
	series [series! gob! port!]
][
	and~ (index? :series) = (length? head :series)
		not same? tail :series :series
]

; Note: INDEX? doesn't stay consistent with past-tail references 
in R2.
(the last is a correction of the one preceding it)
Simplification:

past?: func [
	"Returns TRUE if a series index is past its tail."
	series [series! gob! port!]
][
	not same? :series skip :series 0
]

; Note: INDEX? doesn't stay consistent with past-tail references 
in R2.
Note: I am not sure, whether this would work with ports and gobs, 
though, since I do not know, whether SAME? behaves similarly for 
them
BrianH
3-Jul-2010
[17203x4]
Ladislav, it needs to work in the case of auto-adjusting indexes 
too.
Auto-adjusting is broken in the case that Henrik mentioned. As a 
separate issue, the case Henrik mentioned has *better* behavior, 
because the whole idea of auto-adjusting is a bad one, so it not 
working is an improvement.
But because of that, the PAST? function for R2 needs to work whether 
the index auto-adjusts or not. So before you submit another version, 
test it against past-tail references with auto-adjusting indexes. 
If it works then, it works.
To get a past-tail reference that auto-adjusts, clear from the head 
of the series rather than from further along.
Ladislav
3-Jul-2010
[17207x8]
why didn't you try? it works
every past-tail series auto-adjusts in ceratain interpreter versions 
(in other no past-tail series auto-adjusts), no exceptions
Henrik does not know any series that does not auto-adjust, it is 
just your error
(you just need to read above)
Note: all versions work just in (auto-adjusting) R2
I am mentioning that, since there were some versions of R2 that did 
not auto-adjust
(long time ago)
But, it is possible to write a modified version, which would work 
regardless of auto-adjustment
BrianH
3-Jul-2010
[17215]
In 2.7.7, indexes auto-adjust if you clear the series from the beginning, 
but not if you clear from later on. In the same interpreter.
Ladislav
3-Jul-2010
[17216x2]
Wrong, see above
You did not check Henrik's example as I did
BrianH
3-Jul-2010
[17218]
I did, but got different results than I am getting now.
Ladislav
3-Jul-2010
[17219]
Actually, you did not, it was just an error
BrianH
3-Jul-2010
[17220x2]
With the same code, no less, I am getting different resulta now.
I didn't post the the old results.
Ladislav
3-Jul-2010
[17222x3]
You are getting the same results you got before, it was just your 
error, resulting from the fact, that auto-adjustment adjusts past-tail 
series to tail indices, i.e. to 2 in case Henrik posted
Nevertheless, it is easy to write code that would work regardless 
of auto-adjustment
(which means, it would work in R2, old R2, and R3)
BrianH
3-Jul-2010
[17225x2]
Well, I don't know why the results are different from the same code, 
but your code works now, so if it's alright with you I'll update 
R2/Forward with it.
I'll just chock up the old results to a momentary glitch until they 
recur.
Ladislav
3-Jul-2010
[17227x3]
E.g. this modification works regardless of auto-adjustment: (i.e. 
even in R3)

past?: func [
	"Returns TRUE if a series is past its tail."
	series [series! gob! port!]
] [
	and~ greater-or-equal? index? :series index? tail :series
	    not same? :series tail :series
]
but, pick whichever version you like
BTW, "returns true if a series index is past its tail" mixes different 
datatypes - series index is integer, while tail is a series, so, 
my opinion is, that the shorter wording is less misleading
BrianH
3-Jul-2010
[17230]
The not same? :series skip :series 0 line works well enough, afaict. 
Do you have a case for which it doesn't work and the larger test 
is required?