lua-users home
lua-l archive

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


On Sun, Jan 24, 2016 at 3:31 PM, Daurnimator <quae@daurnimator.com> wrote:
> On 25 January 2016 at 05:58,  <chuckiels2011@gmail.com> 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 http://lua-users.org/wiki/StringTrim
> 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
> no_false
>
> 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: https://github.com/rtsisyk/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)]
>

any? and all? are useful, convenient constructs for functional
programming, which Lua does a reasonable job at (especially due to it
supporting TCO). Usually you express it in combination with something
like map() as a way to determine if anything/everything in a set has
some property.

Consider something like https://mirven.github.io/underscore.lua/ if
you want to do functional programming in Lua.

/s/ Adam