Log In  Follow allenxch
 Size-optimized(not really) pong allenxch   :: Unfold ::    ```--init t,f,b,w,h=tonum,flip,btn,128,128 w,h,x,y,y0,y1=w/2,h/2,0,0,0,0 r,s,l,dx,dy=3,w-5,20,1,1color(7) --update while abs(x)s-r and abs(y-y1)
P#122848 2022-12-22 15:55

 Size-optimized Snake Game allenxch 1  :: Unfold ::  Cart #minsnakegame-3 | 2022-04-29 | Code ▽ | License: CC4-BY-NC-SA 1 An attempt at size-optimizing the classic game of snake.
The final code was 416 in characters.

 ```m={}x=3y=2r=1c=0o=0d=2^7f=flip l=3w=16h=w::_::cls()for i=1,w*h do m[i]=m[i]or 0if(m[i]>0)m[i]-=1 if(o)k=flr(rnd(w*h-l))+1o=nil if(m[i]==0)k-=1m[i]=(k==0)and -8/3 or 0 p=(i%w)*d/w q=flr((i-1)/h)*d/h rect(p,q,p+d/w-1,q+d/h-1,3*min(m[i],1))?l-3,0,0,7 end b=btn()t=(b&2)/2-(b&1)if(t~=0)r,c=t,0 t=(b&8)/8-(b&4)/4if(t~=0)c,r=t,0 x,y=(x+r)%w,(y+c)%h n=x+w*y+1if(m[n]<0)l+=1o=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 ▽ | No License 1 Feel free to post any suggestions!

P#111066 2022-04-29 17:47

 Convenience Request: Anonymous Function Shorthands allenxch 4  :: Unfold ::

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.

 ```(p1=deafault,p2=>expression) (a,b=>a+b)```

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.

## Conclusion

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)

 Help on string indexing with metatable allenxch   :: Unfold ::

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) end end --demo a="1234123512345" print(a)```

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

P#110352 2022-04-17 11:49

Generated 2023-03-21 21:32:10 | 0.060s | Q:17