lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On 25 January 2016 at 05:58,  <> wrote:
> Hello,
> I'm new to the mailing list, so let me know if I'm inadvertently doing anything wrong. If I am, sorry about that :P
> I was wondering if there is a posiblity of expanding the Lua standard library (mostly the string and table library). There are a few basic utility functions that I was hoping could be added to improve usability out of the box:
> Strings:
> - capitalize

This is a simple string.gsub call.
See also: string.upper

> - split (this one I think is much needed. I'm constently rewriting myself split functions).

Lua prefers iterators over table generation. You should too!
string.gmatch is your friend here.

> - strip (also write myself a lot of whitespace striping functions)

Many implementations are possible. See
In general, I prefer trim12.

> - insert

I don't know what this would do??

> Tables:
> - any (the same as any? in Ruby)

Had to look this up: any?: Passes each element of the collection to
the given block. The method returns true if the block ever returns a
value other than false or nil.

So equvalent to: local no_false = true; for i, v in ipairs(tbl) do if
v == false or v == nil then no_false = false; break end end return

I'm not sure why you would ever need this??

> - join (same as split)

See table.concat

> - select (a filter function that can either accept a value or a function)

Lua prefers iterators over table generation.
Possibly see libraries such as luafun:

> - shuffle (can be used to get a random value too, i.e `({1,2,3}):shuffle[1]`)

I'd prefer to implement this myself.
Also, thats terribly inefficient to shuffle a whole table just to get
the first one.
You can select a random element with: t[math.random(1, #t)]