Topic: https://intoli.com/blog/python-slicing-in-javascript/
hide preview

What's next? verify your email address for reply notifications!

71f9vbKS 6y, 115d ago

You shouldn't be allowed near computers.

hide preview

What's next? verify your email address for reply notifications!

unverified 6y, 114d ago
// Wrap an array of 5 elements.  
var array = wrapArray([0, 1, 2, 3, 4])
...

// Outputs: 3  
console.log(array[-1]);

Nope: it would return 4. :)
Speaking of which, it also seems that "-0" would be parsed to array.length - 0 == array.length, giving us undefined... So I guess the lack of forEach etc is not even the main problem of this naive implementation. :)
Other than that, a very interesting article!

remark link
hide preview

What's next? verify your email address for reply notifications!

evan 6y, 114d ago

The naive implementation defines a property for "-0", but it can only be accessed by explicitly using that string as a property name. Negative zero is a distinct value from zero in JavaScript, but both are coerced to strings as "0". I thought about adding an if (i === 0) { return; } in there, but I figured that most people wouldn't think twice about it :-).

hide preview

What's next? verify your email address for reply notifications!

unverified 6y, 114d ago

Hmm, the first example is wrong as [0,1,2,3,4][-1] will output 4 in Python (-1 = get the last thing).

remark link
hide preview

What's next? verify your email address for reply notifications!

evan 6y, 114d ago

Thanks, that typo is fixed now!

hide preview

What's next? verify your email address for reply notifications!

unverified 6y, 106d ago [edited]

This is a nice write up. I had the same idea a while back and wrote this function:

function sliceArray(array=[]) {
  return new Proxy(array, {
    get(target, key) {
      const sliceMatch = key.match(/^(-?\d+)?:(-?\d+)?$/);
      if(sliceMatch) {
        const [/* match */, startIndex=0, endIndex=target.length ] = sliceMatch;
        return sliceArray(target.slice(+startIndex, +endIndex));
      }
      return target[key];
    }
  });
}

const a = sliceArray([1,2,3,4,5,6,7,8,9,10]);

console.log(a['3:-2']['1:']); // outputs: [5, 6, 7, 8]

I opted to use the native Array.prototype.slice since it handles negative indexes (I didn't think to add a step input, but it's not supported by slice anyway).

remark link
hide preview

What's next? verify your email address for reply notifications!

evan 6y, 106d ago

Nice! The slice package actually does use the Array#slice/#splice methods whenever possible, but the extended slices generally require special handling. I left that part out of the article so that the implementation would be a little simpler.

hide preview

What's next? verify your email address for reply notifications!

uranusjr 6y, 84d ago [edited]

I’m not exactly sure how good it would be, but there are likely things you could exchange with PyBee’s Batavia project, which implements Python on top of JavaScript. They already have Python lists and slices sort out. They would love any help to correct the implementation if you find any problems as well!

remark link
hide preview

What's next? verify your email address for reply notifications!

evan 6y, 81d ago

Very cool, thanks for sharing the links! I had heard of the project before, but never looked at the code. It looks like we have the same optimizations in place for when step=1, but I hadn't thought to do result.slice(stop, start).reverse() when step=-1. I'll have to add that to the slice package.

hide preview

What's next? verify your email address for reply notifications!