Jump to content




Post code tricks here!


44 replies to this topic

#21 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 27 March 2013 - 07:07 AM

View PostBubba, on 25 March 2013 - 09:50 PM, said:

Edit: Oh and also this thread has most of the lua tricks that I find useful, although metatables are missing.
I would add metatables, but I feel that they aren't really just atrick, but there are many tricks associated with them. Metatables is basically a sole reason one would use lua, and just calling it a trick doesn't serve it justice. At least a mention is deserved, however.

EDIT: I updated the topic in light of this.

#22 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 27 March 2013 - 01:11 PM

View PostremiX, on 27 March 2013 - 12:39 AM, said:

View PostBubba, on 25 March 2013 - 09:50 PM, said:

I think one of my favorite Lua idioms is the following bit of code:
for item=1,15 do
  local condition = item<=10 and "Less than or equal to 10" or item==11 and "Equal to 11" or "Greater than 11"
  print(condition)
end

I've only ever seen condition statements like this in Java, and even there it's not as simple or powerful as what Lua has (unless I'm missing something pretty huge from Java).

Edit: Oh and also this thread has most of the lua tricks that I find useful, although metatables are missing.

PHP way:
<?php
	for ($i=1; $i <= 15; $i++) {
   		echo $i.' - '.($i < 11 ? 'Less than or equal to 10' : ($i == 11 ? 'Equal to 11' : 'Greater than 11')).'<br>';
	}
?>
For as syntax goes Java and PHP are sort of similair. But what does the dollar sign mean? That is te only thing I dont know.
For example in java:
public class z{
 public static void main(String[] args){
for( int i; i <= 15; i++){
 System.out.println( i == 11? 'blabla' : 'other blabla' )
}
}
}

for the record, I dont know anything about PHP

#23 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 27 March 2013 - 01:13 PM

The dollar sign is just notation for variables. (Terrible notation at that.)

#24 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 27 March 2013 - 01:16 PM

View PostKingdaro, on 27 March 2013 - 01:13 PM, said:

The dollar sign is just notation for variables. (Terrible notation at that.)
Its actually quite smart to it that way. I mean you can easily see what is a variable and what not. But its kinda bad too, because you have to type it over and over again:p

Tip: ctrl+c and ctrl+v are very handy!:P

#25 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 27 March 2013 - 01:26 PM

It's incredibly sad that I have to constantly keep a dollar sign in my clipboard just to code in PHP.

#26 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 27 March 2013 - 05:32 PM

Here is another code trick. you know how sometimes we want only a particular variable from a function return. Ok for example lets say we want the distance between computers and we are using 'modem_message' event. I don't know why we want the distance, lets just pretend that we do.

ok so normally you would do this
local event, receiveChannel, replyChannel, message, distance = os.pullEvent('modem_message')

but there are a few variables that are sitting there that might confuse us later. so maybe we would do this
local _, _, _, _, distance = os.pullEvent('modem_message')

that looks a little better, but why not get just the distance variable... well we all know we can put it into a table and do this
local event = { os.pullEvent('modem_message') }
local distance = event[5]

but why not just put that in one line? like so
local distance = ( { os.pullEvent('modem_message') } )[5]

this probably isn't really the best way to do things, its very expensive, as we are making a table getting one value, then destroying the table again. But I'm not telling you about this for efficiency, I'm telling you this as a "you can do this"

now we are getting only one variable from function returns. another usage example might be that you are wanting to reset the cursor to the front of the current line
-- this is how you would normally do it
local currentX, currentY = term.getCursorPos()
term.setCursorPos(1, currentY)

-- this is how you could do it with the method described above.
term.setCursorPos(1, ({term.getCursorPos()})[2])

...

Another trick related to the last code snippet.
term.setCursorPos( term.getCursorPos(), 4 )

this will set the cursor x pos to the first value returned by getCursorPos and ignore the other return values from that function.

#27 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 27 March 2013 - 05:37 PM

View PostKingdaro, on 27 March 2013 - 01:26 PM, said:

It's incredibly sad that I have to constantly keep a dollar sign in my clipboard just to code in PHP.

I think it's a good way of showing what's a variable.
It's not hard to hold shift and click 4 :D
Unless you got some whack keyboard, lol

#28 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 27 March 2013 - 05:43 PM

It's difficult for me, because if I repetitively hit a key and shift/alt/ctrl at the same time, my hand tenses up and I get pains for like an hour.

Even if that weren't the case, there's a difference between hard and annoying / completely unnecessary, and it's just plain ugly. Why do I need some weirdass symbol to tell me what's a variable or not?

someVar

I'm pretty sure that's a variable.

"bleh" 42 doStuff()

I'm pretty sure this is a string, a number, and a function call.

Context clues exist, PHP.

#29 PonyKuu

  • Members
  • 215 posts

Posted 27 March 2013 - 08:23 PM

TheOriginalBit, I have a question about your assert.

Why do you use + 1 in your error call? If I want to call it with level 2 error, it acts as level 3 error, isn't it?

#30 PonyKuu

  • Members
  • 215 posts

Posted 27 March 2013 - 08:30 PM

Ah, nevermind, I derped it out.

#31 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 27 March 2013 - 08:37 PM

View PostPonyKuu, on 27 March 2013 - 08:23 PM, said:

TheOriginalBit, I have a question about your assert.

Why do you use + 1 in your error call? If I want to call it with level 2 error, it acts as level 3 error, isn't it?

View PostPonyKuu, on 27 March 2013 - 08:30 PM, said:

Ah, nevermind, I derped it out.
Yeh its just because the assert is a function call itself, so you need to get out of assert and then go to the level they expected which requires a + 1.

#32 PonyKuu

  • Members
  • 215 posts

Posted 27 March 2013 - 08:40 PM

Yeah, thank you. That would be helpful for my APIs, since sometimes bughunting becomes a pain in the butt

#33 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 27 March 2013 - 09:27 PM

Ok so another trick that a lot of people sadly do not know
if term.isColor and term.isColor() then
  -- this is a computer from cc1.4+ and is an advanced computer and you can change to any colour you wish.
elseif term.isColor then
  -- this is a computer from cc1.4+ and is a non-advanced computer or TURTLE and you can change between black and white
else
  -- this is a computer before cc1.4 and it means that colours are not supported
end


#34 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 28 March 2013 - 10:27 AM

There's not much point to using CC before 1.4 anyway :lol:

#35 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 28 March 2013 - 10:38 AM

View PostKingdaro, on 28 March 2013 - 10:27 AM, said:

There's not much point to using CC before 1.4 anyway :lol:/>
*cough* tekkit *cough*
Actually why play people nowadays still tekkit (im good in english 0.0), especially with the launch of the ftb launcher. I mean they are updated and not illegal..
But as far as coding tricks go, I like to do this with os.pullEvent()
local evt = {os.pullEvent()}
if evt[1] == 'your desired event' then
   -- make the ifs for that event here
elseif evt[2] == 'another event' then
 -- etc.

it keeps things organised, at least dor me :P

#36 faubiguy

  • Members
  • 213 posts

Posted 28 March 2013 - 10:46 AM

Wouldn't evt[2] be the first parameter of whichever event was pulled?

#37 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 28 March 2013 - 10:48 AM

View PostEngineer, on 28 March 2013 - 10:38 AM, said:

*cough* tekkit *cough*
it keeps things organised, at least dor me :P/>
Tekkit Lite is updated with (almost) the latest version, IIRC. Tekkit Classic is for the people who can't let go of their overpowered EE trinkets.

#38 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 28 March 2013 - 10:54 AM

View Postfaubiguy, on 28 March 2013 - 10:46 AM, said:

Wouldn't evt[2] be the first parameter of whichever event was pulled?

Indeed it would be.

#39 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 02 April 2013 - 05:36 AM

Ok so I am posting with an update to the custom assert function I posted the other day.

After reading some of the code made by the CC devs, I have discovered that if you do a level of 0 on the error function... example
error('Some Error', 0)
it will not print the error message with the file name and line number. It is essentially
printError( 'Some Error' )
error()

this is a very handy trick to quickly exit the program, print a red message, but now show it as an error in the code. so as such I have updated the assert function... here it is:
_G.assert = function(condition, errMsg, level)
  level = tonumber(level) or 1
  if not condition then
	error(errMsg, (level == 0 and 0 or level + 1))
  end
  return condition
end


#40 Spongy141

  • Members
  • 526 posts
  • Location'Merica

Posted 02 April 2013 - 09:36 AM

My favorite Lua trick is this
local var = read()
print(var)
yep, that is my favorite trick, got a problem?





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users