Posts

The Ballad of Nick Drake

Image
Music history is littered with the dead, many of whom died young from suicide or vices.   It is therefore always interesting to wonder what if:   what if this artist had lived on?   How would he or she have changed the musical landscape? English folk musician Nick Drake was someone who could have changed the world of late 1960s-1970s poet-balladeers.   In fact, he did change the world just in his too short span of years and three albums.   There are no live recordings of Drake, no concert films.   Just his albums— “Five Leaves Left” (1969), “Bryter Layter” (1971), and “Pink Moon” (1972)—and various demos he made containing some new material or alternate recordings of the album cuts.   He died in 1974 at the age of 26 from an overdose of anti-depressants.   There is some discrepancy over whether he was a suicide or an accidental death as he was being treated for lifelong depression and mental illness. Drake’s music was rediscovered in the 1990...

HTML templating with ES6 template strings

Despite their name, template strings in ECMAScript 6 (ES6) are more like a different kind of function call than a mechanism for defining templates. This blog post explains how you can still use them for HTML templating. It is based on an idea by Claus Reinke . If you are not familiar with ES6 template strings, you may want to consult [2] before reading on. Defining and using a template You define a template as follows. It relies on the template handler html (a function that we’ll look at later). const tmpl = addrs => html` <table> ${addrs.map(addr => html` <tr>$${addr.first}</tr> <tr>$${addr.last}</tr> `)} </table> `; The trick is that the the inside of each substitution ${} can be an arbitrary expression. We use map() to create an array of strings inside the first substitution (which html() turns into the appropriate string). Thanks to arrow functions [3] , the callback of...

ECMAScript 6 sets: union, intersection, difference

Check out my book (free online): “ Exploring ES6 ”. Updated version of this blog post: section “ Union, intersection, difference ”. A recent question by Dmitry Moskowski reminded me: ECMAScript 6 sets have no methods for computing the union (e.g. addAll ), intersection (e.g. retainAll ) or difference (e.g. removeAll ). This blog post explains how to work around that limitation. Union Union ( a ∪ b ): create a set that contains the elements of both set a and set b . let a = new Set([1,2,3]); let b = new Set([4,3,2]); let union = new Set([...a, ...b]); // {1,2,3,4} The pattern is always the same: Convert one or both sets to arrays. Perform the operation. Convert the result back to a set. As explained in [1] , the spread operator ( ... ) inserts the elements of something iterable (like a set) into an array. Therefore, [...a, ...b] means that a and b are converted to arrays and concatenated. It is equivalent to [...a].concat([...b]) . Intersection Intersection ( ...

Anxiety

Image
“So how are we to wake up from the trance and dissolve the paradox of the ego?   It all comes down to the fundamental anxiety of existence, our inability to embrace uncertainty and reconcile death.” Maria Popova brainpickings.org It was a dark night with forbidding clouds hanging low in the sky.   I wanted to get to the car wash after work to have my car scrubbed down and thoroughly cleaned because over the weekend, when I was filling the gas tank, the nozzle popped out of the tank and spewed gas all over me and the side of the car.   I thought I had read somewhere that gasoline ruins auto paint.   I had scrubbed it with soap and water when I got home, but I could still smell the fuel and I wanted to make sure all traces were removed. The car wash was dark when I pulled in, but the gates were still open. I rolled to a stop near the vacuums and immediately noticed that the hoses were disconnected.   A man sat nearby in the shadows wearing all black and with hi...

Long Life

Image
“Here you are, alive.   Would you like to make a comment?” Mary Oliver Here it is the dead of winter and I am reading Mary Oliver .   What a joy it is.   Her book of tiny, jewel-like essays is called Long Life:  Essays and Other Writings (Da Capo Press, 2004).   The essays are also sprinkled with poems previously unpublished, and although I prefer her poetry to her prose, I am finding both meaningful and exquisite on this January afternoon.   She is a writer profoundly influenced by Thoreau and Emerson, with notes of Emily Dickinson.   She is fine wine indeed. Does this not sound like a Transcendentalist?   “For me it was important to be alone; solitude was a prerequisite to being openly and joyfully susceptible and responsive to the world of leaves, light, birdsong, flowers, flowing water.”   Or this:   “Man finds he has two halves to his existence:   leisure and occupation, and from these separate considerations he now looks u...

ECMAScript 6: maps and sets

Check out my book (free online): “ Exploring ES6 ”. Updated version of this blog post: chapter “ Maps and Sets ”. Among others, the following four data structures are new in ECMAScript 6: Map , WeakMap , Set and WeakSet . This blog post explains how they work. Map JavaScript has always had a very spartan standard library. Sorely missing was a data structure for mapping values to values. The best you can get in ECMAScript 5 is a map from strings to arbitrary values, by abusing objects. Even then there are several pitfalls that can trip you up. The Map data structure in ECMAScript 6 lets you use arbitrary values as keys and is highly welcome. Basic operations Working with single entries: > let map = new Map(); > map.set('foo', 123); > map.get('foo') 123 > map.has('foo') true > map.delete('foo') true > map.has('foo') false Determining the size of a map and clearing it: > le...