|
Pages: [1]
|
 |
|
Author
|
Topic: ANY advice? (Read 738 times)
|
|
leke
|
Hope you all are having a good summer! right: 5 if any [(right = 1) (right = 2) (right = 3)] [print "Low"] if any [(right = 4) (right = 5) (right = 6)] [print "Medium"] if any [(right = 7) (right = 8) (right = 9)] [print "High"] Is there a shorter way I can write the expressions above - without having to repeat (right = 1) (right = 2) (right = 3) etc... Thanks, L.
|
|
|
|
|
Logged
|
|
|
|
|
Gabriele
|
 |
ANY advice?
« Reply #1 on: June 27, 2005, 03:36:29 AM » |
|
For intervals: if all [right >= 1 right <= 3] [...] Otherwise: if find [1 2 3] right [...] About your specific example, print pick ["Low" "Medium" "High"] round/ceiling right / 3 or print pick ["Low" "Low" "Low" "Medium" "Medium" "Medium" "High" "High" "High"] right
|
|
|
|
|
Logged
|
|
|
|
|
CarlRead
|
 |
ANY advice?
« Reply #2 on: June 27, 2005, 04:28:32 AM » |
|
I see Gabriele's added some aproaches while I've been working some out, but here's what I came up with, anyway... ; The parens are not needed, so this would be a faster version of your approach... if any [right = 1 right = 2 right = 3] [print "Low"] if any [right = 4 right = 5 right = 6] [print "Medium"] if any [right = 7 right = 8 right = 9] [print "High"]
; Or you could use SELECT... print select [ 1 "Low" 2 "Low" 3 "Low" 4 "Medium" 5 "Medium" 6 "Medium" 7 "High" 8 "High" 9 "High" ] right
; Or PICK and a bit of maths... print pick ["Low" "Medium" "High"] to-integer (right - 1 / 3) + 1 ; That assumes right is always an integer between 1 and 9. I tried to think of something that'd work with a block like this... [[1 2 3] "Low" [4 5 6] "Medium" [7 8 9] "High"] but couldn't come up with anything in the allotted time. ;-) But maybe that gives you an idea of how to think about such a problem, in that it starts out without any duplication of the data you're working with. And we're having a lousy summer, though that's probably because it's mid-winter in New Zealand at the moment. ;-)
|
|
|
|
|
Logged
|
- Carl Read
|
|
|
|
leke
|
 |
ANY advice?
« Reply #3 on: June 28, 2005, 02:35:29 AM » |
|
Thanks B)
|
|
|
|
|
Logged
|
|
|
|
Brock
Guest
|
 |
ANY advice?
« Reply #4 on: August 03, 2005, 10:31:21 AM » |
|
Carl, this seems to work... >> data: [[1 2 3] "Low" [4 5 6] "Medium" [7 8 9] "High"]>> test: 5 == 5 >> foreach [d r] data [if find d test [print r]] Medium
|
|
|
|
|
Logged
|
|
|
|
Guest
Guest
|
 |
ANY advice?
« Reply #5 on: August 03, 2005, 10:34:47 AM » |
|
..Sorry, sloppy post....
Carl, this seems to work... >> data: [[1 2 3] "Low" [4 5 6] "Medium" [7 8 9] "High"] >> test: 5 == 5 >> foreach [d r] data [if find d test [print r]] Medium >> test: 9 == 9 >> foreach [d r] data [if find d test [print r]] High
|
|
|
|
|
Logged
|
|
|
|
|
CarlRead
|
 |
ANY advice?
« Reply #6 on: August 10, 2005, 10:48:21 PM » |
|
Hi Brock. Yes, that's a nice solution. And it got me thinking as well, so here's yet another approach... >> data: [1 2 3 "Low" 4 5 6 "Medium" 7 8 9 "High"] == [1 2 3 "Low" 4 5 6 "Medium" 7 8 9 "High"] >> test: 5 == 5 >> first find find data test string! == "Medium" >> test: 9 == 9 >> first find find data test string! == "High" Being able to search for datatypes as well as values offers yet more ways to do things.
|
|
|
|
|
Logged
|
- Carl Read
|
|
|
|
Brock
|
 |
ANY advice?
« Reply #7 on: August 11, 2005, 09:30:43 PM » |
|
Carl, first time I have ever seen a datatype used with ' find, I don't think I was even aware you could use types in this way. Nice!! Thanks for the post
|
|
|
|
|
Logged
|
|
|
|
|
|
Pages: [1]
|
|
|
 |
News: 01-09-08 Alpha version of REBOL 3 has been released!
2287 Posts in 593 Topics by 3728 Members
Latest Member: Gasviemb
|