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

Most Needed Functions?

Started by Left, 03 April 2013 - 06:59 PM
Left #1
Posted 03 April 2013 - 08:59 PM
What are the most needed functions for CC/Lua?

E.g. split, encrypt, etc.

I'm gonna make a Library on them
Mads #2
Posted 03 April 2013 - 10:04 PM
The ones which are already in the standard library.
Engineer #3
Posted 03 April 2013 - 10:31 PM
Print Centered function?:


local cPrint = function(text, ny) -- By GravityScore
	if type(text) == "table" then for _, v in pairs(text) do centerPrint(v) end
	else
		local x, y = term.getCursorPos()
		local w, h = term.getSize()
		term.setCursorPos(w/2 - text:len()/2, ny or y)
		print(text)
	end
end

As it says, its by GravityScore as I stole it from the snippets section in Sublime Text 2:P
Left #4
Posted 03 April 2013 - 10:40 PM
Great but I better write my own function. I don't have permission from GravityScore
theoriginalbit #5
Posted 03 April 2013 - 10:46 PM
make sure that when you calculate the x position you use this formula. Engineer must have an outdated ST2 plugin, because I'm pretty sure Grav added this formula after I told him too :P/>

local x_pos = screen_width / 2 - #text_to_print / 2 + (#text_to_print % 2 == 0 and 1 or 0)
this formula ensures that the text always appears to be printed in the centre. since even length strings never look quite right when just using the standard centre formula, need to +1 onto their post to make it look nice. :)/>
Shnupbups #6
Posted 03 April 2013 - 11:26 PM
table.unconcat(<splitter>)
Unconcatenates a concatenated table back into a table. Does not work if concatenated with no splitter. I need this for CCCC Beta 1.1.
theoriginalbit #7
Posted 03 April 2013 - 11:39 PM
table.unconcat(<splitter>)
Unconcatenates a concatenated table back into a table. Does not work if concatenated with no splitter. I need this for CCCC Beta 1.1.
a string split should do the job…

split( stringFromTable, 'characterUsedWhenConcat')

only issue I can foresee is if you had a value in a table like say 'this, that' then you concat it with ', ' … the split would see that as 2 different entries… but there is no way around that, in any case, a string split is what you are after.
Shnupbups #8
Posted 03 April 2013 - 11:54 PM
a string split should do the job…

split( stringFromTable, 'characterUsedWhenConcat')

only issue I can foresee is if you had a value in a table like say 'this, that' then you concat it with ', ' … the split would see that as 2 different entries… but there is no way around that, in any case, a string split is what you are after.
WHATEVER

Also, Cloudy is looking at this topic…
Bubba #9
Posted 04 April 2013 - 12:02 AM
Also, Cloudy is looking at this topic…
How is that at all relevant…?

But anyway, I don't really see a need for table.unconcat when you've got serialize/unserialize (limited though it may be, it is still more powerful than table.concat in terms of doing that sort of thing). I can understand how table.concat has its uses, but it still seems like an unconcat function has more limitations than it does advantages what with the issue you mentioned BIT.

As for the "most needed functions" I would honestly like nothing better than having a good inventory management system (there may be one in the API section, but I'm too tired and annoyed to look right now).
theoriginalbit #10
Posted 04 April 2013 - 12:04 AM
WHATEVER
I was just trying to help.
theoriginalbit #11
Posted 04 April 2013 - 12:06 AM
But anyway, I don't really see a need for table.unconcat when you've got serialize/unserialize (limited though it may be, it is still more powerful than table.concat in terms of doing that sort of thing). I can understand how table.concat has its uses, but it still seems like an unconcat function has more limitations than it does advantages what with the issue you mentioned BIT.
Exactly. a serialize/unserialize is good for when you need to transfer or process but be able to reverse it later… table.concat is good for displaying a table in a consistent format but there is just no reliable way to come back from it…

EDIT: whoops… thought I clicked edit, sorry for the double post guys…
Edited on 03 April 2013 - 10:07 PM
GravityScore #12
Posted 04 April 2013 - 01:48 AM
make sure that when you calculate the x position you use this formula. Engineer must have an outdated ST2 plugin, because I'm pretty sure Grav added this formula after I told him too :P/>

local x_pos = screen_width / 2 - #text_to_print / 2 + (#text_to_print % 2 == 0 and 1 or 0)
this formula ensures that the text always appears to be printed in the centre. since even length strings never look quite right when just using the standard centre formula, need to +1 onto their post to make it look nice. :)/>

You asked me to add that? I never saw that. Adding it now anyway :P/>

Great but I better write my own function. I don't have permission from GravityScore

Anything but my programs I really don't care if you use without my permission. All APIs/utilities/functions are free to mod/use without permission. I should really put that somewhere…

only issue I can foresee is if you had a value in a table like say 'this, that' then you concat it with ', ' … the split would see that as 2 different entries… but there is no way around that, in any case, a string split is what you are after.

You could write your own concat function that always preceded any instances of the string to concat the table with with a \ or something. Then when un-concatenating, just check if the instance has a \ before it.
Engineer #13
Posted 04 April 2013 - 02:39 AM
make sure that when you calculate the x position you use this formula. Engineer must have an outdated ST2 plugin, because I'm pretty sure Grav added this formula after I told him too :P/>

local x_pos = screen_width / 2 - #text_to_print / 2 + (#text_to_print % 2 == 0 and 1 or 0)
this formula ensures that the text always appears to be printed in the centre. since even length strings never look quite right when just using the standard centre formula, need to +1 onto their post to make it look nice. :)/>

make sure that when you calculate the x position you use this formula. Engineer must have an outdated ST2 plugin, because I'm pretty sure Grav added this formula after I told him too :P/>

local x_pos = screen_width / 2 - #text_to_print / 2 + (#text_to_print % 2 == 0 and 1 or 0)
this formula ensures that the text always appears to be printed in the centre. since even length strings never look quite right when just using the standard centre formula, need to +1 onto their post to make it look nice. :)/>

You asked me to add that? I never saw that. Adding it now anyway :P/>

Lol I thought: I have the github verson.. Ooh wait. It wasnt even added :P/>
theoriginalbit #14
Posted 04 April 2013 - 03:11 AM
You asked me to add that? I never saw that. Adding it now anyway :P/>
It was in the centre write function that I gave you…

You could write your own concat function that always preceded any instances of the string to concat the table with with a \ or something. Then when un-concatenating, just check if the instance has a \ before it.
Kinda defeats the purpose of table.concat if you are going to start adding some kind of escapes for your code to pickup on unconcat. you may as well use textutils.serialize/unserialize, as your concat would be useless with random stuff in there….

Example:

if i have this table

local t = {
  'This',
  'that',
  'other.',
  'good,',
  'bad,',
  'ugly',
}

now I use this

print( table.concat( t, ' ' ) )
it would output this
This that other. good, bad, ugly

now suppose we use our custom concat so we can unconcat it later it would be this
This\ that\ other.\ good,\ bad,\ ugly

kinda defeats the purpose don't you think? may as well just use a split on it… or just preserve the table!
Dlcruz129 #15
Posted 04 April 2013 - 04:31 AM
Encryption is useful if you're making an OS.
theoriginalbit #16
Posted 04 April 2013 - 04:59 AM
Encryption is useful if you're making an OS.
When was OS mentioned?
Or are you asking for an encryption function?
Dlcruz129 #17
Posted 04 April 2013 - 06:07 AM
Encryption is useful if you're making an OS.
When was OS mentioned?
Or are you asking for an encryption function?

I was just saying that encryption is useful.
Shnupbups #18
Posted 04 April 2013 - 12:15 PM
How is that at all relevant…?
It wasn't. I am a random guy.
I was just trying to help.
I accidentally held caps lock instead of shift on the W, so I'm sorry if it sounded rude. If it would've sounded rude anyway, I am also sorry, but I do have asperger's so it's… hard for me to communicate nicely…
Left #19
Posted 06 April 2013 - 02:43 AM
Encryption or hash? Maybe both?
Smiley43210 #20
Posted 07 April 2013 - 12:35 AM
As well as their counterparts (ex. decryption)
Engineer #21
Posted 07 April 2013 - 01:04 AM
Encryption or hash? Maybe both?
I recommend using hash, since everyone has acces to the decrypt files
Mackan90096 #22
Posted 11 April 2013 - 01:21 AM
term.clear() ?
superaxander #23
Posted 11 April 2013 - 03:41 AM
term.clear() ?
No term.setCursorPos cause you can then manually clear the screen with spaces
samdeman22 #24
Posted 11 April 2013 - 05:34 AM
some functions for printing to a page would be nice, I think it would be good if printing was a bit easier in pretty much every aspect.
SadKingBilly #25
Posted 11 April 2013 - 01:11 PM
Some of these might already exist, but whatever.

Function that checks the parity of a number. If number is even, it returns true; otherwise, it returns false.
Spoiler

function parity(number)
	if number % 2 == 1 then
		return false
	else
		return true
	end
end

Function to speed up printing with colors. Default text color is white and default background color is black, but both can be customized.
Spoiler

function colorWrite(text, x, y, color, bgColor)
	color = color or colors.white
	bgColor = bgColor or colors.black
	term.setTextColor(color)
	term.setBackgroundColor(bgColor)
	term.setCursorPos(x, y)
	term.write(text)
end

Function to combine two things that we are always doing together. Clears the screen and puts cursor at (1, 1). Optionally changes the background color.
Spoiler

function refresh(background)
	background = background or colors.black
	term.clear()
	term.setCursorPos(1, 1)
end

Function to concatenate several strings with a custom separator.
Spoiler

function mergeText(...)
	local result = ""
	for i = 2, #arg do
		if i == #arg then
			result = result .. arg[i]
		else
			result = result .. arg[i] .. arg[1]
		end
	end
	return result
end

And, of course, a split function.