Skip to content

Instantly share code, notes, and snippets.

@ThomasKruegl
Last active August 21, 2020 14:16
Show Gist options
  • Save ThomasKruegl/6f462cb92c18a29f82a85cf305d66801 to your computer and use it in GitHub Desktop.
Save ThomasKruegl/6f462cb92c18a29f82a85cf305d66801 to your computer and use it in GitHub Desktop.
Explanations for https://eloquentjavascript.net/06_object.html#c_uuBcsDdS7D
Matrix constructor:
this.content[y * width + x] = element(x, y);
-> this.content[3 * 5 + 2] = element(2, 3);
so, what is element(2,3)? element is the argument passed to the constructor, here (in symmetricMatrix):
super(size, size, (x, y) => {
if (x < y) return element(y, x);
else return element(x, y);
});
-> So 'element(2,3)' in the Matrix class is:
if (2 < 3) return element(3, 2); else return element(2,3);
-> Since 2 is smaller than 3, this simplifies to:
return element(3, 2)
Where element is now the function you have passed into the SymmetricMatrix constructor, which is (x, y) => `${x},${y}`)
-> So element(3,2) is:
`${3},${2}`
That's why content[3 * 5 + 2] = '3,2' ;)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment