More elegant coding

Sorry to bother anyone…I’m trying to get to grips with Lua basic functions. And have written the following code which works exactly as I want it to, however as I’m on steep learning curve I would be grateful to know if there is a much simpler way of achieving the aims of the code, or if the code itself could be more elegantly formed.

--to convert the numbers 0 to 255 into binary 
--and store the binary code for each number in a table.

function setup()
    maptilecode={}                       -- creates table/array
    for ident = 1,256 do
        maptilecode[ident]={}          -- makes 256 columns long
        for y=1,9 do
            maptilecode[ident][y]=0   -- by 9 rows deep, all entries 0
        end
        binary(ident,ident-1,1)         -- now writes the binary code for the value, 
                                                    -- 1 less than the identity to the 
                                                    -- identity col in maptile code
        end
        

end

function binary(ident,rntot,i)    -- fnc "binary" recieves identity,
                                                -- initial running total and counter
    rntot,r=math.modf(rntot/2)    -- gives int and float on division by 2
    if r>0 then r=1 else r=0 end  -- converts float to binary remainder
    maptilecode[ident][i]=r          -- records remainder as digit in maptilecode
    i = i + 1                                 -- increases count
    if rntot>0 then binary(ident,rntot,i) else return -- again or stop?
    end

end

Also as you may have noticed I haven’t understood how to paste code in properly so that it maintains all it’s colours etc. Any advice would be nice. Sorry I’m a complete novice.

Three ‘~’ like this: — before and after your code will keep it readable

@TheAbstractMan

yes there’s an easyer, I’ll look if I can find and adjust my piece of code…

It looks like you are converting to binary, this should do the trick:

function setup() 
	maptilecode={} -- creates table/array 
	for ident = 1,256 do 
		maptilecode[ident]=toBinary(ident, 9) -- makes 256 columns long 
	end
end

function toBinary(n, b) -- number, amount of bits used
	local t = {}
	for i = b - 1, 0, -1 do
		local e = 2 ^ i
		if n >= e then
			n = n - e
			table.insert(t, 1)
		else
			table.insert(t, 0)
		end
	end
	return t
end

I can’t even read that at the moment… Press the edit button above your code, and put 3 ~ above and below your code, like:

~~~
Your code here
~~~

That way it won’t all get smushed into one line.

Personally, no offence @Jordan but I think this is a better method :wink:

function setup()
    
    bin = {}
    for i = 1, 256 do
        table.insert(bin, decToBase(i-1, 2))
        print(bin[i])
    end
    
end

function decToBase(IN, BASE)
    
    local B,K,OUT,D = BASE or 10, "0123456789ABCDEFGHIJKLMNOPQRSTUVW", ""
    if IN > 0 then
        while IN>0 do
            IN, D = math.floor(IN/B), math.mod(IN,B)+1
            OUT = string.sub(K,D,D)..OUT
        end
    else
        OUT = 0
    end
    return OUT
    
end

not only can it convert to binary, it can also convert to other bases as base 8 and 16

Note that I didn’t write this myself, I did a little research :wink:

None Taken, all in the name of elegant coding. I saw that page too :), but I thought that because the original post dealt with binary it would be faster to do pure binary math.

@Jordan ok, that’s probably true :wink: +1

This is somewhat unrelated… But it’s pretty difficult to write clean code for Codea anyways, because there’s no proper concept of a namespace. The global namespace just gets cluttered with more and more stuff.

Also: what idiot when designing lua decided that all variables should be global by default? I can just about tolerate duck typing and the other stuff, but that was just pure evil.

Here’s another way to create a binary table for the numbers 0 to 255.


function setup()
    binTab={}
    for z=0,255 do
        bin(z)
    end
    print(table.concat(binTab,"\
"))
end

function bin(z)
    str=""
    for a=7,0,-1 do
        if z>=2^a then
            str=str.."1"
            z=z-2^a
        else
            str=str.."0"
        end
    end
    table.insert(binTab,str)
end

Many thanks again. And to whoever edited the code whilst I was away to make it readable.