Saturday 28 January 2017

Collapsing South Korea

I'm a terribly eurocentric person, so even when I'm a freak of worldwide geography/history/society/politics, most of my reading and documentary watching is focused on Europe or on whatever threatens her. In the last years, my interest for the French present and future has made me also pay rather attention to Maghreb and la Francophonie in general. Saying this, my knowledge about South Korea (and East Asia in general) is pretty minimum, so my vision limits itself to something like this:
A modern country with a strong economy based on its technological giants (Samsung, LG...), with a highly qualified (and hardworking) population, educated in a too competitive and exhausting system. A country still at war (though in pause since the USA intervention/invasion, a peace treaty has never been signed) with its northern crazy brother, with a massive metropolis called Seoul, with pretty beatiful women, and with a distinctive culture that has managed to survive the meddling from her too big, and for most of the history unfriendly, neighbours, China and Japan.

I guess most people will share a similar vision. Technological giants, highly qualified population, no religious problems, apparently not known for suffering serious natural catastrophes... With all the problems that European societies are experiencing: the economy in southern Europe, the islamist invasion and radicalisation (along with its "tendency" to delinquency) in France, Belgium... the rise of far-right extremism... one could think of South Korea as a pretty good place to live. Well, I've been watching some documentaries lately and it does not seem to be so nice.

I already mentioned above that South Korea seemed to me as a rather competitive society, but I thought this was mainly during the educational phase. Watching this documentary it seems to be at all levels of society. There are more and more koreans (from students to highly priced professionals of any sector) going crazy to find whatever job abroad that allows them to flee the country and move into a "normal life". It comes to mind how nice it would be to kick out of France any Salafist piece of shit, or anyone with an important criminal record and replace him/her with a friendly atheist/Christian/Buddhist, law abiding South Korean folk. One could only dream of seeing any "quartier sensible" were Islamists and dealers reign supreme transformed into a "Petit Seoul". Furthermore, with all these guys working and paying taxes rather than living off our taxes and collapsing the social security system, maybe there would be money to finally bring the high speed train to Toulouse!!!

If thinking about alcohol problems, Russia and Finland came immediatelly to my mind. I also knew that Japanase tend to drink quite a lot when they quit the office, but I had no idea of how bad the problem is in South Korea. This documentary is just terrifying. After a stressful and hardly competitive working day good part of the population can not find better way to relax that to gather around massive amounts of alcohol, gatherings where competition continues, this time to see who manages to drink more and more.

I'm quite aware of the problems of an aging society. Asturies has one of the lowest birth rates in the world which combined with low immigration and high emigration has produced a very strong aging of society. Japan is well known as the fastest aging society in the world (very low fertility rate combined with a rather unfriendly immigration policy), but it seems South Korea is facing a huge problem with this also. I don't know about its immigration policy, but fertility rate is pretty low and furthermore young people are running away!

Much worse than being an aging society is the fact that a huge percentage of the elderly live in misery. This documentary is really shocking. South Korea did not have until 1988 a pension system, so many people were not in the system for time enough to be entitled to a normal pension and now have to survive on some minimum social benefits. In the past this was not a problem as children would take care of their parents, but in a growingly individualist society where family links just disappear, this is no longer the case and the situation has turned into a real drama.

So next time you feel depressed for how decadent and collapsing our European society seems to your eyes, think that other seemingly thriving societies are also in the shit

Pd: I'm a bit ashamed of having linked stuff produced by that Qatary channel. I profoundly hate that shit country that has been funding salafist scum all over the world and I can not wait for the day when their petrol and gaz economy will collapse, to see them back on their knees, back on what they are, a bunch of illiterate extremists living in a fucking desert... This said, I have to admit that for topics where Islam does not have any sort of realation, the channel has some rather good documentaries.

Friday 27 January 2017

Typed Serialization in Javascript

I had written some time ago about "typed serialization" in C# and Perl. What I mean is serializing an object with information about its type, and using that information later on to deserialize it into an instance of the correct type, rather than having that type "hardcoded" in code. I'll describe here one technique to achieve this in modern javascript.

The builtin JSON.stringify and JSON.parse are more than enough for my data serialization needs. Problem is, what about behaviour? JSON.parse will just create a plain object and add data to it, but we want our methods back! Assuming that the methods for your object are in its internal prototype [[Prototype]] (or further up in the prototype chain), and not directly attacched to the object itself, you need to set the [[Prototype]] of the new object accordingly. You can use for that Object.create or the more recent Object.setPrototypeOf.

So let's follow the whole procedure

First we have to serialize our data along with the type information. We'll just wrap our object in another object with that info, like this

 function serialize(obj){
  let aux = {
   typeName: obj.constructor.name,
   data: obj
  };
  return JSON.stringify(aux);
 }

When deserializing we'll have a string with the name of the "type" (the name of the constructor function for our object). To obtain the real function we can use the magic of eval:

function getFunctionFromStringName(functionNameSt){
 eval("var func = " + functionNameSt + ";");
 return func;
}

Once we have the function object, we can assign its prototype to our object, either this way:

//this does not work, Object.create takes a map of property descriptors
  //return Object.create(constructorFunc.prototype, aux.data);
  var obj = Object.create(constructorFunc.prototype);
  Object.assign(obj, aux.data);

Notice the comment. We can not just use the object with the data just deserialized because Object.create expects a bunch of property descriptors rather than simple data.

or this way:

 Object.setPrototypeOf(aux.data, constructorFunc.prototype);

I've put the code in a TypedSerializer class in this gist along with some test code.

Saturday 21 January 2017

Closures and this

The other day, when reminded of the fact that ES6 arrow functions trap the lexical "this" rather than using a dynamic "this" (one can think of them as bound functions, though I think they are not exactly the same), I tried to refresh my mind on the internals of how C# anonymous methods trap "this" (it's trapped, so it's a lexical "this" rather than dynamic), and it took me to some interesting new finding.

Let's say you write an anonymous method using some of the external local variables and "this". We need to closure on them, to trap them.

public class Person
{
 public string Name{get;set;}
 
 public void DoSomethingWithThisAndFreeVar()
 {
  var counter = 0;
  //this closure traps both a variable and "this". Its method will get created in a separate, auxiliar class, with a __this and counter fields
  Action f1 = () => {
   counter++;
   this.Name += ".";
  };
  f1();
 }
}

In this case the smart compiler will create a method in an auxiliar class that will have fields supporting the local variables, and a __this field pointing to the "this" field of our normal class. This is what I remembered and it's clear.

Now let's say we have an anonymous method that only makes use of "this".

public class Animal
{
 public string Name{get;set;}
 
 public void DoSomethingWithThis()
 {
  //this closure only uses "this", so in this case its method will get created inside this class itself
  Action f1 = () => { 
   this.Name += ".";
  };
  f1();
 }
}

As we know, when a delegate is created it gets a Target property that points to the instance of the class where the method for the delegate is created ("this"). So, in this case we don't need an auxiliar class, the method for the anonymous method can be created just inside our normal class. It makes sense to think that the C# compiler will distinguish between the previous situation and this one and emit different code. And of course that's right as we can see.

Maybe one could think why in the first case the compiler does not do the same. Put the method in the existing class and add auxiliar fields for the local variables. That would make no sense at all. First we would be adding a lot of rubbish data to the class, data that would be around taking memory as long as the object exists, while the auxiliar classes for the closure are usually short lived. Second, we could have multiple instances of that closure at the same time, that would want to share the "this" but trap their own local variables, so obviously we can not have them as fields in the main class.

Sunday 15 January 2017

Virtual Revolution

There has been a limited screening (just a single session) of this film (Virtual Revolution) in the American Cosmograph. The review in their monthly booklet was rather good, so it seemed like a good candicate for my first visit to the cinema in this new year.

Let's start by the bad parts. I guess the film was done on a pretty low budget, and the effects and imagery show that. The acting is poor in general, the characters are rather uninteresting, the aesthetics are a bit poor (the idea of a Paris-Metropolis is nice, but the result could have been better), the story is weak... but I still think that it deserves a watch. Why then?

Because the main idea is pretty good. Years ago I was almost obsessed with films/stories about virtual reality (Matrix, The Thirteenth Floor). This film adds an extra twist to the "disconnect from the real world and enjoy a virtual lie" utopia/dystopia. Here people connect voluntarily to the virtual reality (OK, it's nothing new, while in Matrix or most people were unaware of being out of reality, in The Thirteenth Floor people connect voluntarily), and this is encouraged by the government. This is the interesting part, this is not Matrix, the connected people are not producing energy or anything for the state, so why is it still promoting it?

Well, one case if obvious. There are people that are considered hybrids, they spend part of their time in the real world, they work and have a normal life, and then they connect to the virtual world in search of the pleasure they can not find in the real world. Sure all of us know someone that to a certain extent does that with World o f Warcraft and similars. This kind of hybrid is the perfect solution for a government to avoid discontent and protests. If your salary does not allow you to fulfill your basic aspirations (travelling, a decent house, a certain respect or recognitiion from others) you can get it in the virtual reality, and as long as you get it there you'll remain calm, will not revolt and the machine will continue to work. Indeed, this kind of virtual reality plays a role similar to the one of religion in the past. People would manage to endure a painful existence thinking about the other life.

The second case is not so obvious. There is a second kind of connected folks, the ones that spend almost their whole time connected. Basically they do nothing in the real world, just eat (badly) so that their body can still work and continue to connect. So, if they do not produce anything, and even cause some expenses to the state that is giving them some minimum housing and feeding, why are they useful for the state?

I can think of several reasons:
Let's assume that there is a level from which unemployment and non active population can not go lower, due to structural issues in the economic system, folks working in types of jobs that have been made obsolete by technological advances and that can no longer adapt to a new profession, disabled people or people with some small disability (physical, psycological...) that in reallity prevents them from finding or keeping a job... If this people spend their life in a virtual work their economical needs are pretty low, so the state can give them a pretty minimum salary and they will not complain as they are happy in their alternate reallity. Furthermore, their unhealthy lifestyle (poor diet, no physical excercise, no care about conducting regular analysis or visiting the doctor before things turn serious...) will notably reduce their life expectacy, so they will not be living off the state for too long.

Our planet is already overpopulated and population growth has not done but to increase. Any smart government knows this and knows how a big threat it is to humanity, but there are no easy solutions. Trying to impose the Chinese "1 child policy" at a global level would cause massive revolts. One option would be to reduce the fertility of the population by means of toxic elements in diet (it's already happening with sperm quality), but this would work at a global level, it would sound better to reduce the fertility of only certain population groups (and allow the intellectual elites to reproduce freely). The virtual reality options is simple and effective. The folks that decide to live in a virtual world and turn their back on reallity are not going to find a partner and start a family in the real life, so you've found a pretty good way of applying a "0 child policy" to a sector of the population characterized by not being particularly smart (indeed we could say that it's a pure example of natural selection)