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

World: r3wp

[!REBOL3-OLD1]

Steeve
14-Nov-2007
[5307x3]
I have always wondered what was the point of the function alter?
someone is using  it ?
*
btiffin
14-Nov-2007
[5310]
Steeve;  Sunanda uses it in his world class index builder SKIMP. 
 rebol.org  skimp.r  It's a heady read I might add.  After giving 
it a glance ask yourself if Sunanda cut his teeth on personal computers 
or Big Iron.  :)
Sunanda
15-Nov-2007
[5311]
'alter is short for  'alternate (ie alternate between two states).

It's a shorthand way to add an item to a series if it is not there, 
or remove it if it is.

I may create strange data structures, but I use 'alter all the time.
Anton
15-Nov-2007
[5312x3]
Steeve, I actually used it once the other day.
I think I used it like this:
	if not find block item [alter block item]
But it seems an inefficient way of doing things.
Henrik
15-Nov-2007
[5315]
I always found ALTER less than useful. Never used it.
Ingo
15-Nov-2007
[5316]
Hi Anton, did you 'source 'alter?

alter: func [

    {If a value is not found in a series, append it; otherwise, remove 
    it.} 
    series [series! port!] 
    value 
    /local temp
][

    either temp: find series value [remove temp] [append series value]
]

So, you'd better off with:

if not find block item [append block item]
Henrik
15-Nov-2007
[5317]
interesting
Ingo
15-Nov-2007
[5318]
But I think, that the return value of alter is less than usefull, 
There's no way of knowing, whether the value has been added or removed 
... other than doing a 'find on it afterwards ... ;-)
Henrik
15-Nov-2007
[5319]
true.
Anton
15-Nov-2007
[5320x2]
Sorry, I did not use the example I gave above. Actually, I used alter 
like this:
	alter opened-rows count
That was probably my first use of alter ever.
Ingo
15-Nov-2007
[5322]
Now, that seems more in line, with how it's meant to be used.
Steeve
15-Nov-2007
[5323]
a proposal of a refinement for alter:
alter/count 

increment a value instead of add/remove it, return the total count 
for this value.
>> alter/count [] 'a
== 1
>> alter/count [a 1] 'a
== 2
Oldes
16-Nov-2007
[5324x3]
Instead of ALTER functionality I use this quite a lot... but I'm 
not sure I would use funtion with refinement for this as I use it 
in loops where speed is important.
But if you need it, you can use something like that:
alter-count: func[
	block [any-block!]
	value [any-type!]
	/local temp count
][
	either temp: find/tail/skip block :value 2 [
		change temp count: temp/1 + 1
		count
	][
		append block reduce [:value 1]
		1
	]
]
(the name of the function should be probably different)
It cannot be part of ALTER function as ALTER can be used with any 
series. I'm not sure how you could strore the counts in bitsets or 
strings;-)
Steeve
16-Nov-2007
[5327]
one-liner version:
alter-count: func[
	block [any-block!]
	value [any-type!]
][

 pick change block: any [find/tail/skip block :value 2 insert tail 
 block :value] 1 + any [block/1 0] -1
]
Anton
19-Nov-2007
[5328]
More than ALTER, I would like
	append-if-necessary
	remove-if-necessary
(but probably with better, shorter names)
Steeve
19-Nov-2007
[5329x4]
huh ?
yeah! i posted my first modification on devbase
even if i have don"t access to alpha T_T
hum * i haven't an access
Steeve
20-Nov-2007
[5333x2]
i have some difficultes to construc devbase.r , missing icons in 
the title, any idea ?
sorry , i got no problem in fact (i just have to open my eyes)
Henrik
21-Nov-2007
[5335]
everything should download automatically
BrianH
21-Nov-2007
[5336x2]
These alter-count functions aren't taking R3 into account. Most of 
these key/value usage patterns will be handled by the map! type in 
R3.
; alter-count, just code since it's too simple for a function
key-counts/:key: 1 + all [key-counts/:key 0]
Whoops: all -> any
Steeve
21-Nov-2007
[5338]
not agree, how do add new key with your code ?
BrianH
21-Nov-2007
[5339x3]
Like that.
Doesn't work in R2, but this is the !REBOL3 group, isn't it? :)
The trick is that you can't remove keys, but if you assign none to 
the map at a given key the effect is the same.
Steeve
21-Nov-2007
[5342]
i see
BrianH
21-Nov-2007
[5343]
The general pattern in R3 is that none is the equivalent of missing 
data. Pick off the end of a series returns none too.
Steeve
21-Nov-2007
[5344x2]
more tolerant functions
throw less errors
BrianH
21-Nov-2007
[5346]
Recovering from a none is easier and more efficient than recovering 
from an error. Series bounds are just an implementation detail anyways, 
when you have series that can autoexpand.
PeterWood
21-Nov-2007
[5347]
Does that mean I won't be able to build the equivalent of an R2 hash! 
with map! in R3?


I use a hash! to build a unique list of words as it was far quicker 
than any other method I could come up with.
BrianH
21-Nov-2007
[5348]
Yeah, hash! has been replaced with map!, which is faster but more 
specialized. You could either use block!, assign true to the map! 
at the word key, or keep a word count.
sqlab
22-Nov-2007
[5349x3]
this works in R2

 key-counts/:key: 1 + any [select key-counts :key  do [repend key-counts 
  [:key 0] 0 ] ]
key-counts/:key: 1 + any [select key-counts :key do [repend key-counts 
:key 0] ]
forget the second
PeterWood
22-Nov-2007
[5352]
Thanks for the suggestions: I think that I'll have to use  a map! 
with a dummy value as using a block! was too slow.
Oldes
26-Nov-2007
[5353]
I'm missing hash! in R3 because with map! I cannot use integers as 
keys... I hope it will be changed.
BrianH
26-Nov-2007
[5354]
It's hard in hash! as well, but I agree. There is some missing infrastructure 
around the map! type as well, like iteration and searching.
Mchean
10-Dec-2007
[5355]
has the conversation moved elsewhere?
Pekr
11-Dec-2007
[5356]
no, it is just some kind of slow-down period. Activities around R3 
should be renewed as we speak, it seems Gabriele and Cyphre will 
be back on R3 full-time soon. There is also draft of release strategy. 
It seems RT decided (upon suggestions) to divide R3 release, so we 
should see Core like R3 alpha in one month, View will come later 
(2 - 3 months)