aardappel / lobster

The Lobster Programming Language
http://strlen.com/lobster
2.25k stars 119 forks source link

remove(0,0) results in out of range error #196

Closed AntonBogun closed 2 years ago

AntonBogun commented 2 years ago
let l=[]
l.remove(0,0)
print(l)

results in VM error: remove: index (0) or n (1) out of range (0), while it should do nothing.

aardappel commented 2 years ago

0 is actually a default value such that if you leave it out, it removes just 1 element.

I should allow it to use other values as defaults, that would fix this.

aardappel commented 2 years ago

This should fix it.. https://github.com/aardappel/lobster/commit/780df79b481181cde42133323dcf09aea96d2026 It actually improves default arg handling for builtin functions generally.

aardappel commented 2 years ago

Actually, I was trying to support 0 for the amount to remove, but that can't work since its intended to return the first thing it removes: https://github.com/aardappel/lobster/commit/8071024d25b265a7534ecb69dc413f99a313143a

AntonBogun commented 2 years ago

Maybe should add a del() which is same as remove() but without returning the values?

aardappel commented 2 years ago

Just so removing 0 elements becomes valid? I understand that's nice for genericity, but it seems OTT to add another function just for that edge case.

If anything, I could separate out remove into 2 overloads: one arg that returns something, and 2 args (range) that returns nothing.

AntonBogun commented 2 years ago

Sounds like a good idea

aardappel commented 2 years ago

The version with a size parameter is now called remove_range: https://github.com/aardappel/lobster/commit/8654eab9409f926834bfb830f5ea948077f76976

Implementation is streamlined too.