Last active
August 21, 2020 14:16
-
-
Save ThomasKruegl/6f462cb92c18a29f82a85cf305d66801 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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