Quiz
- I don't fully understand the .map method array. Why do we square each of the elements, or what happens in this line "const sqs = nums.map( x => x*x )"? Thank you!
Thanks for this question. Let's take it step by step, starting with Python's list comprehensions:
But if the code is complicated, bulky, or hairy, you might want a function instead:
JavaScript can do the same thing, mapping a function across an array (list):
Take a moment to understand the syntax of that. There's no
x
variable, and there are no parens afterhairy
. Instead, the hairy function is given tomap
andmap
will invoke it.Alternatively, if the code isn't too hairy, we can use a simple function literal:
Or, using arrow functions, it's even more compact:
And now we've come full circle: mapping an expression across a list.
That's what we are doing in the code that maps across a list of filenames.
- can you elaborate on the loading situation for textures. How do we make sure that the load is complete before we render the scene? The reading talks about the event handler, but how do we implement that? Does the code automatically wait for the loading event to finish? or is that something we need to manually adjust. sort of related, are there ways to estimate the amount of time to load a scene, so that during the creation process, we have a rough idea of how long we will need to be waiting (cuz if its a huge scene with lots of textures, and its gonna take more than 5 minutes, i'm gonna go grab icecream)
Wonderful question, and cleverly written.
Part 1: the structure of callback-based coding is like this:
The
startEvent
function initiates the event, using the arguments, and when the event happens or when the event is completed, it invokes theafterFunction
.JavaScript has used that ability for a long time.
For example, our keyboard callback setup:
The Threejs function is like that:
Part 2:
I've seen student webpages where they display 5-10 images from their phones (each of which are enormous) and it can take 30 seconds for the page to load. So, not enough time for ice cream, but don't do that. Resize the image to be a bit more frugal, if possible.
We'll look at this example next week: texture mapping with Caravaggio and even that image is only 149KB.
- Could you explain parallelism a little more? I got a bit confused in the reading, when it was mentioned in regards to .map.
Yes, this is an important question! The
map
method is not parallel, but it can initiate parallel events:Suppose I need six large items from my office, say mounted posters. I send six students, at the same time.
Then, I wait for all six to have returned.
Once that happens, I can continue with what I wanted to do with them.
- I'd be curious to know more about the section on asynchronous programming -- what might an alternative method look like, and why would it be "worse?"
The worse way would be:
That sends one student at a time, waiting for them to return, before sending the next.