ID:133219
 
I was actually about to post asking for this feature, but apparently it's already in the language. The in operator can be used in conjunction with the rarely-used to to determine if a value falls in a given range. For example:
mob/Login()
world << (5 in (5 to 10)) //Evaluates as true.
world << (1 in (5 to 10)) //Evaluates as false.
world << (11 in (5 to 10)) //Evaluates as false.
//Etc.


Audeuro says this has been brought up before, but I decided to post it anyways as I'm sure not many newer programmers know about it. I was quite surprised when I saw the code evaluated properly. <_<
I'm not as sure that fractional values like 5.2 would work the same way here, but it is interesting nonetheless. The "to" keyword is indeed pretty useful for those who know about it.

Lummox JR
In response to Lummox JR
I tested it with decimals and negative numbers, and it works properly. I just didn't show them as examples.
I wish there was somewhere I could've found out about the function of "to".

I'm still cloudy about the keyword, and never use it in any of my projects yet.
In response to Kaiochao
to can be used in for() loops, switch() statements, and apparently how Popisfizzy just showed.

mob/verb/countdown()
for(var/X in 1 to 10) //10 to 1 won't work because 1 is lower than 10
world << 10-X

mob/verb/number_switch(n as nun)
switch(n)
if(1 to 10) countdown() //>_>
if(0) world << "None"
if(27 to 33) world << "Multiples of three are awesome"
//I mean the upper and lower boundaries of the number, not every number it might be, for anyone wondering
In response to Kaiochao
it also works in loops
for(var/i=1 to 20) world<<i


Kaiochao wrote:
I wish there was somewhere I could've found out about the function of "to".

Check for a VB tutorial about it, that's where I know it from. Seems to have the same functionality in DM.

EDIT:
Jeff8500 wrote:
to can be used in for() loops, switch() statements, and apparently how Popisfizzy just showed.

Pfft, out-posting me by like 10 seconds =P
In response to Jeff8500
That's the most useful switch() statement I've ever seen.
It's "n as num," by the way. ;)

Thanks for the "info".
In response to Falacy
Falacy wrote:
it also works in loops
for(var/i=1 to 20) world<<i


Fun fact: If you're ever doing a 4K project, you can skip the space just before "to". I've used "1to 10" as a quick-and-dirty way of looping through valid directions in such games.

Lummox JR
In response to Lummox JR
Lummox JR wrote:
Fun fact: If you're ever doing a 4K project, you can skip the space just before "to". I've used "1to 10" as a quick-and-dirty way of looping through valid directions in such games.

I thought the 4k project was 4k lines o.O I haven't participated in any of them though so I dunno =P
In response to Falacy
4k lines would be a lot. The 4k and 8k contests are how many bytes the source (not the DMB or anything else) can be compressed into.

Oh, and Lummox, if you read this, that reminds me that none of your 4k sources (or anyone else's, I believe) can be downloaded anymore. They're all under the BYONDScape subscriber system, which is defunct and no longer given upon buying a membership.
In response to Falacy
Falacy wrote:
I thought the 4k project was 4k lines o.O I haven't participated in any of them though so I dunno =P

BYOND 4K challenges consist of creating a fully playable game with only 4K of code in your .dm file, but with unlimited resources and maps.

Usually the way I handle such projects is to store all my data in a list within map or savefile. That leaves most of the 4K free for algorithms.

Lummox JR
In response to Jeff8500
Jeff8500 wrote:
//10 to 1 won't work because 1 is lower than 10

Actually, thats incorrect. Alongside this syntax with 'to', the 'step' parameter was implemented. Observe:

for(var/i = 10 to 5 step -1) world << i
In response to Alathon
Please explain this "step", I never knew of it, nor have I ever seen it used!
In response to Jeff8500
Jeff8500 wrote:
Please explain this "step", I never knew of it, nor have I ever seen it used!

mob/verb/To(a as num, b as num)
if(b < a)
for(var/i = a to b step -1)
world << i
else
for(var/i = a to b step 2)
world << i


Result with 1 and 10 as arguments:

1,3,5,7,9

Result with 10 and 5 as arguments:

10,9,8,7,6,5
In response to Alathon
What other functions does 99.9% of dream makers not know?
Someone should make a demo containing all of these things no one else has heard of.
In response to Kaiochao
There's eval(), which is technically an incomplete feature that was never officially released. Search "eval()", Tom explained all about it in a post a while ago.
In response to Kaiochao
Kaiochao wrote:
What other functions does 99.9% of dream makers not know?
Someone should make a demo containing all of these things no one else has heard of.

You can also use a loop like this
for(var/i=1,i<=10,i+=1) world<<i

which it seems like majority of people know about, but its not documented anywhere that I know of.
In response to Falacy
That's under the for loop proc in the DM Reference.

var/i=1 is Init
i<=10 is Test
i+=1(or i++) is Inc.
In response to Kaiochao
Kaiochao wrote:
That's under the for loop proc in the DM Reference.


Ah, oh well. Try out other various things that are common in C++ and/or VB and they may work in DM.
Relatively to other things, this is pretty known and documented for the switch() statement where it can also be used. In other places it's just a shortcut instead of writing out a full proper range check or loop, and probably compiles the same, so it isn't as important.
Page: 1 2