World: r3wp
[Core] Discuss core issues
older newer | first last |
BrianH 23-Mar-2008 [9569] | How is it different from replace/all ? |
[unknown: 5] 23-Mar-2008 [9570x2] | b: [[1] [[[1]]] [1]] |
how do you replace/all the 1's in that with 2's? | |
BrianH 23-Mar-2008 [9572] | I'd probably use parse, or Gabriele's rewrite function. How did you do it? |
[unknown: 5] 23-Mar-2008 [9573] | Mine is strictly a replace/all function it is much more tasking on the system but if made native could probably be cool |
BrianH 23-Mar-2008 [9574] | Will you only be replacing literal values, or doing general pattern replacement? |
[unknown: 5] 23-Mar-2008 [9575] | any occurrence even in embedded series |
BrianH 23-Mar-2008 [9576] | I got that :) I was wondering what kind of things you were searching for, to be replaced. Just literal values? Blocks? |
[unknown: 5] 23-Mar-2008 [9577] | practically anything |
BrianH 23-Mar-2008 [9578] | Interesting. How do you check for equality? Do you go by reference or structural equivalence? |
[unknown: 5] 23-Mar-2008 [9579x3] | well that is where it gets a little of a concern. Currently, I only check for equal? |
>> b: [[1] [[[1]]] [1]] == [[1] [[[1]]] [1]] >> replace-all b [1] [2] == [2] >> b == [[2] [[[2]]] [2]] | |
Can also do this: >> b: [[1] [[[1]]] [1]] == [[1] [[[1]]] [1]] >> replace-all b 1 2 == 2 >> b == [[2] [[[2]]] [2]] | |
BrianH 23-Mar-2008 [9582] | That is structural equivalence. Nice. |
[unknown: 5] 23-Mar-2008 [9583x2] | >> b: [[1] [[[1]]] [1]] == [[1] [[[1]]] [1]] >> replace-all [1] "1" ** Script Error: replace-all is missing its newval argument ** Near: replace-all [1] "1" >> replace-all b [1] "1" == "1" >> b == ["1" [["1"]] "1"] |
It works very well | |
BrianH 23-Mar-2008 [9585x2] | I would call it replace-deep, but cool. |
Is it recursive? | |
[unknown: 5] 23-Mar-2008 [9587x9] | yes |
Doesn't take much | |
replace-all: func [series oldval newval /local sd][ sub-ic?: func [sd][ forall sd [ either equal? first sd oldval [ poke sd 1 newval ][ if series? first sd [sub-ic? first sd] ] ] ] sub-ic? series ] | |
was written for a particular use I had but not for general use. It will have some limitations to be a mezzanine | |
It was built rapidly off of another function that I built in TRETBASE. | |
If you see a series it doesn't work on let me know. | |
I see some errors in it already in the code that I should patch real quick | |
Here you go: | |
replace-all: func [series oldval newval /local sub-ic][ sub-ic: func [sd][ forall sd [ either equal? first sd oldval [ poke sd 1 newval ][ if series? first sd [sub-ic first sd] ] ] ] sub-ic series ] | |
BrianH 23-Mar-2008 [9596x2] | Not bad. You use an inner function for the recursion, which should allow you to go to greater recursion depth before running out of stack space. I'd change the series? to any-block?, and the EITHER IF to a CASE, and the FORALL to a WHILE, and add type specs to the outer function, and change /local sd to /local sub-ic?, but otherwise good stuff. |
Sorry, I wrote most of that before you posted your fixed version. | |
[unknown: 5] 23-Mar-2008 [9598x2] | There you BrianH.. You just thought of ways to improve it. I just whipped it up when we were discussing it from another function built for a different purpose. |
I like your recommendations also. Maybe I will change the ip-check? function I have in TRETBASE which this is based on. | |
BrianH 23-Mar-2008 [9600] | Be sure to change your oldval and newval references to get-words for safety. |
[unknown: 5] 23-Mar-2008 [9601x2] | Not sure I follow you. |
Oh so it doesn't evaluate them you mean. | |
BrianH 23-Mar-2008 [9603] | either equal? first sd :oldval [ poke sd 1 :newval |
[unknown: 5] 23-Mar-2008 [9604x6] | Yeah good point |
Did i spark your interest for changing the replace function? | |
If so I suggest you keep the replace function and make this an entirely different replace function. Call it replace-deep if you like. | |
Then just cut out out the /all from your current replace function and leave as is. | |
The reason is that I believe this will be more tasking on the stats/evals then replace is by far. | |
By the way that recursion method with the function inside the function is the only way I found to get round most of those stack errors you mentioned. | |
BrianH 23-Mar-2008 [9610] | Have you seen the replace function? We are getting to the point of diminishing returns on new features - too much complexity overhead. As it is, we are going to have to use APPLY in the R3 version just to add another option. |
[unknown: 5] 23-Mar-2008 [9611] | No I haven't really paid much attention to it. i know there was some discussion on the 2.7.6 discussions but i haven't followed R3 development much at all. |
BrianH 23-Mar-2008 [9612] | Do source replace - it's a good way to learn some interesting optimization techniques. |
[unknown: 5] 23-Mar-2008 [9613] | is it the same in 2.7.6 as it is in 2.7.5? I'm currently using 2.7.5. |
BrianH 23-Mar-2008 [9614] | No, it really isn't. The 2.7.6 REPLACE was one of the backports, as was EXTRACT. |
[unknown: 5] 23-Mar-2008 [9615x2] | Extract is in 2.7.5 also which is the one I was using. Maybe that is why we are on different pages on that issue. |
I'll check them out later. | |
BrianH 23-Mar-2008 [9617] | I didn't realize that you weren't using 2.7.6. I wrote REPLACE and EXTRACT in that release. |
[unknown: 5] 23-Mar-2008 [9618] | oh - lol. |
older newer | first last |