Monday, May 5, 2014

Type Erasure in Scala and Java

  • Martin Odersky tried to introduce generics to Java without modifying the JVM by translating away generics source-to-source. He and others reworked a Java compiler based on this technique; their works eventually evolved to JDK 1.5.
  • Generics in Scala and JDK 1.5 (and later) has to use type erasure, because JVM does not understand generic types―specification of Java bytecodes does not allow defining parameterized type of variables. Any modification to the bytecode to support reified generics would break backwards compatibility.
  • While it is possible for Scala to compile to bytecodes that support reified generics, this comes at the cost of giving up interoperability with Java classes and this is the last thing the designers of Scala would do. 
  • On the other hand, C# supposes generics from 2.0. It does not have to use erasure because .NET started to accommodate generics since then. 

References

1. http://lampwww.epfl.ch/~emir/bqbase/2006/10/16/erasure.html
2. http://code.stephenmorley.org/articles/java-generics-type-erasure/

Saturday, April 12, 2014

Akka

http://www.toptal.com/scala/concurrency-and-fault-tolerance-made-easy-an-intro-to-akka

Tuesday, March 25, 2014

JavaScript Design Pattern Examples

Factory

function ObjectFactory(ctor) {
  var obj = {};
  return function() {
    if(typeof ctor.prototype === 'number')
      obj.__proto__ =  Object.prototype;
    else
      obj.__proto__ =  ctor.prototype;
    var ret = ctor.apply(obj, arguments);
    return typeof ret == 'object' ? ret : obj;
  }
}
function Point(x, y) { this.x = x; this.y = y }
// The following is equivalent to: var p1 = new Point(0, 0);
var p1 = ObjectFactory(Point)(0, 0);

Singleton

var createSingletonOf = function(ctor) {
  var singleton; 
  return function() {
    return singleton || (singleton = ObjectFactory(ctor).apply(null, arguments))
  }
}
var point = createSingletonOf(Point);
var p2 = point(1,2);
var p3 = point(3,4); // p3 === p2

Observer

var Observer = (function() {
  var listeners = [];
  return {
    listen: function(callback) { listeners.push(callback) },
    trigger: function(res) { listeners.forEach(function(fn) { fn(res) }) },
    observe: function(act) { this.trigger(act()) },
  };
})();

Adaptor

// ThermometerFahr is an adapter of ThermometerCels
var ThermometerFahr = function() {
  var thermoC = new ThermometerCels();
  this.setTemperature(tempF) { thermoC.setTemperature((tempF?32)/1.8) } 
  this.getTemperature(tempF) { return thermoC.getTemperature()*1.8+32 } 
}
var thermoF = new ThermometerFahr();

Decorator

Function.prototype.bind = function (scope) { // a helper function
    var fn = this;
    return function () {
        return fn.apply(scope);
    };
}
/* MethodProfiler class */
var MethodProfiler = function(obj) {
  this.obj = obj;
  this.timers = {};
  for(var key in obj) {
    if(typeof obj[key] !== 'function') continue;
    (function(method) {
      this[method] = function() {
        this.tick(method);
        var ret = obj[method].apply(obj, arguments);
        this.log(method, this.getElapsedTime(method));
        return ret;
      }.bind(this);
    }.bind(this))(key); 
  }
};
MethodProfiler.prototype = {
  tick: function(method) {
    this.timers[method] = (new Date()).getTime();
  },
  getElapsedTime: function(method) {
    return (new Date()).getTime() - this.timers[method];
  },
  log: function(method, time) {
    console.log('Elapsed time of executing ' + method + ' is ' + time + ' ms');
  }
};

// Demonstration of usage
var test_target = { foo:function() { alert('hi there') } };
var profiler = new MethodProfiler(test_target);
// profiler is a decorator of test_target
profiler.foo();


Saturday, March 22, 2014

Monday, February 3, 2014

Functional JavaScript

How experiences in Haskell help writing functional and reusable JavaScript code:
http://seanhess.github.io/2012/02/20/functional_javascript.html

PureScript: writing JavaScript like a Haskell maniac
https://github.com/purescript

Implementing and explaining notions in category theory using JavaScript:
https://jscategory.wordpress.com/