lua-users home
lua-l archive

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




Le dim. 30 juin 2019 à 15:20, Sergey Kovalev <kovserg33@gmail.com> a écrit :
Chain symbol "!"
It should expand as follow:

function_name!_expression_ --> function_name(_expression_)
sin!x -> sin(x)
fn!function() end -> fn(function() end)
f1!f2!f3!f4 -> f1(f2)(f3)(f4)

You last statement gives a contradiction, because it is left-associative (it performs functions composition, similar to the circle "∘" math operator)
  f1∘f2∘f3∘f4 = f1(f2(f3(f4)))
  sin∘x = sin x = sin(x)

It is different from "chaining" which is normally right-associative and is written in the other direction (i note it with the "::" operator like in Lua calls of a member of the left-side table):
  f1::f2::f3::f4 = f4(f3(f2(f1))) = f4∘f3∘f2∘f1

But your last statement actually does not mean any composition, because:

  f1(f2)(f3)(f4)  

actually means:

  ( (f1(f2) )(f3) )(f4)  

and it is NOT a composition of the 4 functions. It just means that

  - f2 is an arbitrary parameter, which is processed first by calling the function f1 which returns some unknown function "u1",
  - which is then called with an arbitrary parameter f2 and returns another unknown function "u2",
  - which is then called with an arbitrary parameter f4.

Nothing in the Lua notation "f1(f2)(f3)(f4)" indicates that f2, f3, or f4 are functions, only f1 is assumed to be a function. However:

  - f1 and "u2" are assumed to RETURN functions,
  - "u1" and "u2" are assumed to accept a function in their first parameter (they may as well accept other types, including a number, string, table, userdata, thread, or nil, and could accept more parameters).

Hmmm... All this "chaining" is then counterintuitive. I prefer the Lua definition using "::" (in the reverse direction) for chaining method calls, which is conceptually a regular mathematic composition of function (except that with "::" it is written in the reversed order compared to the conventional mathematical notation using "∘")

----

I think that you really want to propose is a true function composition, like in maths and it will be more intituively written using right-associativity:

  f1!f2!f3!f4 = f1(f2(f3(f4))) = f4()::f3()::f2()::f1
  f1!f2!f3!f4 (x) = f1(f2(f3(f4(x)))) = f4(x)::f3()::f2()::f1

so that we also have:

  sin!x = sin x = sin(x) = x::sin()