lua-users home
lua-l archive

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


Op Sun, 10 Oct 2021 09:22:16 +0200 schreef Francisco Olarte <folarte@peoplecall.com>:

In the table library I did, I use range() and similar functions, and I
have them ( with different names ) in lua ( closed ), half-open and
start-offset variants. It's just about a page of code ( for sequences
). You do not have that many types of range, and having different
functions make it easier for me than remembering magic things for
reversing.

Francisco Olarte.


You and Javier got me thinking.
A range object is more explicit and readable than bare indices despite its more verbose.

So, a function supporting ranges would be like:

library.transform( input, output, operation )

'input' and 'output' can be a table or a range.
A range is a value returned by one of these functions:

library.range( table [, begin [, end ] ] )
library.reversed_range( table [, begin [, end ] ] )

The usage will be like:

function func( x )
      return x * x
end

t = { 1, 2, 3, 4, 5, 6 }

u = {}
library.transform( t, u, func )
-- u: { 1, 4, 9, 16, 25, 36 }

v = { 0, 0, 0, 0, 0, 0 }
library.transform( library.range( t, 1, 3 ), library.reversed_range( v ), func )
-- v: { 0, 0, 0, 9, 4, 1 }


I don't mind to write more (verbose) code when something special happens.
Also a range can be a result that is used as an input for another function.


This idea to use ranges seems to be a good candidate for a table manipulation API.


-- Jasper