lua-users home
lua-l archive

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


On 07/31/2018 08:17 AM, Dirk Laurie wrote:
>> ... recursive functions (where you can't call itself without previous declaration).
> 
> The only [1] place in Lua where syntax sugar saves you one statement is
> 
>     local function f(...)
> 
> which is not equivalent to
> 
>     local f = function(...)
> 
> but to [2]
> 
>     local f; f = function(...)
> 
> precisely in order that recursive functions work.
> 
> [1] Please show me wrong and exhibit anotther. You'll make my day.
> [2] I'm putting in the semicolon to prove that there are two statements.

Yeah. But I do not use it anyway, preferring variant "2".

Not just because it's "sugar" but because

  1. It hides important fact that Lua functions are just
    assignable values.

  2. It fails to solve more general mutual recursion case:

    local f, g
    f =
      function()
        f()
        g()
      end
    g = f


On 07/31/2018 02:33 AM, Sean Conner wrote:>> I use upvalues mostly in recursive
functions (where you can't call
>> itself without previous declaration).
>
>   With the Y-combinator you can:
>
> 	function Y(f)
> 	  local function g(...) return f(g,...) end
> 	  return g
> 	end
>
> 	print(Y(function(rec, x) if x < 2 then return 1 else return x * rec(x-1) end
end)(5))

Cool, but I feel it's too smart for me.

>> And separate almost any "function" statement as separate module.
>> That is why I was need relative require().
>
>   I'm not sure I understand this.
>
>   -spc

Cause I have more horrible habit of building modules not
like "[...] return {do_a = do_a, do_b = do_b}" but like
"return {do_a = require('do_a'), do_b = require('do_b')".

Surely there will be name clashes, so files structured
in directories. But to avoid fully specifying path in
every require() I've implemented wrapper of require()
that allows relative paths.

This is my personal obsession somewhat similar to yours
indentation. Does not solves any real problem, requires
energy to maintain, but gives nice inner feeling and
motivates to code. Is this an Anonymous Alcoholics club?

-- Martin