World: r4wp
[Rebol School] REBOL School
older newer | first last |
PatrickP61 8-May-2013 [1943] | Hey all, I'm having such a good time learning again! I've got some code to generate a print ruler, but I think it could be cleaned up a lot more. If some of you have a quick moment, could you take a quick look and advise me on how to shorten this code. ruler1: copy ruler2: "" idx: 0 loop 110 [ idx: idx + 1 append ruler1 "_" append ruler2 last-digit: last to-string idx if last-digit = #"5" [ clear back tail ruler1 append ruler1 "+" ] if last-digit = #"0" [ either idx < 99 [clear back back tail ruler1] [clear back back back tail ruler1] append ruler1 to-string idx ] ] replace/all ruler2 "0" "_" print ruler1 print ruler2 ____+___10____+___20____+___30____+___40____+___50____+___60____+___70____+___80____+___90____+__100____+__110 123456789_123456789_123456789_123456789_123456789_ 123456789_123456789_ 123456789_123456789_ 123456789_123456789_ |
GrahamC 8-May-2013 [1944] | Try ++ idx instead of idx: idx + 1 |
PatrickP61 8-May-2013 [1945x2] | I don't like the BACK BACK ... etc, depening upon the length of IDX which is what I want to print out Can I do something like LENGTH? IDX and use that number to "back up the series"? |
I'll give ++ idx a try | |
GrahamC 8-May-2013 [1947x2] | copy/part tail form idx -1 will give you the last number |
shouldn't use mutually exclusive serial if statements | |
PatrickP61 8-May-2013 [1949] | You mean instead of LAST-DIGIT: LAST TO-STRING IDX |
GrahamC 8-May-2013 [1950] | yes |
PatrickP61 8-May-2013 [1951x2] | Should I use FIND instead? |
How can I use the LENGTH? of IDX as a way to CLEAR those last positions. ie IDX is 110, length is 3 then clear the last 3 characters from the ruler1 series and replace with to-string idx | |
GrahamC 8-May-2013 [1953] | try ? clear skip tail series -3 |
PatrickP61 8-May-2013 [1954] | Thanks Graham, I'm getting an error with ++ idx, help ++ indicates incrementing a 'word, but I'm not understaning it I'll try your suggestions |
GrahamC 8-May-2013 [1955] | >> idx: 1 == 1 >> ++ idx == 1 >> idx == 2 |
PatrickP61 8-May-2013 [1956] | :-D Oh my gosh, i couldn't get copy/part tail form idx - 1 working because I thought it was IDX subtract 1. Ooops, you meant -1 as part of copy/part!!!! |
Bo 8-May-2013 [1957x2] | >> help copy USAGE: COPY value /part range /deep So, copy/part takes two parameters: (1) the start index, and (2) the range If you rewrite the copy/part like I did below, it is much easier to see how it is split up: copy/part tail form idx ;parameter 1 -1 ;parameter 2 |
I'm not saying to use that format for writing your scripts, but as a visual aide, it may help. Also, you could make it kind of lisp-y like this: copy/part (tail form idx) (-1) | |
PatrickP61 8-May-2013 [1959] | Yes, thank you Bo, I like the lisp-y version!!! |
Bo 8-May-2013 [1960] | I sometimes do that when I am trying to debug a lot of nested statements, because if you use parens, Rebol will let you know that it didn't receive something it was expecting instead of just pulling the output from the next nested statement. |
PatrickP61 8-May-2013 [1961] | I've found that it is very easy to nest inside of statements, but hard to remember what goes where!!! I like your suggestion! :-D |
Bo 8-May-2013 [1962x2] | Actually, the first method I posted above is easier to understand for a lot of nested statements. Compare this real line of script from one of my programs. The way I normally write it: browse probe rejoin copy [http://www.respectech.com/log/show-invoice.cgi?user= username "&pass=" password "&submit=" replace/all client "&" "%26" "&invno=" invnum either amtdue [rejoin ["¬ice=1&amtdue=" to-decimal amtdue "&daysdue=" daysdue]][copy ""]] Lisp-y: browse (probe (rejoin (copy [http://www.respectech.com/log/show-invoice.cgi?user= username "&pass=" password "&submit=" (replace/all (client) ("&") ("%26")) "&invno=" invnum (either amtdue [rejoin ["¬ice=1&amtdue=" (to-decimal amtdue) "&daysdue=" daysdue]][copy ""])]))) Heirarchical (takes a lot more space, but is easier to follow -- however, doesn't have the parameter enforcement of parens): browse probe rejoin copy [ http://www.respectech.com/log/show-invoice.cgi?user= username "&pass=" password "&submit=" replace/all client "&" "%26" "&invno=" invnum either amtdue [ rejoin [ "¬ice=1&amtdue=" to-decimal amtdue "&daysdue=" daysdue ] ] [ copy "" ] ] |
I'm glad we don't have to use the Lisp method or the heirarchical method, but it is nice that we can, especially when we are starting out. I usually only use the heirarchical method when I am trying to understand and visualize someone else's really complex scripting. I'll use the Lisp method when I am getting an error that I have narrowed down to a small snippet of code, and I can't figure out why the error is occurring. I also throw in a lot of 'probe statements to see what's really going on. | |
Ladislav 9-May-2013 [1964x3] | Patrick, clear back tail ruler1 append ruler1 "+" actually is change back tail ruler1 #"+" |
Similarly, either idx < 99 [clear back back tail ruler1] [clear back back back tail ruler1] append ruler1 to-string idx can be written as change skip either idx < 99 [-2] [-3] to-string idx | |
err, I lost a part of the expression: change skip tail ruler1 either idx < 99 [-2] [-3] to-string idx | |
Gregg 9-May-2013 [1967x2] | Patrick, rather than building up the strings bit by bit, consider how you might do it if you think of them as a buffer you're modifying. e.g., start with something like this: ruler1: copy "" insert/dup ruler1 "_" 110 forskip ruler1 5 [change skip ruler1 -1 "+"] or this ruler1: head insert/dup copy "" "____+" 22 |
Sometimes things aren't clearer when you reduce them, but sometimes it can work well. | |
PatrickP61 9-May-2013 [1969x2] | Thank you Bo, Ladislav, and Gregg -- I'll use all your suggestions :-) |
Thanks to all that helped. Learning all the time. I think I have the code pretty tight now! A lot better than my first attempt. Final solution: ruler-len: 80 ruler-1: head insert/dup (copy "") "----+----." to-integer (ruler-len + 9 / 10) ruler-1: head clear (skip ruler-1 ruler-len) ; trim excess r1: skip head ruler-1 9 ; adv to pos 10 forskip r1 10 [ adj: ((length? idx: to-string (index? r1)) - 1) change skip r1 (-1 * adj) idx ] ruler-1: head r1 ; r1 is at tail ruler-2: head insert/dup (copy "") "123456789." to-integer (ruler-len + 9 / 10) ruler-2: head clear (skip ruler-2 ruler-len) ; trim excess print-ruler: does [print ruler-1 print ruler-2] | |
Ladislav 10-May-2013 [1971x3] | My notes: * it is absolutely unnecessary to set RULER-1 three times to the same value thus, I normally do: ruler-1: copy "" insert/dup ruler-1 "----+----." to-integer (ruler-len + 9 / 10) clear (skip ruler-1 ruler-len) ; trim excess .... ; for the same reason it is unnecessary to do: ; ruler-1: head r1 |
... and for the same reason I would do r1: skip ruler-1 9 ; HEAD unnecessary | |
The same note applies to RULER-2 | |
Bo 11-May-2013 [1974] | When Carl had me write my first Rebol script, his goal was to see if I understood how scripting in Rebol is different than most other languages. One of the helpful things he told me was that if you see patterns in your scripts, then you are likely not scripting as efficiently as possible. |
Henrik 11-May-2013 [1975] | Bo, that's a great quote. |
Reichart 12-May-2013 [1976] | LOL, I was just posting a rant on FB that can be summed up as "if you do it over and over again, make a BLOODY shortcut". |
PatrickP61 14-May-2013 [1977] | Ladislav, Excellent notes!!! For whatever reason, (old coding habits I guess) I started with defining the object RULER-1 and then manipulating it. I now see what you are saying about using the INSERT/DUP. As to notes about HEAD, I realize now that I confused INSERT and APPEND, (thinking it inserts at tail, when it doesn't. I welcome any and all comments to help me get out of my paradigm!! |
Ladislav 14-May-2013 [1978x5] | As to notes about HEAD, I realize now that I confused INSERT and APPEND - APPEND used in a similar way as above would not need HEAD either |
Neither INSERT nor APPEND modify the index attribute of their argument (the index attribute of series is immutable, in fact) | |
Having a series with index 1 (the head has this index), neither INSERT nor APPEND can change the index of the series. What they do is something else - they return a series with a different index. | |
To illustrate this further, let's consider a trivial example: a: 1 b: 2 add a b ; == 2 You can examine A and B now and see that the ADD function did not change A or B, but it returned 2 (another value). Similarly, INSERT does modify the argument series, but not its index, however it returns a series with a different index. | |
Is this clear? | |
Endo 14-May-2013 [1983] | Your example should be "== 3" I guess? |
DideC 15-May-2013 [1984x2] | :-) |
(best of us can be wrong sometimes. Just to LOL, no pun intended) | |
Pekr 15-May-2013 [1986x2] | >> add: :multiply >> a: 1 == 1 >> b: 2 == 2 >> add a b == 2 |
You can always find a solution :-) | |
Endo 15-May-2013 [1988] | /LAST refinement doesn't return the last match when used with /REVERSE refinement. Is that a known issue? >> FIND/reverse/last tail "aoboco" "o" == "o" >> FIND/reverse tail "aoboco" "o" == "o" >> FIND "aoboco" "o" == "oboco" >> FIND/last "aoboco" "o" == "o" |
MaxV 15-May-2013 [1989] | Hello, someone needs Sublime Text 2 editor syntax highlight file, see http://rebol.informe.com/forum/rebol-2-f8/sublime-text-2-t59.html Do you know where he may find it? |
DideC 15-May-2013 [1990] | find/reverse and find/last are somewhat identical but not from the same point of start. So you have to use one XOR the other. |
Gregg 15-May-2013 [1991] | Endo, what result do you expect for FIND/reverse/last tail "aoboco" "o" ? |
Ladislav 15-May-2013 [1992] | Your example should be == 3" I guess?" - actually, I had a: 1 and b: 1 originally, and I somehow "managed" to change B to 2 :-( |
older newer | first last |