Daily Learnings (June 1,2016)
1. Not using array.map to instantiate an array
I wanted to create an array of length 5, and then replace each value in the array with another value:
var foo = new Array(5);
foo = foo.map(function(){return 1;});
console.log(foo); // [null,null,null,null,null]
I had expected foo to be [1,1,1,1,1]; instead, it came back as [null,null,null,null,null]. I did a bit of digging and realized that the .map Array prototype function only processes non-null array values. Hence, the following would hold true:
var foo = [null,1,null];
foo = foo.map(function(){return 10;});
console.log(foo); // [null,10,null]
In order to replace the array’s null values, the following 2 approaches seem to work quite well (more info here):
1. Normal for-loop
var foo = new Array(5);
for (var i=0;i<foo.length;i++){
foo[i]=1;
}
console.log(foo); // [1,1,1,1,1]
2. Array.apply
var foo = Array.apply(null, new Array(5));
foo = foo.map(function(a){return 1;});
console.log(foo); // [1,1,1,1,1]
2. Scientists have tested a way to implant memories in mice
I’ve recently started to read a wonderful book, Future of the Mind by Dr. Michio Kaku, which details how technology has been able to understand and enhance the mind in recent decades. The book is chock full of great anecdotes and incredible scientific feats.
The latest experiment that blew me away was the ability for scientists to detect and capture the electric signals that go through a mouse’s brain when it learns to do a new task (such as pulling a lever), and then injecting that same electrical pattern into another mouse’s brain. Using this approach, scientists have observed the second mouse to be able to “remember” how to do the same task, despite never doing it before. As the book noted, this could be a precursor to being able to “upload” memories and skills into the brain, a la the Matrix.
3. WaitButWhy manages to articulate a strange emotion
One of my favorite blogs, WaitButWhy, posted a new article on “Clueyness”, which describe the sadness that one feels based on one’s imagined outcome of an event rather than the actual event itself (they describe it much better). A very fun, quick read!