A while ago I stumbled upon this snippet of code in the MDN web docs:
function Date(n) {
return ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"][n%7 || 0];
}
I found it interesting as I've never seen array syntax used like that before. Sure I've created arrays on the fly and done something like [1, 2, 3][0], but in this case with the [n%7 || 0], it uses n for the index we want and a most importantly that hardcoded 7 for the array length in order to "wrap" the n to the array.
Today I came across this question and when coding my answer:
let sampletext="Home/Student/Hello.txt"
let textarr = sampletext.split('/');
let idarray = textarr.map((x, i) => {
if(i === 0)
return { id: x, parent: '' };
return { id: x, parent: textarr[i - 1] };
});
console.log(idarray);
I noticed that I had to do the .split separately, as otherwise I couldn't meaningfully refer to the arr[i - 1] since if I had sampletext.split('/').map(...), I didn't have a reference to the array which sampletext.split('/') makes.
These two examples have something in common, which I'd like to call "missing left referencing" and what I mean by that is something like this:
Example 1 with "left referencing"
function Date(n) {
return ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"][n%left.length || 0];
}
Êxample 2 with "left referencing"
// "mapLeft" magically pulls whatever .split('/') spits out by using "left" keyword internally and passes it to the function passed to the mapLeft
let idarray = sampletext.split('/').mapLeft((x, i, left) => {
if(i === 0)
return { id: x, parent: '' };
return { id: x, parent: left[i - 1] };
});
So what I am looking for is a way to basically get a reference to the left side of an expression, from within the right side of an expression, the idea being that left would be a reserved word similar to this.
I don't think that anything like this exists in JavaScript at least, but I am wondering if any language supports such syntax and if syntax like this is even possible to implement into a language?