LiveScript icon indicating copy to clipboard operation
LiveScript copied to clipboard

object and array slices by array should work

Open clkao opened this issue 12 years ago • 5 comments

o = a: 123, b: 394, c: 321

you can do:

o<[a b]> # => [123, 394]

which is great, if you have keys = <[a b]>, this should work too:

o[...keys] # => [123, 394]

(note that o[...<[a b]>] does work)

same thing for array should work as well:

[1,2,3][...indexes] where indexes = [1,2]

clkao avatar Aug 29 '12 11:08 clkao

this is apparently closed by the merge with coco by accident, i believe that's #162 for coco ;) please reopen.

clkao avatar Sep 04 '12 00:09 clkao

Right now, o[...keys] becomes javascript: slice$.call(o[keys]) which will fail with Array.prototype.slice called on null or undefined unless you have a key like "a,b" (javascript forces the key to become a string for lookup in o).

A work-around:

out = [o[k] for k in keys]

If you use prelude:

map o, keys

Apparently o[...<[a b]>] works because the compiler optimizes it to o['a', 'b'] which then becomes [o['a'], o['b']] by LiveScript before reaching JavaScript.

josher19 avatar Sep 04 '12 04:09 josher19

Arise, dead thread! I would be in favor of this change—I think it's the more intuitive interpretation of the syntax, and obviously more consistent given the behavior of o[...<[a b]>]. But it is a breaking change, and there is currently one test that contraindicates it in chaining.ls:

a = [0 1 2][[0 1], {2}]
eq '0,1' ''+a.0
eq '0,1' ''+a[...0] # would fail because 0 is not an array
eq 2 a.1.2

I'm still for it; what do others think?

rhendric avatar Feb 05 '16 22:02 rhendric

The current behavior seems hardly intuitive. a[...b] compiles roughly to a[b].slice() (an equivalent of [...a[b]]), so I doubt anyone would want to use it with that intention. I may be wrong though.

pepkin88 avatar Feb 07 '16 15:02 pepkin88

There are, I must admit, valid use cases for the current behavior that don't neatly adapt to the new one. parts[\first \second ...\rest] as shorthand for [parts.first, parts.second].concat(parts.rest) is kind of nice. Still unintuitive IMO given the way splat works otherwise, but convenient if that's what you want.

rhendric avatar Feb 08 '16 00:02 rhendric