Jump to content


Eric's Content

There have been 88 items by Eric (Search limited from 10-February 22)


By content type

See this member's


Sort by                Order  

#114972 A nice, small class implementation.

Posted by Eric on 04 May 2013 - 03:44 AM in APIs and Utilities

Nice and concise.

Here's my more complex version, inspired by how python handles classes and descriptors: https://github.com/eric-wieser/pylua

There are some examples there at the bottom of the page.



#100250 BasicUtils v1.5

Posted by Eric on 22 March 2013 - 12:35 PM in APIs and Utilities

But can you deepcopy while preserving references?

local t = {}
t.value = 1
t.self = t

local q = deepcopy(t)

assert(q.self == q)



#92914 Shorter if statement

Posted by Eric on 03 March 2013 - 11:55 AM in Ask a Pro

print(random_var == 5 and "Hello World!" or "Bye World!")



#91959 Block API v0.7

Posted by Eric on 28 February 2013 - 08:47 AM in APIs and Utilities

I think you're missing a "return bobble" in there somewhere



#91085 adding object API functions to object metatables

Posted by Eric on 25 February 2013 - 10:49 AM in Suggestions

View Posttesla1889, on 25 February 2013 - 09:47 AM, said:

View PostEric, on 25 February 2013 - 07:26 AM, said:

That's not possible for tables, since you end up overwriting the methods with your keys.
its the metatable, not the table itself
so yes. you could. but that wouldnt help you at all

Ok, take this example:
local openDoorFor = {
    Eric = true,
    Notch = true
}

if openDoorFor[somePlayerName] then
    -- ...
end

Ok, so what if `somePlayerName` is 'insert' or 'remove'. This is why raw tables cannot themselves have metatables.



#91034 [1.5] os.queueEvent("modem_message", ...) also fires rednet_message

Posted by Eric on 25 February 2013 - 07:38 AM in Bugs

You should never have to requeue events - use the parallel API instead, where all events are sent to each thread.



#91032 adding object API functions to object metatables

Posted by Eric on 25 February 2013 - 07:26 AM in Suggestions

That's not possible for tables, since you end up overwriting the methods with your keys.



#90988 [1.48] os.queueEvent/os.pullEvent fail for 8-bit strings

Posted by Eric on 25 February 2013 - 03:31 AM in Bugs

Found another related bug in the file system:
fs.makeDir('d')  -- empty directory
local h = fs.open('d/'..string.char(128), 'w')  -- odd filename
assert(h == nil) -- which doesn't get created - fair enough

-- So there should be no files in the directory, right?
local files = fs.list('d')
assert(#files == 1)  -- except there are

-- Can you guess what the name of the file is?
local file = files[1]
print(files[1]:byte(1, math.huge))
That outputs:
239
190
128



#90983 reading pointers

Posted by Eric on 25 February 2013 - 03:15 AM in Ask a Pro

View PostTheOriginalBIT, on 25 February 2013 - 01:59 AM, said:


More accurately, Events don't work with string.dump



#90980 [1.48] os.queueEvent/os.pullEvent fail for 8-bit strings

Posted by Eric on 25 February 2013 - 02:52 AM in Bugs

This is the root cause of this "suggestion": http://www.computerc...net-being-crap/

Test #1: check that :byte and .char work as expected:
for i = 0, 255 do
    local c = string.char(i)
    local j = c:byte()
    if j ~= i then
        print(("FAIL: Converting %d from int->char->int gave %d"):format(i, j))
    end
end
Passes

Test #2: Check that strings are preserved through events:
for i = 0, 255 do
    local c = string.char(i)
    os.queueEvent('test', c)
    local _, c2 = os.pullEvent('test')
    assert(#c == 1 and #c2 == 1) -- to prove these are single-byte strings

    local j = c2:byte()
    if c ~= c2 then
         print(("FAIL: Passing %d through an event gives %d"):format(i, j))
    end
end
Fails:
FAIL: Passing 128 through an event gives 239
FAIL: Passing 129 through an event gives 239
FAIL: Passing 130 through an event gives 239
...
FAIL: Passing 255 through an event gives 239

To summarize, either queueEvent or pullEvent fail to pass strings containing characters above character 127.

This bug is almost certainly present in 1.5 as well, although I haven't tested it.



#90953 reading pointers

Posted by Eric on 25 February 2013 - 01:43 AM in Ask a Pro

String.dump(f) will allow you to send simple functions which contain no upvalues.



#90949 1.5 Causes "shell:149 No such method isPresent"

Posted by Eric on 25 February 2013 - 01:21 AM in Bugs

Have you checked line 149 of lua/rom/programs/shell to see if there's an obvious error?



#90463 [New Lua Function] os.getVersion (yes I know there is os.version)

Posted by Eric on 23 February 2013 - 10:21 AM in Suggestions

Version identifiers are not decimals and should not be treated as such. Consider:
1.0
1.1
1.2
...
1.8
1.9
1.10
1.11
Those are in order of increasing version, but not numeric or lexographic order.

Keep it as a string, or switch to an array of integers.



#86642 Split a string in two variables

Posted by Eric on 12 February 2013 - 09:10 PM in Ask a Pro

CC's gmatch is in bios.lua (for reasons I don't fully understand), and falls back on :find (which is java-side)



#86641 need help with metatables

Posted by Eric on 12 February 2013 - 09:07 PM in Ask a Pro

Your meteta les are irrelevant here. What you're seeing is standard pass-by-reference behaviour for tables.

t = {}
s = {test = "test"}

table.insert(t, s)
s.test = "changed"

print(t[0].test)




#86077 Split a string in two variables

Posted by Eric on 10 February 2013 - 11:47 PM in Ask a Pro

local message, size, color = message:match('/(.+)-(.-)-(.-)')



#85970 Split a string in two variables

Posted by Eric on 10 February 2013 - 01:41 PM in Ask a Pro

local command, args = message:match('/(.-) (.+)')



#85929 The HTTP API should allow us to set headers.

Posted by Eric on 10 February 2013 - 11:23 AM in Suggestions

A related addition to the HTTP API would be to allow reading of response headers, most importantly status codes.

@raineth: Nice! (+1)



#85880 Extending Existing API's

Posted by Eric on 10 February 2013 - 07:53 AM in Ask a Pro

Your first method should work just fine.



#83219 [API][MiscPeripherals] Music API (Iron Note Block)

Posted by Eric on 03 February 2013 - 06:01 AM in APIs and Utilities

Pro-tip: instead of

a=peripheral.wrap("top")
b=peripheral.wrap("bottom")
c=peripheral.wrap("front")
d=peripheral.wrap("back")
e=peripheral.wrap("left")
f=peripheral.wrap("right")
if a then
  g=a
elseif b then
  g=b
elseif c then
  g=c
elseif d then
  g=d
elseif e then
  g=e
elseif f then
  g=f
else
  -- ...

consider using:

g = peripheral.wrap("top")
 or peripheral.wrap("bottom")
 or peripheral.wrap("front")
 or peripheral.wrap("back")
 or peripheral.wrap("left")
 or peripheral.wrap("right")

if not g then
   -- ...

Here's the same api, in half as many lines.



#80815 Creating a New Computercraft Function.

Posted by Eric on 27 January 2013 - 06:27 AM in Ask a Pro

View PostremiX, on 26 January 2013 - 10:52 AM, said:

    elseif type(lengthOn) ~= "string" then error("Bad argument #3, number expected - got " .. type(lengthOn))
	elseif type(lengthOff) ~= "string" then error("Bad argument #4, number expected - got " .. type(lengthOff))
	elseif type(nTimes) ~= "string" then error("Bad argument #5, number expected - got " .. type(nTimes))
NIce... So if you give it a number...

I would say avoid type checking where possible - it violates the principle of duck typing.



#80623 run functions of other files

Posted by Eric on 26 January 2013 - 12:44 PM in Ask a Pro

No, you actually wouldnt even get "attempt to perform arithmetic __div on nil and nil". That code produces a syntax error, not a runtime error.

Put "3/4" in a file and run it (the CC Lua shell doesn't show this behavior) and you'll see what I mean.



#80588 os.pullevent while thing?

Posted by Eric on 26 January 2013 - 10:52 AM in Ask a Pro

What do you mean by "let them use the computer regulary"?



#80558 run functions of other files

Posted by Eric on 26 January 2013 - 08:54 AM in Ask a Pro

View Postsjele, on 26 January 2013 - 08:48 AM, said:

path/to/my/file.functionName()

That's a syntax error, since that's equivalent to (path/to/my) / ( file.functionName() ), which is an expression, not a statement (in the same way that 1 + 1 is not a valid line of lua code).



#79352 Octree - Efficient way of storing, and querying 3D data

Posted by Eric on 23 January 2013 - 10:34 AM in APIs and Utilities

View PostBigSHinyToys, on 23 January 2013 - 09:08 AM, said:

View PostNeverCast, on 23 January 2013 - 08:46 AM, said:

If this were me I would've just created a function that returns a unique key for x,y,z, and then dropped values in to the table with those keys.
It would automatically return nil if it didn't exist.

That said, the code is impressive.. and maybe one day it'll have a cause!
something like this ??
local tTable = {}
local function get(x,y,z)
	return tTable[table.concat({x,y,z},"-")]
end
local function set(x,y,z,val)
	 tTable[table.concat({x,y,z},"-")] = val
end

Or in a cooler way:
local grid_mt = {
  __index = function(self, t)
    return rawget(self, table.concat(t, "-"))
  end,
  __index = function(self, t, v)
    return rawset(self, table.concat(t, "-"), v)
  end
}

local grid1 = setmetatable({}, grid_mt)
local grid2 = setmetatable({}, grid_mt)

grid1[{1, 2, 3}] = 3
grid2[{4, 5, 6}] = grid1[{1, 2, 3}]