Jump to content




BasicUtils v1.5

api lua

  • This topic is locked This topic is locked
20 replies to this topic

#1 superaxander

  • Members
  • 609 posts
  • LocationHolland

Posted 18 March 2013 - 08:28 AM

BasicUtils v1.5

BasicUtils is a collection of functions that you can use in many situations. It is divided in several parts.
TableUtils
Spoiler
SystemUtils
Spoiler
GraphicUtils
Spoiler
StringUtils
Spoiler
Misc
Spoiler

Usage:
First run the file using shell.run and you're ready.

Download:
pastebin get DQ7JUHxT utils

Changelog
Spoiler


Cheers

Edited by superaxander, 22 March 2013 - 08:47 AM.


#2 superaxander

  • Members
  • 609 posts
  • LocationHolland

Posted 18 March 2013 - 11:50 PM

Version 1.3 released!
-Added isFunction
-Added graphicutils.setBackgroundColor(color)
-Added systemutils.startThread(func1,func2,...)

#3 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 18 March 2013 - 11:59 PM

haha! my assert function! :D

#4 superaxander

  • Members
  • 609 posts
  • LocationHolland

Posted 19 March 2013 - 12:00 AM

View PostTheOriginalBIT, on 18 March 2013 - 11:59 PM, said:

haha! my assert function! :D/>/>
I know I needed it in some Functions.

Edited by superaxander, 19 March 2013 - 12:00 AM.


#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 19 March 2013 - 12:03 AM

View Postsuperaxander, on 19 March 2013 - 12:00 AM, said:

I know I needed it in some Functions.
Yeh I noticed...

Some improvements to your is functions
Spoiler


#6 superaxander

  • Members
  • 609 posts
  • LocationHolland

Posted 19 March 2013 - 12:06 AM

Didn't think of that xD

#7 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 19 March 2013 - 12:13 AM

View Postsuperaxander, on 19 March 2013 - 12:06 AM, said:

Didn't think of that xD
:P here is another improvement... your tableutils.same function is REALLY inefficient and actually returns true when it shouldn't in some cases ( i tested :P )

so here is an improved version :)
Spoiler


#8 superaxander

  • Members
  • 609 posts
  • LocationHolland

Posted 19 March 2013 - 12:23 AM

Version 1.4 Released
-Changed is functions thanks to TheOriginalBIT
-Changed tableutils.same thanks to TheOriginalBIT
-Changed tableutils.copy thanks to TheOriginalBIT
-Fixed bugs thanks to TheOriginalBIT

Edited by superaxander, 19 March 2013 - 01:25 AM.


#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 19 March 2013 - 12:39 AM

More bugs and some fixes

A slightly fixed copy function
Spoiler

Line 48 and 53 missing a ')'
Spoiler

Line 147—151 extra ')'
Spoiler

Line 151 missing ')' at end of line

lots of problems in graphicutils.drawImage
Spoiler


#10 superaxander

  • Members
  • 609 posts
  • LocationHolland

Posted 19 March 2013 - 01:26 AM

View PostTheOriginalBIT, on 19 March 2013 - 12:39 AM, said:

More bugs and some fixes

A slightly fixed copy function
Spoiler

Line 48 and 53 missing a ')'
Spoiler

Line 147—151 extra ')'
Spoiler

Line 151 missing ')' at end of line

lots of problems in graphicutils.drawImage
Spoiler
Fixed! Thanks

#11 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 19 March 2013 - 01:31 AM

View Postsuperaxander, on 19 March 2013 - 01:26 AM, said:

Fixed! Thanks
No problems... fyi most of those bugs could have been found just by running the script and letting the compiler yell at you :P

#12 superaxander

  • Members
  • 609 posts
  • LocationHolland

Posted 19 March 2013 - 01:50 AM

Ik but I wasn't on a computer today

#13 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 22 March 2013 - 06:40 AM

oh, I suggest adding a deepCopy to tableutils.... that way if there is a table within the table it will make a copy of that too, not just reference it
I've done the code.
Spoiler


#14 superaxander

  • Members
  • 609 posts
  • LocationHolland

Posted 22 March 2013 - 08:44 AM

View PostTheOriginalBIT, on 22 March 2013 - 06:40 AM, said:

oh, I suggest adding a deepCopy to tableutils.... that way if there is a table within the table it will make a copy of that too, not just reference it
I've done the code.
Spoiler
Thanks!

#15 superaxander

  • Members
  • 609 posts
  • LocationHolland

Posted 22 March 2013 - 08:48 AM

V1.5 Is out!
Changelog:
-Added tableutils.deepcopy functions thanks to TheOriginalBIT

#16 Eric

  • Members
  • 92 posts
  • LocationUK

Posted 22 March 2013 - 12:35 PM

But can you deepcopy while preserving references?

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

local q = deepcopy(t)

assert(q.self == q)


#17 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 22 March 2013 - 02:00 PM

Here's a deep-copy function that preserves references as Eric said.
function table.deep_copy(t)
  local lookup = {}
  local function copy(obj)
	if type(obj) ~= "table" then
	  return obj
	elseif lookup[obj] ~= nil then
	  return lookup[obj]
	end
	local tbl = {}
	lookup[obj] = tbl
	for k, v in pairs(obj) do
	  tbl[copy(k)] = copy(v)
	end
	return setmetatable(tbl, getmetatable(obj))
  end
  return copy(t)
end
It also copies the metatable (doesn't really copy it, just sets it to the same).

Edit: damn forum not keeping indentation :P

Edited by MysticT, 22 March 2013 - 02:03 PM.


#18 albrat

  • Members
  • 162 posts
  • LocationA Chair

Posted 01 July 2013 - 04:13 AM

I was taking a look at this thread and saw that you needed a string handling function still... :)

this code takes a string with "breaker" points in it and removes the breakers / splits the output into a table.


-- Our string splitting Function

function string:split( inSplitPattern, outResults )

   if not outResults then
	  outResults = { }
   end
   local theStart = 1
   local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
   while theSplitStart do
	  table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
	  theStart = theSplitEnd + 1
	  theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
   end
   table.insert( outResults, string.sub( self, theStart ) )
   return outResults
end

The function is called by inserting this type of code

mess = "test code write message"
tablemes = string.split(mess, " ")  --  This is the string splitter and its processing.
message = tablemes[1]  --  --> output of "test"
pas = tablemes[2] --  -->  Output of "code"

The default for the script is to remove a SPACE char. but by changing what you send to the script it can remove any char.

#19 superaxander

  • Members
  • 609 posts
  • LocationHolland

Posted 01 July 2013 - 06:05 AM

I am sorry this is discontinued

#20 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 01 July 2013 - 06:11 AM

View Postalbrat, on 01 July 2013 - 04:13 AM, said:

I was taking a look at this thread and saw that you needed a string handling function still... :)

this code takes a string with "breaker" points in it and removes the breakers / splits the output into a table.

-- Our string splitting Function

function string:split( inSplitPattern, outResults )

   if not outResults then
	  outResults = { }
   end
   local theStart = 1
   local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
   while theSplitStart do
	  table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
	  theStart = theSplitEnd + 1
	  theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
   end
   table.insert( outResults, string.sub( self, theStart ) )
   return outResults
end
There is a far more efficient method of splitting a string... I suggest looking into Lua Patterns

function string.split( str, patt )
  local t = {}
  for s in str:gmatch('[^'..patt..']+') do
    t[#t+1] = s
  end
  return t
end

Also as for this statement
if not outResults then
  outResults = { }
end
we can use some logic to make this easier for us...
outResults = outResults or {}
this works because of 2 things...
1. in Lua nil resolves to false...
2. boolean logic

Quote

true or true = true
true or false = true
false or true = true
false or false = false
So basically when outResults is nil, it becomes false, and the value after the `or` is used, meaning a new table will be assigned.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users