Log In  

This is an anonymous function shorthand suggestion & discussion for pico8.

sum=function(a,b)return a+b end

Right now, anonymous function definitions look like this.
This is way too long for such a simple function!
I know that Lua doesn't have its own short function; although efforts were made(as seen here), none stood out to be practical.
But as there is already a ton of shorts in pico8, why not add one more?

Of course, then we have to decide the syntaxes for this short.
Below I list three function shorts from other languages:

Verbose Lambda

As implemented in Python and Java.

lambda p1=default,p2:expression

sum=lambda a,b:a+b

(example from Python)
Allows default values, which makes the assignment of local values very concise.
From my experience of size-optimization in pico8, one pattern troubles me: nobody uses local variables.
Of course, size-optimized code is supposed to be purely procedural and I get that; still, they can be better with such a shorthand.

The arrow

Choice of Javascript.



Nice and short. (Don't need 2 characters for the arrow tho, one will do the trick)
Just like the Lambda method, it also allows default values.

> Personally, I think the Elixir fn(p1)-> expression end fits the Lua syntax better.
> But why arrow again when there are already parentheses to separate the parameters and the expression??
> Also, the additional end fails to capture the expression-like nature of anonymous functions. It's not a control structure(although it can be used as one).

Overall, I think this is the best choice.

The & shorthand

From Elixir.

sum = &(&1 + &2)

(example from https://elixirschool.com/en/lessons/basics/functions/)
This is the shortest so far.
Instead of naming the parameters, why not just number them?
This shorthand has several problems tho:

  1. doesn't align with the De Bruijn index.
  2. no default values.
  3. if I do &(&2), does this just evaluate to function(x)return function(y)return y end end? As you can see in this example, I just skipped &1 entirely. I do not think this is great at all for intuitive and readable code. It's confusing and not as elegant as shorthands should be. (Just an opinion, feel free to tell me how wrong I am.)

So I guess I won't consider this as an option.


A functional shorthand would make the pico8 experience even better.
I am not sure if this has been requested before, but it might shape how we code in pico8 by a lot.
Feel free to comment below on your favorite shorthand. I am sure there are even better syntaxes than the ones I have listed above.

P#110778 2022-04-25 12:57 ( Edited 2022-04-26 16:37)

Self Criticism: Such a shorthand might turn out to be disruptive to the Lua syntax. For now, most shorts in pico8 are alternative functions(the ? for print or t for time). These are pretty harmless because one can always assign something else to the variable. However, a function short is different and might not interact well with the rest of the Lua syntax.
(Self Rebuttal?: tbh, Lua function definitions are already very different in syntax than other structures... For the loops(except for repeat) one needs to append a "do" keyword to begin a block(of code). For if there's at least then. But for function, there is nothing. Hmmm...)

P#110829 2022-04-25 12:57 ( Edited 2022-04-25 14:48)

Gotta admit: I'm a fan of JavaScript's arrow functions.

P#110843 2022-04-25 17:06

PICO-8 has quite a few shorts that are more sophisticated syntactic replacements. I think simple versions of this idea, especially if syntactically limited to a single line like most PICO-8 preprocessor transforms, could replace to function(...) ... end easily enough.

P#110870 2022-04-26 03:21
:: merwok

Could people explain what these shorthands bring?

Lua already gives us inline anonymous functions, which are convenient to pass to another function call like foreach or custom sort functions. We can also define functions directly in tables. We don’t have a problem with self in the way that javascript has with this and that is one of the motivations for arrow functions. So what would this add? Saving a few characters?

P#110884 2022-04-26 13:57

I do use function shorthand/lambdas pretty frequently in other languages but I'm not sure it necessarily adds anything for pico-8/Lua.

Although a shorthand will save you some characters it's still just building a function underneath so it's not likely to actually save you any tokens and I feel like tokens are generally more precious than characters. That said, if you're using them a lot then the savings in characters might be substantial.

I do think a good shorthand could make code more readable, especially in the built-in editor where line length is severely limited. The shorter the definition the better chance the whole thing can fit onscreen at once. On the flip-side, overuse of lambdas can make code harder to follow but that's true whether the definition is short or not. And you can get the same readability benefits by defining a named function and passing it as a parameter rather than defining it inline. But, again, at the cost of characters and tokens.

If it were to be adopted I would probably go with some variation of the elixir example you gave (fn(p1)-> expression end) because this is basically already Lua syntax and seems like it would be a fairly easy thing to add. Essentially fn just needs to be translated to function and -> (or whatever other symbol you prefer) translates to return. Then the whole thing becomes function(p1) return expression end under the hood.

I don't think this would be a game changer or anything; more of a convenience than a whole new feature. But hey, convenient things are convenient so maybe worthwhile?

P#110891 2022-04-26 15:39

True, I don't think a shorthand would be able to save any tokens. I would still like something like this to happen though. I guess I'm just the kind of person who abuses lambda expressions whenever I get the chance to do so; so personally, this will be pretty game-changing for me. I guess the impact of such a shorthand mainly relies on the community - their coding style and their subjective preferences. I guess I am changing the post name to a convenience request rather than a feature request.

P#110893 2022-04-26 16:36

Even if shorthands like this didn't save any tokens, they'd still be useful for stuff like Tweetcarts and char-limited game jams.

P#110899 2022-04-26 19:00 ( Edited 2022-04-27 00:26)

my 2 cents, but at this point why even keep a lua-like syntax?
not sure the idea of forking pico8 lua even more from stock would help anyone but the most experienced (and multi-language) users.

P#110900 2022-04-26 19:21
:: dw817

I agree with @freds72.

Wow. That was unexpected. Somebody shoot me. :) But yeah, clarity has always been important to me in coding, especially when you don't need to compress code. Like for instance I don't even use a+=3 in my normal coding any more and always write it out as a=a+3 for ease of viewing.

On the other end of the stick though this suggested method might open up new TWEET CART methods. Just - don't lose sight of what we already have in current readability.

P#110907 2022-04-26 22:20 ( Edited 2022-04-26 22:35)

[Please log in to post a comment]

Follow Lexaloffle:        
Generated 2022-09-30 19:40:15 | 0.016s | Q:26