lua-users home
lua-l archive

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


Am 16.05.2016 um 14:34 schröbte Jonne Ransijn:
I don't really like the 1'000'000 syntax,

Me neither.

because I would personally rather be able to add syntax to call a function
like so:

print(sin 45)
-- (x) => expr
-- is the same as:
-- function(x) expr end

That probably should be `function(x) return expr end`.

local add = (x) => (y) => x + y
local con = (x) => (y) => x .. y
local con3 = (x) => (y) => (z) => x .. y .. z

I have written a `curry` function[1] that removes the need for this kind of line noise:

    local con3 = curry( 3, function( x, y, z ) return x .. y .. z end )

print(add 21 21)
print(con 'Hello, ' 7)
print(con3 1 '000' 000) -- con3(1000000) or con3(1)('000')(000)?

but 1_000_000 or 1..000..000 might work, or a whitespace could be made
mandatory?


Another syntax hack:

    local function num( a, ... )
      local r = 1.0 * a
      for i = 1, select( '#', ... ) do
        local v = select( i, ... )
        assert( type( v ) == "number" and v >= 0 and v < 1000 )
        r = r * 1000 + v
      end
      return r
    end

    print( num( 1,000,000,000,000 ) )    --> 1000000000000.0
    print( num( 2,012,014,144,567 ) )    --> 2012014144567.0
    print( num( 1,000,000,000,000,000,000,000 ) )    --> 1e+21

Converting to floats only when an integer overflow would occur, or restricting fractions to the last argument is left as an exercise for the reader.


Philipp


  [1]: https://github.com/siffiejoe/lua-fx#reference