Log In  

Cart #minsnakegame-3 | 2022-04-29 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

An attempt at size-optimizing the classic game of snake.
The final code was 416 in characters.

l=3w=16h=w::_::cls()for i=1,w*h do
m[i]=m[i]or 0if(m[i]>0)m[i]-=1
if(m[i]==0)k-=1m[i]=(k==0)and -8/3 or 0
if(m[n]<=0)m[n]=l f()f()f()goto _
?"game over"

Here is the commented version.

Cart #minsnakegame_commented-0 | 2022-04-29 | Code ▽ | Embed ▽ | No License

Feel free to post any suggestions!

P#111066 2022-04-29 17:47

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)

I was trying to get string indexing using brackets to work on pico-8.
I quickly found this discussion on lua-uers.org that discusses this problem in Lua 5.1 and 5.2.(See below)

However, after trying several solutions provided in the discussion I still can't get any to work in pico-8.
They work on my other Lua IDEs, but somehow pico-8 always returns runtime errors.

Current code:

--from lua-users.org
getmetatable('').__index = function(str,i) return string.sub(str,i,i) end
getmetatable('').__call = function(str,i,j)  
  if type(i)~='table' then return string.sub(str,i,j) 
    else local t={} 
    for k,v in ipairs(i) do t[k]=string.sub(str,v,v) end
    return table.concat(t)


Current results:

Does anybody know how to correctly implement this in pico-8?

P#110352 2022-04-17 11:49

Follow Lexaloffle:        
Generated 2022-07-01 08:39:26 | 0.049s | Q:13