Friday 16 February 2018

Afflicted

Afflicted has come as a real, enjoyable, surprise. I'd hardly read the review, so I just knew about the "found footage" style. As the story unveils I was getting shocked and amazed, this is one of the most interesting and original approaches to the Vampire genre in a really long while. Derek is one young guy on vacation in Europe for his once in a lifetime (and probably last) trip, that has been turned into a vampire and has no idea of it until the symptons become evident. His reactions and those of his best friend sharing the trip with him, their attempts to try to survive on non human blood, and the despair when finding that only fresh human blood makes the trick....

The story is distressing and frenetic, the locations are nice (Italian coastal villages and Paris!) and the "camera in hand" technique contributes a lot to the anguish that one breaths through the film. By the way, I don't know why this film is considered a "found footage" film, the footage is not found later on, but transmitted in real time to social networks... "found footage" and "self-recorded, camera in hand" are not the same to me.

I've always had a soft spot for vampire stories, in particular those where they are presented as elegant, aristocratic, decadent creatures. (We Are the Night, Underworld...) Audrey, the female vampire that turned Derek, is pretty interesting. I would have loved that she had been more present in the film giving us a better glimpse of her life, but her short appearance at the end is probably the best moment of the film. She decided to turn Derek into a vampire as a gift when she realised that he had a serious health condition threatening to end his life at any moment. The genious moment is when she tries to calm him saying "yes, you have to kill to live, but you can choose who you kill". That's an amazing idea. Normally vampires are either evil ones feeding on any easy victim, or good ones that manage to survive on blood intended for transfusions or some sort of synthetic one. Here we are given the perfect option, leverage that need to kill to eliminate those humans that do not deserve to be alive. Buff, I could immediately think of me as a vampire in Paris (or Toulouse, Marseille, Brussels...) spending the nights bleeding to death as many Salafists as I could find. Buff, in a few years radical Islam would no longer be a threat to our Civilization :-D

Thursday 15 February 2018

L'Insulte

I pretty much enjoyed watching The Insult in the cinema last week. Being a Lebanese film, from a "political" perspective I would prefer it to have been shot in French as original language rather than Arab, but I was lucky that it was the contrary, cause that way it was screened in Arab with French subtitles. With my French (lack of) level, reading the subtitles is much easier than trying to understand spoken French...

Long in short the film is about how a small dispute between a Lebanese Christian and a Palestinian refugee degenerates into a serious problem that ends up turning into a state affair. During this course, it succeeds in showing us the chaos in which Lebanon has lived (and continues to live) for so long. If you think that the conflict in Lebanon is just a matter of Christians vs Muslims, you're pretty wrong, cause there's much more to it: Christians, all the different Muslim sects (Sunnis and Druze, Shia and Alewite), Leftists and particularly important, Palestinians.

When the film starts, the Christian is the bad guay, and the Palestinian the good guy (what unusual...) but as the film evolves and the past is brought to the present, things get much more complex and quite less dichotomic. The deep disdain that the Christian character feels for the Palestinians stems from the fact that he and his family are also refugees in their own country. At the beginning of the Lebanese war alleged Palestinian forces looted and massacred the Christian village where he lived. Everybody knows about the Palestinian drama, but very few people know about the Christian drama, and this is salt over open wounds. As the lawyer says during the trial "no people should have the monopoly of suffering".

The film also reflects how hard is to this day the life of Palestinian refugees in Lebanon (this documentary shows it quite well) but it could give the impression that it's just Christians who are against them. I think this is something beyond religious lines (bear in mind also that some Palestinians are Christians or even non religious), and is much more a Lebanese against "invaders" issue. Bearing in mind that the Presence of Palestinian forces (after Black September) in Lebanese soil was the last (but pretty important) drop that led to the Lebanese civil war, one could understand (but not justify) a certain "animosity" towards Palestinians and a desire of putting them out of the country...

I'm aware of few films about the Lebanese war, basically, the magnificent Incendies and the also rather good Waltz with Bashir, but it seems I can't find any recent documentary about the war itself (not about its consequences in nowadays Lebanon).

Wednesday 7 February 2018

Destructuring/Deconstructing

Destructured assignment (simplify how to return multiple values from a function and assign them to the corresponding variables) is a feature that I had really missed in both C# and Javascript, and it has been added almost at the same time, with a different name and with different behaviour. I'll write a small summary here.

In C# the feature is called Deconstruction and you can use it out of the box returning the new ValueTuple type from your methods. ValueTuple has builtin support for deconstruction. For other types, in order to allow deconstruction the class will have to provide Deconstruct method/s. In both cases, there's a single syntax for the assignment "()". So the thing is like:

static (string name, string city, int age) GetData()
{
 return ("Didier", "Toulouse", 45);
}

var (name, city, age) = GetData();
//as the returned tuple has names I can switch the order
(city, name, age) = GetData();

// Compilation Error: Cannot deconstruct a tuple of '3' elements into '2' variables
// (name, age) = GetData();

//so we have to use a discard
(name, _, age) = GetData();

//we can use different names
var (n, c, a) = GetData();

As you can see in the example if you want to assign less variables than the elements in the returned tuple you can use "_" (called discards). Discards make much sense when the variable names you use and those in the returned tuple do not match, or when the returned tuples is not using names, but in the example above, I don't see the reason for getting that Compilation Error and needing the discards, but well, I guess I'm missing something.

In Javascript the feature is called destructuring and we have 2 different syntaxes, "{}" for object destructuring and "[]" for array destructuring. Some basic example:

function getAddress(){
 return {name: "Marseille", population: 1600000};
}

let {name, population} = getAddress();

//-------------------------

function getCities(){
 return ["Paris", "Toulouse", "Marseille"];
}

let [capital, aero, mediterranean] = getCities();

I think there's not much to say about Object destructuring, but for Array destructuring the main thing to bear in mind is that it can be used for any iterable object, not just for arrays (so probably it would be more clear to call it "destructuring with array syntax"). This means that if we want to implement destructuring in one custom object, same as in C# we use Deconstruct methods, in javascript we'll use the iterable protocol. Let's see an example of how the same object is destructured differently with the object and array syntaxes.

let obj = {
 name: "Francois",
 city: "Marseille"
};

obj[Symbol.iterator] = function* () {
 yield "item1";
 yield "item2";
 yield "item3";
};

//object destructuring, can not be customised 
let {name, city} = obj;
console.log("name: " + name + " city: " + city); 

console.log("----------------");

let [a, b] = obj;
console.log("a: " + a + " b: " + b); //a: item1 b: item2


//we can use "discards" ( ,)if we want access to only certain elements (with "holes" in between)
[a, , b] = obj;
console.log("a: " + a + " b: " + b); //a: item1 b: item3

As shown above, discards in javascript use the " ," syntax rather than "_". One interesting feature present in javascript and missing in C# (as far as I know)is destructuring into function parameters.


function printAddress({street, number, city}) {
 console.log(street + ", " + number + " (" + city + ")" );
}

let address = {
 street: "Republique", 
 number: 45,
 city: "Paris"
};

printAddress(address);

Sunday 4 February 2018

Structs in C#

I've never been particularly interested in using structs rather than classes in C#. I guess it comes from the reasoning "JavaScript, Python, Groovy... do not have them, so why should I care?". They are available mainly (or only) for performance reasons that probably in most cases should not hit us, so why should we care too much?

The fact that the new Tuples introduced in C# 7 (ValueTuple) that make so nice to work with multiple return values are structs rather than classes has slightly woken up my interest in them. I remember that one important performance feature is that structs lack the Object header that all reference types (instances of a class) have. This header is 8 bytes or 16 bytes depending on whether you are in a 32 bits or 64 bits system. If you are working with many instances of that class or struct, this can make a huge difference.. Another thing that had in mind is that assignment and modifications can be a bit tricky. I've been reading about it, and doing some minor tests, so I'll write a short summary. This entry in MSDN already gives us most of what we need

A struct is a value type. When a struct is created, the variable to which the struct is assigned holds the struct's actual data. When the struct is assigned to a new variable, it is copied. The new variable and the original variable therefore contain two separate copies of the same data. Changes made to one copy do not affect the other copy.

Value type variables directly contain their values, which means that the memory is allocated inline in whatever context the variable is declared. There is no separate heap allocation or garbage collection overhead for value-type variables.

Heap vs Stack. When you declare a variable of struct type, the struct will be created directly in the stack. var myStruct = new MyStruct();
However, when a class has a struct field or property, the struct is stored in the Heap. The difference here with normal classes is that the field will get the struct inlined right there, rather than being a reference to another memory location.

Given that structs are either directly located in the stack or inlined, assignments are based on copying. We are doing copies of the struct when we assign a local struct variable to another, or we assign it to a struct field or property or we pass it as parameter. Let's see some examples:

    struct Address
    {
        public Address(string city, string street)
        {
            this.City = city;
            this.Street = street;
        }

        public string City;
        public string Street;

        public override string ToString()
        {
            return this.City + ", " + this.Street;
        }
    }

    class Person
    {
        public string Name {get;set;}
        public Address Location {get;set;}

        public Person(string name, Address location)
        {
            this.Name = Name;
            this.Location = location;
        }

        public override string ToString()
        {
            return this.Name + " - " + this.Location.ToString();
        }
    }

 
    //main
            var address1 = new Address("Marseille", "Port Vieux");
            //a copy of address1 is done and assigned to address2
            var address2 = address1;

            address1.Street = "Rue de la Republique";

            Console.WriteLine(address1.ToString()); //Rue de la Republique
            Console.WriteLine(address2.ToString()); //Port Vieux

            Console.WriteLine("----------------");
            
            //when we pass the struct as parameters its also a copy what gets passed
            var p1 = new Person("Francois", address1);

            Console.WriteLine(p1.ToString());
            //Republique

            address1.Street = "Rue du Temps";

            Console.WriteLine(p1.ToString());
            //Republique

            //with this assignment we are doing another copy
            p1.Location = address1;
            Console.WriteLine(p1.ToString());

            address1.Street = "Rue de la Liberte";
            Console.WriteLine(p1.ToString());
            //Temps

There's and important difference between modifying a struct through a field or a property. If my class has a struct property and I modify one of its fields, that struct itself is modified inline, I mean, given:

struct Address
    {
        public Address(string city, string street)
        {
            this.City = city;
            this.Street = street;
        }

        public string City;
        public string Street;

        public override string ToString()
        {
            return this.City + ", " + this.Street;
        }
    }

   class Container
    {
  public Address AddressProp {get;set;}
        public Address AddressField;
        
        public override string ToString()
        {
            return "Prop: " + this.AddressProp.ToString() + " - Field: " + this.AddressField.ToString();
        }
    }

We can do this instance.structField.field = value;

            var container = new Container();
            container.AddressField = new Address("Lyon", "Rue Victor Hugo");

            Console.WriteLine(container.ToString());
            //here I'm modifying the struct contents, the inlined values
            container.AddressField.Street = "Rue du Rhone";
            Console.WriteLine(container.ToString()); //Rhone

However, if it is a property, we'll get a compilation error instance.structProperty.field = value;

  container.AddressProp = new Address("Paris", "Boulevard Voltaire");
  //this line does not compile!!!
  container.AddressProp.Street = "Rue de Belleville";
 //Error: Cannot modify the return value of 'Container.AddressProp' because it is not a variable

Getting such compilation error makes sense. When accessing the property it's returning a copy that I'm not assigning, so the ensuing assignment is useless and the compiler just prevents it.

On the other side, both for properties and fields if I assign them to a variable I'll get a copy of the original struct that I can modify with no issue.