Sunday, 17 June 2018

Climate Engineering

I've recently watched a pretty good documentary about Climate Engineering (the deliberate and large-scale intervention in the Earth’s climate system). I came across with the Spanish version, "Un Clima Mecanico" (not sure about the English title), but there seems to be quite a few other documentaries on this topic.

I thought the idea of voluntarily changing the climate or the weather for someone's benefit lied mainly in the realm of fiction (let's exclude Global Warming, that could benefit a few people at the expense of the rest of us), but indeed it was already done by the USA during the Vietnam War, in the Operation Popeye. Furthermore, it seems like the Soviet Union used to artificially clean up the skies of Moscow for the May 1st parades, amazing.

Modifiying the local weather for one or several days seems scary (causing huge storms that can damage crops, flood cities...) but modifying the climate would have a global effect, and that's terrifying. If you modify the climate to do your country more rainy others will become drier, and the same goes for the temperatures. Conflicting entities would end up in a traditional war... The documentary shows how the Soviets had a crazy plan for melting the Artic to benefit of its resources, it's sad to see that they just had to wait a few decades to let Global Warming do the job for them.

For the moment, Climate Engineering remains such a dangerous and conflictive topic that apparently governments are not supposed to promote research on it... but well, who knows what is really under the surface...

Agriculture 2.0

This documentary about Vertical Agriculture is just amazing. I used to think of "Urban Agriculture" mainly as a nice hobby with a social component. If you live in a flat in a city you need to be a bit lucky to enjoy that hobby. The options are:

  • You have a balcony (relatively common in France, totally uncommon in Asturies) and you can grow there a few tomatoes, strawberries and even have a small orange tree in a pot.
  • The building where your flat is placed has some sort of private garden (rather common in France, very uncommon in Asturies) and the neighbours have agreed in setting a growing area. This could also happen in your building's roof.
  • Some neighbours association has agreed with the city council to set up some growing area in some park or unused space (this happens sometimes in France, is unknown to me in Asturies).

Well, all the above is cool, but has nothing to do with the documentary. This documentary is about how to set up agricultural facilities in towns that would really help to feed the town regularly (with veggies that in many cases do not grow in the city's climate), not just a neighbours lunch. City space is rare and expensive, so the idea is taking the conventional greenhouse to its version 5.0! You can start by hanging up if possible different layers of growing veggies in one container and then move to setting multistories plantations. You can also skip using soil and move into Hydroponics. The concept envisioned by one Swedish entrepreneur where a building would be used for office space and agricultural space is beautiful.

What amazed me the most is how by fully controlling the weather in your container: temperature, light, humidity... you can grow anything maximising its nutrients and taste. If you know that certain harvest a certain year in a certain place was particularly tasty, and you have access to the weather patterns in that place that year, you can be close to replicating it. Agriculture is becoming the new IT freaks realm!

When in October 2016 I was travelling by train from Amsterdam Airport to Rotterdam I felt totally amazed by the huge extension of modern greenhouses. These had nothing to do with the ones covered in plastic that are so important in Southern Spain, these were glasshouses, like the ones in a botanical garden, but on a massive scale. This shows up in the documentary and happens to be called Westland.

Tuesday, 22 May 2018

Async/await with Animations

Years ago I had fun coding an animation matrix (the sample has been moved here). Most cases involved a sequence of animations, each one starting after the previous one had finished. It was one more case where I had to simulate an asynchronous loop, where at the end of each "iteration" I would callback into an "orchestrator function" that would launch the next iteration. I realized that now that we have async/await, this could be done in a much more straight forward way, so I've prepared some sample code.

For the kind of animation that I'm doing it would have been more simple to just animate divs rather than drawing squares on a canvas, but lately I've been playing around with particle systems on a canvas, and I've preferred to continue with the canvas route.

Whatever item we are animating, the thing is that starting the animation should return a Promise that will get resolved when the animation completes. For this sample I'm animating a square that falls from the top to the bottom of the canvas and bounces a couple of times until it stops. You can see it here. The code (yep, I've jumped into the TypeScript trend) looks like this:

abstract class AnimationItem{

 public abstract update():void;
 public abstract draw():void;
 public animate():Promise<void>{
  this.moving = true;
        return new Promise<void>((res, rej) => {
            this.resolveCurrentAnimationFunc = res;

//in our child, concrete class:
    public update():void{

        this.ySpeed += this.gravity;
        //this.logger.log("speed: " + this.ySpeed + ", pos:" + this.currentPosition.y + ", " + this.acceleration);
        this.currentPosition.y += this.ySpeed;
        if(this.currentPosition.y >= this.endPosition.y){
            //let's place it just in the limit
            this.currentPosition.y = this.endPosition.y;
            if(this.bounces > 0){
                this.ySpeed = -1 * this.ySpeed/2;
                this.logger.log("bouncing: pos:" + this.currentPosition.y + ", speed: " + this.ySpeed + ", " + this.acceleration);
                //stop it
                this.moving = false;

So we have an animate method that returns a newly created Promise and stores in our class a reference to the resolve handle of this Promise. The update method takes care of updating the position of the object and resolving the Promise once the final position is reached.

I'm animating several of these items (rows and columns), one after another. With the power of async/await our code is so simple as just writing normal loops like this:

for (let y=0; y<2; y++){
        for (let x=0; x<3; x++){
            let item:AnimationItem = new FallingSquare(ctx, 
                new Vector(x * size, 0), //currentPosition:Vector, 
                new Vector(x * size, canvas.height - (size * (y + 1))), //endPosition:Vector,
                0, //speed:number, 
            await item.animate();

I've uploaded along with the transpiled and bundled javascript file the original typescript source code and the source map, so to see the full code just launch the debugger!

Friday, 18 May 2018

Foreach Async Gotcha

Today I came up with an interesting gotcha with async/await. I transformed a normal for loop containing await calls into an Array.prototype.forEach call, and realised that it was not working properly. Let's see:

Let's say that we have this nice async loop:

//asynchrnous function returning a Promise
function formatTextAsync(txt){
    return new Promise((res, rej) => {
        setTimeout(()=> res(txt.toUpperCase()),

async function loop1(){
    console.log("loop1 start");
    let items = ["bonjour", "hi", "hola"];
    for (item of items){
        console.log( + " input: " + item);
        item = await formatTextAsync(item);
        console.log( + " output: " + item);
    console.log("loop1 end");

console.log("before main");
console.log("after main");

// before main
// loop1 start
// 1526678289508 input: bonjour
// after main
// 1526678291514 output: BONJOUR
// 1526678291515 input: hi
// 1526678293518 output: HI
// 1526678293518 input: hola
// 1526678295521 output: HOLA
// loop1 end

If we decide to move the loop body into an Array.prototype.forEach method call, the output is quite different:

async function loop2(){
    console.log("loop2 start");
    ["bonjour", "hi", "hola"].forEach(async (item) => {
        console.log( + " input: " + item);
        item = await formatTextAsync(item);
        console.log( + " output: " + item);
     console.log("loop2 end");

// before main
// loop2 start
// 1526678543105 input: bonjour
// 1526678543109 input: hi
// 1526678543109 input: hola
// loop2 end
// after main
// 1526678545109 output: BONJOUR
// 1526678545111 output: HI
// 1526678545111 output: HOLA

So in the second code, the next iteration is launched without waiting for the async call in the previous iteration to complete. This means that the async calls end up running in parallel rather than in sequence.

The explanation for this behaviour is quite simple. We've moved the loop body containing the async call into a new function that we've marked as async, but, is anyone awaiting for this new function?, the answer is No, so the next iteration is started without waiting.

We can guess that the implementation of Array.forEach is something along these lines:

Array.prototype.forEach = function (fn) {
    for (let item of this) { fn(item) }

In order to get the correct async behaviour that we have when using a normal loop, we would need something like this:

Array.prototype.forEachAsync = async function (fn) {
    for (let item of this) { await fn(item) }

Lisbon Documentaries

Some months ago I wrote about how a lovely city Lisbon is. I've recently watched 2 pretty interesting documentaries showing 2 complementary views of the city, and I've thought that it would be nice to share them here.

DW (Deutsche Welt) has some excellent documentaries in English, dealing with a broad range of topics: social, politics, technology, travel... This one shows a really interesting portrait of life in Lisbon. It's from 2013 I think, which seems to me like the time when Lisbon was starting to become a trendy city attracting foreigners not just for regular vacation, but for longer/permanent stays. In some of the amazing panoramas one can see some construction cranes, but little to do with the huge number of them that I could see last November. I really enjoy when I see in films or documentaries places that occupy a place in the memories of my heart, and this documentary has quite a few of them.

This one from 2018 by BBC puts its focus on the artistic side of the city and how it has been modelled by the Portuguese history. I share with the female host the fascination with the Mosteiro dos Jeronimos and its Manueline gothic. I remember how amazed and excited I felt in my first time there. On the other side, though I understand that they want to focus on Portuguese art, I find it hard to believe that their filming in the Museu Nacional de Arte Antiga has not included any reference to the most amazing work located there, the Temptation of Saint Anthony. For sure Hieronymus Bosch has little to do with the History of Portugal, but any art lover visiting Lisbon should not miss spending quite a while in front of this heartbreaking Triptych.

Thursday, 10 May 2018

La Charia au Paradis

Some weeks ago I watched La Charia au Paradis a quite disturbing documentary about Maldives, that small island country in the Indian Ocean. Until then my basic knowledge about these islands could be summed up in these few points.

  • An idyllic tourist destination
  • One of the places that was hit hard by the 2004 tsunami
  • One of those places particularly threatened by the sea level rise (the whole country could just disappear)

I had no idea about the politics of these islands, and though I had the impression that it was mainly a muslim country, I was not aware of particular problems caused by the "religion of peace". Well, I was quite far from the true. In the last years fundamentalist Muslims have taken control of the country and are imposing their madness to the rest of the population. The law system is based on the shariah, and Saudi Arabia has been more than happy to host and train Maldivian guys in the "true Islam" so that they can return to the islands to teach/impose their Medieval learnings to a population that until recently was for the most part rather moderate. Hum, lovely Saudi Arabia, exporting love an tolerance not only to the European "Quartiers Sensibles", but also to peaceful, idyllic islands.

For the moment, given that the whole country lives on tourism, shariah law is not applied to the foreigners that fill the touristic resorts, but that could be changing in the future. Maybe they are planning to replace Western amoral couples with Saudi families. In the meantime, if I were a tourist I would be a bit scared that one day one bunch of beardy Islamist could decide to raid one of the resorts...

Sunday, 29 April 2018

Promises and the Event Loop

I've been reading some documents/articles about the JavaScript event loop (particularly for node.js) and I've found the explanation for what I had noticed in this post regarding awaiting an already resolved promise and the "jumping out" of the async function.

This series looks particulary good. The documentation about the event loop tends to be confusing (particularly the node.js documentation, this discussion helps to clarify it a bit though), but this one seems rather clear (hope it's not simplifying things too much and getting things not fully correctly).

The main idea is that the event loop goes through several phases, traversing several queues where different events/handlers/callbacks have been enqueued. There are 2 "intermediate queues" with a sort of high priority, as they are checked several times per iteration (one time after the traversal of any of the other queues). One of these 2 queues is the Micro task queue. This queue is used for Promises, well, for Promises callbacks, I mean, the handlers attacched to a Promise via then or catch. These callbacks/handlers are added to that queue when the corresponding Promise is resolved. If you add the handlers to an already resolved Promise, the handlers are added to the queue. So contrary to Tasks in .NET, the callback is not executed immediately, but added to the queue. This is what explains the behaviour that I mentioned in my previous article. An example (I'm not using await this time, just then().

let p1 = Promise.resolve("hi");
p1.then(it => console.log("the result is: " + it));

console.log("after resolve and then");

p1.then(it => console.log("and again is: " + it));

// after resolve and then
// the result is: hi
// and again is: hi

You can see how the "console.log" is executed before the "then handler" that got added in a previous line to an already resolved Promise. This answer fully matches with my understanding of Promises and the queue.

When a promise fulfills or rejects, the promise library puts tasks onto the queue for all the attached then or catch handlers. (If a then or catch handler is attached after the promise fulfills or rejects, then the task is put on the queue immediately.)