World: r3wp
[Core] Discuss core issues
older newer | first last |
[unknown: 5] 23-Mar-2008 [9622] | Actually, that is exactly what I want |
BrianH 23-Mar-2008 [9623x2] | replace-all: func [series oldval newval /local sub-ic][ sub-ic: func [sd][ while [not tail? sd ][ case [ equal? first sd :oldval [poke sd 1 :newval] series? first sd [sub-ic first sd] ] sd: next sd ] ] sub-ic series series ] |
CASE is faster than 2 comparisons with EITHER or IF. | |
[unknown: 5] 23-Mar-2008 [9625] | I recall you saying that before I never really checked it out. |
BrianH 23-Mar-2008 [9626] | I profile code patterns to see which is faster. |
[unknown: 5] 23-Mar-2008 [9627x3] | I think your case is messed up |
I think I had that if for a reason but not sure why | |
in the case of mine both conditions will not be true whereas in your case arrangment both can can't they? | |
BrianH 23-Mar-2008 [9630] | Not unless you use case/all. Did you have /case as an option to your function at some point? |
[unknown: 5] 23-Mar-2008 [9631x2] | all yeah I always forget that. |
Keep thinking of like switch when I see case and I use case often. | |
BrianH 23-Mar-2008 [9633] | It's more like cond from Lisp. |
[unknown: 5] 23-Mar-2008 [9634] | yeah I never used another language other than REBOL so I wouldn't know. |
BrianH 23-Mar-2008 [9635] | Wow. I've never heard that before. |
[unknown: 5] 23-Mar-2008 [9636x3] | lol - I'm just a hobbiest with REBOL. |
I have no programming education. Had to self teach me this stuff. | |
That is why when you use a lot of programming terms I'm lost. | |
BrianH 23-Mar-2008 [9639] | I'm self-taught too, but after more than 20 years you pick up some stuff :) |
[unknown: 5] 23-Mar-2008 [9640x6] | I think that replace function is good Brian. I would push for that to be a mezzanine at this point. |
:-) | |
the reason I like it as series instead of any-block is for using it on long strings. | |
>> str: "this is just a test of replace-all" == "this is just a test of replace-all" >> replace-all str #"t" #"d" == "dhis is jusd a desd of replace-all" | |
The case did cut it down a bit on the evals: >> replace-all b 1 2 == [[2] [[[2]]] [2]] >> stats/evals == [209 100 42] | |
I would say that is one heck of a useful function | |
BrianH 23-Mar-2008 [9646] | I've been using timblk to profile, not stats. How useful have you found stats to be? |
[unknown: 5] 23-Mar-2008 [9647x4] | its pretty useful. Btiffin mentioned timblk also. I will have to look for it sometime. |
replace-all: func [ "Replaces all occurences of old value with new value" series "Series containing values to replace" oldval "Value to be replaced" newval "New value to replace old value" /local subf ][ subf: func [sd][ while [not tail? sd ][ case [ equal? first sd :oldval [poke sd 1 :newval] series? first sd [subf first sd] ] sd: next sd ] ] subf series series ] | |
same function just commented a bit and changed sub-ic to just subf meaning subfunction | |
catch throw-on-error? | |
BrianH 23-Mar-2008 [9651] | Add the [series!] type check to the series parameter of the outer function and you'll be set. |
[unknown: 5] 23-Mar-2008 [9652] | oh yeah |
BrianH 23-Mar-2008 [9653] | catch throw-on-error What do you mean? |
[unknown: 5] 23-Mar-2008 [9654x2] | Should it throw errors to a catch so that it doesn't expose the lower code? |
not sure how that would behave in this case | |
BrianH 23-Mar-2008 [9656] | You aren't doing anything in the inner code that could generate an error, so no need. |
[unknown: 5] 23-Mar-2008 [9657] | If were sure that all the values that it traverses are comptible then I agree. In fact I don't think we need one until it becomes known as a problem. |
BrianH 23-Mar-2008 [9658] | The only problem would be if your blocks have cyclic references and you get stack overflow. I'm not sure you can catch that. |
[unknown: 5] 23-Mar-2008 [9659] | replace-all: func [ "Replaces all occurences of old value with new value" series [series!] "Series containing values to replace" oldval [any-type!] "Value to be replaced" newval [any-type!] "New value to replace old value" /local subf ][ subf: func [sd][ while [not tail? sd ][ case [ equal? first sd :oldval [poke sd 1 :newval] series? first sd [subf first sd] ] sd: next sd ] ] subf series series ] |
BrianH 23-Mar-2008 [9660] | Don't put [any-type!]. The only difference between that and no type spec at all is that your function would be able to accept unset! values, and that would require other changes to your code to work properly. |
[unknown: 5] 23-Mar-2008 [9661x4] | here this is better to read: |
ahhh good point. | |
replace-all: func [ "Replaces all occurences of old value with new value" series [series!] "Series containing values to replace" old-value "Value to be replaced" new-value "New value to replace old value" /local subf ][ subf: func [sd][ while [not tail? sd ][ case [ equal? first sd :old-value [poke sd 1 :new-value] series? first sd [subf first sd] ] sd: next sd ] ] subf series series ] | |
I changed newval to new-value and oldval to old-value | |
BrianH 23-Mar-2008 [9665] | That is the REBOL way :) |
[unknown: 5] 23-Mar-2008 [9666] | hehe - looks good. |
BrianH 23-Mar-2008 [9667] | In general, I find that it is a good idea to let unset! values be erroneous, rather than changing your code to accept them. That makes it easier to make a clear distinction between non-values that are erroneous (unset!) and non-values that may not be erroneous (none!). |
[unknown: 5] 23-Mar-2008 [9668x4] | good tip |
Is it a good practice to unset series within functions if the series is an argument to the function? Not sure how the garbage handler in REBOL works or if it sees it as garbage until the next function call. | |
In other words if i pass a massive series of data as an argument to the function should I at the end of the function unset that argument? | |
I wish Carl would give us pointers on when to use unset function and how to optimize our use of the memory space and other performance tips. | |
older newer | first last |