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

[Forum] Code Snippet Section

Started by kind-sir, 26 December 2013 - 12:02 PM
kind-sir #1
Posted 26 December 2013 - 01:02 PM
Heya ComputerCrafters,

There seems to be an odd predicament when choosing a section to post code in.
Code that functions as a program is posted in the "Programs" section, and a class or API would go into the "API" section

However, what about code that has no particular use in any of the fore-mentioned sections, such as small lines or functions that would aid in a bigger program (a snippet)? A section with such a function would benefit not only new programs, but also coders who want an easy repository to find a piece of code they have been struggling with.

A couple of examples would be:
"Find What a Peripheral Can Do"
http://www.computerc...ipheral-can-do/
local tArgs = {...}
per = tArgs[1]
for i,v in ipairs(peripheral.getMethods(per))
  do print(i..". "..v)
end
Which was then turned into:
for i,v in pairs(peripheral.getMethods(...)) do print (i..". "..v) end
And finally:
textutils.tabulate(peripheral.getMethods(...))


"Advanced Computer Detection"
From:
local advancedComputer = false
if term.isColor then
  advancedComputer = true
end
To:
local advancedComputer = term.isColor and true or false


With simple snippets that could be further broken down into efficient lines of code would surely benefit any coder.


If this has been suggested, it must have missed my search results.

Thank you for your time,
Kind regards,
Kind-Sir
Edited on 26 December 2013 - 12:04 PM
Lyqyd #2
Posted 26 December 2013 - 01:15 PM
Moved to Forum Discussion.
kind-sir #3
Posted 26 December 2013 - 01:25 PM
My apologies Lyqyd, it appears that there IS a snippet section suggestion in the Forum Discussion category:
http://www.computercraft.info/forums2/index.php?/topic/12937-code-snippets-sub-forum/page__p__119144__hl__snippet__fromsearch__1#entry119144

Feel free to lock this thread if it is seen as redundant or unneeded.
oeed #4
Posted 26 December 2013 - 04:53 PM
This is more of a single post (probably in General) that an entire sub-forum. You may also want to check out GravityScore's Sublime Text 2 CC syntax that contains lots of snippets.

http://www.computercraft.info/forums2/index.php?/topic/10577-cc-syntax-highlightingcode-completions-v12-sublime-text-2/page__p__88787#entry88787
theoriginalbit #5
Posted 26 December 2013 - 08:09 PM
"Advanced Computer Detection"
From:
local advancedComputer = false
if term.isColor then
  advancedComputer = true
end
To:
local advancedComputer = term.isColor and true or false
Not quite right, that just checks wither you're able to see if it is an advanced computer, if anything that would be a check for ComputerCraft 1.45 or higher.

You could also further simplify that statement to
local advancedComputer = term.isColor ~= nil
but again, that is not quite right, it just checks for presence of the function, what you'd actually want to do is the following
local advancedComputer = term.isColor and term.isColor()
this will first make sure the computer is one from CC 1.45 or higher, because it checks for presence of the function, it then invokes the function and checks whether the computer is advanced.

Did You Know: Since ComputerCraft 1.45 normal computers have also been able to change the text and background colours between black and white.
aaa #6
Posted 21 January 2014 - 11:12 PM
Could not there be a snippet tag?
awsmazinggenius #7
Posted 21 January 2014 - 11:20 PM
I personally believe that snippets should have a megathread. Besides, if there was a whole section for them, we'd get things like this from new users (no offense, and some new users actually do their searching and are actually quite talented programmers):
Clear Terminal And Set Cursor Pos To 1, 1

function clearTerm()
  term.clear()
  term.setCursorPos(1, 1)
end
Henness #8
Posted 22 January 2014 - 12:28 AM
Simple Environment Proof "turtle.forward()"

local function forward()
 while not turtle.forward() do
  if turtle.detect() then
   if not turtle.dig() then
    return false
   end
  else
   turtle.attack()
  end
 end
 return true
end

Personally I like the snippet idea :)/>
Edited on 21 January 2014 - 11:36 PM
oeed #9
Posted 22 January 2014 - 03:45 AM
I personally believe that snippets should have a megathread. Besides, if there was a whole section for them, we'd get things like this from new users (no offense, and some new users actually do their searching and are actually quite talented programmers):
Clear Terminal And Set Cursor Pos To 1, 1

function clearTerm()
  term.clear()
  term.setCursorPos(1, 1)
end

That is exactly what I'm about to do :P/>
aaa #10
Posted 22 January 2014 - 10:39 AM
type metamethod
returns the field __type of the metaTable if there is one, the default type otherwise. (eg, I add "vector" in the field __type of the vectors metatable)

from

metaType = function(t)
  if type(t) == "table" then
    local m = getmetatable(t)
    if m and m.__type then
      return m.__type
    end
  end
  return type(t)
end

to

local rawMetaType = function(t)
  return getmetatable(t).__type or error()
end
metaType = function(t)
  local b,s = pcall(rawMetaType,t)
  return b and s or type(t)
end

It spare only two lines, but gives an example of use of pcall replacing conditions matching.
Edited on 22 January 2014 - 09:39 AM