This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Bane_MC's profile picture

Smarter Turtles v0.1

Started by Bane_MC, 20 May 2013 - 03:57 PM
Bane_MC #1
Posted 20 May 2013 - 05:57 PM
Not sure that I want to ask a pro for anything specifically, but the programs section says we shouldn't post incomplete code and should post here instead, so…

Title: Smarter Turtles v0.1
Comment: Looks like there are a few of these going around. Here's my own contribution and, in my opinion, it's a little more ambitious. I'm coming from the FTB forums, so hopefully a post to their forums where I've already provided full details + some history will be alright. http://forum.feed-the-beast.com/threads/smart-turtles.21785/

If anything, my requests are these:
Pros: I'm a software engineer by trade, but most of my work is done in OOP (with a splash of javascript), and so functional programming is a different mindset for me. My goal is to replace the turtle API while maintaining backwards compatibility. I was looking for ways to replace the existing turtle API and stumbled across an example of accomplishing this with metatables, however the example only included a single function replacement… and so I'm not sure if my implementation is, perhaps, sub-par. I'm hoping that some pros could provide insight into anything I may have done incorrectly or inefficiently so that I can improve my codebase before taking this too much further.

Everyone else: What do you like about it? What do you hate?

Thanks
Lyqyd #2
Posted 21 May 2013 - 02:50 PM
Split into new topic.

This sounds interesting, I'll try to remember to check it out. :)/>
Bane_MC #3
Posted 23 May 2013 - 09:32 PM
Thank you, Lyqyd!

Dearly looking for some code review before I continue too much further on this. I'd rather not propagate any errors I may already be making.
Bane_MC #4
Posted 30 May 2013 - 10:33 PM
No one has anything to say on this? Nothing at all?

I would really appreciate some feedback on the code.
Yevano #5
Posted 30 May 2013 - 10:52 PM
Your code looks fine. If you're more comfortable with OOP, Lua can offer this through the use of tables. You ought to do some research on common ways of simulating classes in Lua. I think a good next step would be some sort short range path finding so the turtles don't get stuck on small impassable barriers.
ElvishJerricco #6
Posted 31 May 2013 - 12:58 AM
Project NewLife has an OOP implementation that's simultaneously sort of an example of functional programming. Like javascript, classes are declared by a function. It's interesting so you should check it out.
Yevano #7
Posted 31 May 2013 - 08:40 AM
I tend to use metatables since you can override metamethods for your objects more easily. I haven't looked at ElvishJerricco's implementation, but I'm guessing it uses function environments to access the instance methods for classes.
Bane_MC #8
Posted 02 June 2013 - 02:39 PM
Thanks, guys.

I'm more familiar with OOP, yes, but I'm fine with working in functional programming. Project NewLife looks fantastic, ElvishJericho, but I want to avoid including such a large dependency.

So, you see no issues in my current use of metatables? Am I using them correctly? Could it be done better?
Yevano #9
Posted 02 June 2013 - 02:58 PM
I think it's a bit weird that you are using metatables to store the original function and to provide the _call at the same time, but the logic seems sound. When I was talking about metatables, I was referring to their use in OOP. For example, you can have a class table, and when creating new objects you set the metable __index of the object to refer to the class table. This way, you only have to declare the class functions once and use them in all objects.
Symmetryc #10
Posted 02 June 2013 - 03:20 PM
I haven't really looked through it, but you seem to be using a lot of "if" statements to get simple tasks done. There is another way to replicate "if"-esque behavior, like this:

local output
local input = read()

-- Old way
if input=="Hello" then
	 output = "Hi"
end

-- New way
output = input=="Hello" and "Hi" or nil
Bane_MC #11
Posted 06 June 2013 - 09:18 PM
Thanks, Symmetric, but I'd rather go with legibility. if statement may be more verbose, but they're more understandable and don't rely on whacky hijinks.