Thursday, December 25, 2014

Patterns and anti-patterns in building Akka systems

"Let it crash" Principle
  • Recovery should be automatic to restore normal service as soon as possible
Inverted control flow
  • Better to push jobs than to pull them
  • Reduce the frequency of blocking requests
  • Fit nicely with short jobs that comes at a fixed rate
  • Not an adequate pattern when
    • jobs are produced faster than finished
    • jobs are expensive and should be avoided if possible
  • Solution: push jobs after old jobs are finished
    • Eg. push with rate limit / acknowledgement
Durable mailbox
  • Actor system doesn't guarantee the delivery of messages per se
  • Have to use additional protocols to approach this purpose
    • ack everywhere
    • store to persist
  • Asking for guarantees in an uncertain world takes costs
    • increased effort / complexity / latency
    • additional dependency in the architecture
Event sourcing
  • A good way to compile a list of time-series events
  • Has to take care of replication inconsistency, application latency, etc
Non-blocking request
  • Future-based messaging
Sentinel
  • handling failure above a supervisor, flow control and distributed workers

Sunday, December 7, 2014

Cloud Design Patterns

http://msdn.microsoft.com/en-us/library/dn568099.aspx

"This book contains twenty-four design patterns and ten related guidance topics, this guide articulates the benefit of applying patterns by showing how each piece can fit into the big picture of cloud application architectures. It also discusses the benefits and considerations for each pattern. Most of the patterns have code samples or snippets that show how to implement the patterns using the features of Microsoft Azure. However the majority of topics described in this guide are equally relevant to all kinds of distributed systems, whether hosted on Azure or on other cloud platforms."

Friday, November 14, 2014

Type System in Java

Unified type system. In a unified type system, all types including primitive types inherit from a single root type. For example, C# is unified typed, as every type in C# inherits from the Object class. Java has several primitive types that are not objects. However, Java provides wrapper object types that exist together with the primitive types, so developers can use either the wrapper object types or the simpler non-object primitive types.

Type erasure. (to be completed later)

Generic types. Generics have been available in Java since J2SE 5.0 was released in 2004. Generics allow for parameterized types and hence allow the compiler to enforce type safety. For example, an instance of the Vector class can be declared to be a vector of strings, written as Vector<String>. To avoid major changes to the Java runtime environment, generics are implemented using type erasure. This amounts to removing the additional type information and adding casts wherever required. For example, there is no way to distinguish between a List<String> and a List<Long> at runtime. Since JVM doesn't track the type arguments of a generic class in the bytecode, both of them are of type List at runtime.

Arrays. Java’s arrays are parameterized types written in a form different from the generic types. For example, the array type String[] is analogous to the vector type Vector<String>. On the other hand, Java arrays are not subject to type erasure like generic types are, and this leads to inconsistency in the handling of arrays and generic types. One consequence of this is that the following code will not compile:
class Test<T> {
  public Vector getVector(){ return new Vector<T>(); } // ok
  public T[] getArray(){ return new T[10]; } // compile-time error
}
Since arrays don’t support type erasure, method getArray needs the type information of T to create the array. However, as Java generics are implemented using type erasure, the parameterized type T does not exist at runtime. Hence, the compiler cannot assign a type to the array and thus will raise a "generic array creation" error.

Type parameters. (to be completed later)

Type inference. Java doesn't support type inference for variable assignments. Hence, it requires that programmers declare the types they intend a method or function to use. However, it is not true that Java doesn't have type inference. Indeed, recent versions of Java supports type inference when using generics. In Java 7, one can get some additional type inferencing when instantiating generics like so
Map<String, String> foo = new HashMap<>();
Java is smart enough to fill in the blank angle brackets for us. Moreover, we get type inference in Java 8 as a part of lambda expressions. For example, consider
List<String> names = Arrays.asList("Tom", "Dick", "Harry");
Collections.sort(names, (first, second) -> first.compareTo(second));
The Java compiler can infer from the signatures Collections#sort(List<T>, Comparator<? super T>) and Comparator#compare(T o1, T o2) that first and second should be a String, allowing the programmer to omit the type declarations in the lambda expression.

The reason why Java doesn't allow type inference for non-generic types seems to be because of  its design philosophy. That is, programmers should write things explicitly to make sure that the compiler has the same understanding of the code as them do. Besides, Java was originally aimed at programmers coming from C++, Pascal, or other mainstream languages that did not have it. Thus, type inference was probably not supported due to the principle of least surprise.

Sunday, November 9, 2014

Spark for Beginners

Setup Spark enviornment on the local Ubuntu machine
http://blog.prabeeshk.com/blog/2014/10/31/install-apache-spark-on-ubuntu-14-dot-04/

Write Spark locally with IntelliJ and running apps on the remote cluster:
http://blog.csdn.net/Camu7s/article/details/45530295

Run Spark apps on Windows without installing Hadoop:
http://qnalist.com/questions/4994960/run-spark-unit-test-on-windows-7

Compile and install Hadoop on Windows:
http://stackoverflow.com/questions/18630019/running-apache-hadoop-2-1-0-on-windows

Install Dato on CentOS 6.4

Dato needs Python 2.7, while CentOS uses Python 2.6. So first you have to install Python 2.7 as an alternative build of Python on your system, as well as the libraries needed to compile Python modules:
sudo yum install -y readline-devel sqlite-devel bzip2-devel.i686 \
openssl-devel.i686 gdbm-devel.i686 libdbi-devel.i686 ncurses-libs 
cd /tmp

# install zlib manually because the default one for centos is too old
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make & sudo make install

# install Python 2.7.6
wget https://www.python.org/ftp/python/2.7.6/Python-2.7.6.tgz
tar -zxvf Python-2.7.6.tgz
cd Python-2.7.6

# IMPORTANT!
./configure --enable-shared --enable-unicode=ucs4

# IMPORTANT!
make & sudo make altinstall
You also need to install setuptools, pip, and virtualenv before running Dato. The installation steps however are standard, see e.g., here for details.

Saturday, October 11, 2014

Combiners in Hadoop MapReduce

A combiner function in MapReduce has the same form as the reduce function (and is an implementation of the Reducer interface), except its output types are the intermediate key and value types (K2 and V2), so they can feed the reduce function:
combiner: (K2, list(V2)) → list(K2, V2)
Often the combiner and reduce functions are the same, in which case K3 is the same as K2, and V3 is the same as V2. On the other hand, Hadoop reserves the right to use combiners at its discretion. This means that a combiner may be invoked zero, one, or multiple times. Hence, the correctness of a MapReduce algorithm shouldn't depend on computations performed by the combiner or depend on them even being run at all.

Sunday, October 5, 2014

Guidance for Scientific Writing

The Structure, Format, Content, and Style of a Journal-Style Scientific Paper

Typesetting mathematics for science and technology according to ISO 31/XI

According to the standard, constants like i and Euler's e, and the differential operator d should be upright. However, these typesetting rules seem to be ignored by many respected authors and publishers. See this thread for some interesting discussions on this issue: What's the proper way to typeset a differential operator? For those looking for an easy solution, one of the re-posts there suggests to typeset a math paper in Latex using package commath.

Monday, August 11, 2014

Scala: the unification church of Java

What Scala unifies in Java

Java has three namespace: package, method, and fields.
Scala unifies all terms under the "uniform access principle".

Java handles primitive types irregularly (eg. boxing and unboxing).
Scala unifies all types under a single-object model.

Java handles Arrays irregularly (eg. without type erasure).
Scala unifies Arrays with other collections.

Java handles void, null and non-termination irregularly.
Scala handles Unit, Null and Nothing in the type system.

What Scala diversifies from Java

Java has insufficient granular access control.
Scala creates a bunch of new access levels.

Java has single inheritance.
Scala allows you to have as many super-classes as you like.

Java has no bottom types.
Scala has Null and Nothing.

Java only has strict evaluation.
Scala has by-name parameters, lazy vals, streams, views, etc.

Java only has invariant type parameters.
Scala has definition-site variance.

Java doesn't support meta-programming.
Scala support macros (experimental).

Sunday, July 27, 2014

Wednesday, July 9, 2014

DOA Kokoro Skill Sheet

中距離攻擊

  • 1KKK → 66P → 4P+K [CB]
  • 2H+K → K → 9PP-2K-...
  • 2H+K → K → 6KP [CB] 
  • 1KK-P → 6KP → 2KP
  • 1KK4K 或 3K4K 或 P4KK [轉身] → K [轉正+浮空]

連續技

  • 66P → 1KK [CB] 
  • (66P) → 9K [小浮空] → 6PK → P2KP

浮空技

  • 正面:K → 9PP2KP (高浮空適用) 或 P2K (低浮空適用)
  • 正面:6KP → P2KP 或 9PP2KP (注意 6PK 在高浮空時容易 miss )
  • 正面:P+K → 9P → P2KP (第一步容易 miss)
  • 背面:4P → 9PP2KP (建議使用)
  • 背面:P6P → PP2KP 或 9PP2KP (P6P 後要按取消鍵,成功率較低)

特殊攻擊

  • 面牆攻擊(9P 避免直接撞牆):P2KP → 9P → P2KP
  • 閉肘攻擊:236T → P+K‧P [CB]
  • 背摔:4T → 4KP
  • 閉肘(P+K)造成進攻時間差,運用得當可增加對方反制的困難度。閉肘後可接 P+K 或 P 或 2P 攻擊,速度夠快或 CH 時有 CB 效果。
  • 可接閉肘的招式:8P、2H+K、66P+K‧P+K
  • 部分招式可接 T 破壞防守方的平衡,之後再接 P+K 或 P 或 2P 攻擊,速度夠快或 CH 時有 CB 效果。
  • 可接 T 的招式:PP、2K、PP2K、4PK、66P+K‧P+K

注意事項

  • 連續攻擊時用 6KP 收尾較易造成 CB。
  • 被動防守時(即按住 4 不放)較適合的反擊技為 7K 。
  • P2K 後可稍停頓,視對手動作採取 P 或 T 二擇。 
  • 對手在中距離發招時,可趁空檔用 214P 或 1P+K 把對方打飛。
  • 以下是同系列的動作,差別在出招速度和破壞力:
    快:2KP 中:P2KP 慢:7P2KP

Saturday, July 5, 2014

A History of Haskell: being lazy with class

I recently stumbled across a fascinating paper from the ACM SIGPLAN History of Programming Languages Conference (HOPL’III), 2007: 

http://www.scs.stanford.edu/~dbg/readings/haskell-history.pdf 

Related materials and talks about this paper can be found at Simon Peyton's page.

A collection of online Haskell tutorials:

Learn you a Haskell http://learnyouahaskell-zh-tw.csie.org/
Write you a Haskell http://dev.stephendiehl.com/fun/
What I wish to know when I learned Haskell http://dev.stephendiehl.com/hask/

Search Engines for Codes


✱ Sourcegraph 


Saturday, June 28, 2014

DOA Marie Rose skill sheet


  • 單一招式攻擊力低,須靠密集的攻擊和浮空後的打擊消耗對手體力
  • 移動範圍較大,藉 7P 脫離對方攻擊圈,輔以 3P+K 二擇突進攻擊
  • 中距離攻擊 P+K 適合硬撞,挑對時機成功率高,被擋下後破綻大
  • 高級技巧:小迴旋舞可讓對方攻擊時失去平衡,以創造己方反擊機會
  • 高級技巧:用專家 hold 及背摔可繞到對手背後攻擊 

浮空技

延長滯空時間:
  • 4K,H+K(正面),6K4K(正面),4K4K(背面)
  • 4K→6K4K(高級技巧,輕量級對手限定)
趁對方浮空時,施以連續攻擊:
  • 9P → 6PP → PP6PK(成功率較低,輕量級對手限定)
  • (6PP 或 9P) → PP6PK(輕中量級對手適用)
  • PPP → PP6PK(輕中量級對手適用)
  • (背對)KK → 9P → PP6PK(輕中量級對手限定)
  • (背對)KK → 6PK(重量級對手適用)
完整使用例:
  • 4K→(6K4K)→(6PP 或 9P)→PP6PK

連續技

開頭:
  • 遠距攻擊:6KK,3P,3PK,4PP,4PK,KK → 3P
  • 近身攻擊:H+K → 6P+K,4K → 4PK
之後可接:
  • 轉身背對:9K4 (→ P+K [會心一擊] → 7K → 浮空技)
  • 6P ( → P+K [會心一擊] → 7K → 浮空技)
  • PP8K [浮空]
完整使用例:
  • (6KK → 3P 或 4PK)→ 9K4 → P+K [會心一擊] → 7K → 浮空技
  • 3P → 3PK → PP8K [浮空] → 浮空技
  • 4K → 4PK → 4PK [浮空] → 浮空技

超遠距攻擊

當對方在自己攻擊範圍之外時,先使用飛身技衝到對方面前再施以連續打擊。
  • 飛身技:6KK,3P+K → K
完整使用例:
  • 6KK → KK → 3P [打飛] → 浮空技

背身攻擊

  • (6KK→3P 或 4PK) [突擊] → 6P4 → PP打飛 → H+K [追打] → 浮空技
  • 4P+K [架開對手攻擊] → 4T [背摔] → H+K [追打] → 7K → 浮空技

牆邊攻擊

連續技在對手撞牆之後會中斷,因此在撞牆之前盡可能延長打擊時間是重點。
  • PP8K [浮空] → PP6PK(輕中量級對手適用)
  • H+K [追打] → 8K [浮空] → PP6PK(輕中量級對手適用)
使用 7K 會直接使對方撞牆,因此使用較弱的 8K 讓對方浮空。

對手撞牆後未倒地時的追加攻擊:6K → PP6PK 或 6K4‧K → PP6PK

使用摔技的訣竅

  • 使用摔技之前,先用輕拳引誘對方防守再摔,可增加成功率和破壞力。
  • 防守時,趁對方出招空檔 4T 反摔成功率極高。
  • 使用小迴旋舞架開對手中上P之後背摔。注意:小迴旋舞時被摔會加重傷害。

其他

  • 利用 9K 或 P+K 避開對方起身攻擊。抓對方起身時機,用 3P+K → T 突襲。

Friday, June 27, 2014

Using Promises Instead of Callbacks

Stop using nested callbacks for async function calls in JavaScript!
https://blog.jcoglan.com/2013/03/30/callbacks-are-imperative-promises-are-functional-nodes-biggest-missed-opportunity/

A detailed comparison between various implementations of Promise:
http://complexitymaze.com/2014/03/03/javascript-promises-a-comparison-of-libraries/

Bluebird is albeit the fastest Promise implementation in the market.
https://github.com/petkaantonov/bluebird

JQuery already uses Promise in its async operations, e.g., get, post, ajax, etc.

Scala also has Promise, aka Future:
http://docs.scala-lang.org/overviews/core/futures.html

Saturday, May 24, 2014

DOA Terminologies

招式詳細數據:http://blueviolet.ninja-web.net/game/doa5lr/per/kokoro2.htm

:遊戲中的時間單位。現在的格鬥遊戲如 DOA、鐵拳等都是每秒 60 幀的畫面,所以在 DOA 的遊戲中,一幀就是現實中的 1/60 秒。

幀數:幀數就是從指令輸入直到招式發揮作用所需的時間。幀數愈低的招式速度愈快,速度快的招式壓制速度慢的招式,比如霞的 P 壓制巴斯的 P。當雙方招式在同一幀對上時,結果會看招式判定。判定高的招勝出,判定相同雙方會同時受到攻擊。通常上端攻擊比中段判定高,中段比下段判定高,下段又大多可以閃避上段攻擊,形成一個循環相剋的關係。

格鬥遊戲中都有硬直時間(硬直幀數)的概念,可以理解為收招時間或破防後的恢復時間。比如你發一招對手防住了或是被擊中,雙方都會產生短暫的硬直,硬直時間內輸入指令是無效的。加幀優勢幀的意思就是比對方更快從硬直中恢復,減幀劣勢幀就是比對手更晚從硬直中恢復。

比如你發一個防禦 -3 幀的招數,就是說被對手防禦會比對方晚 3 幀從硬直中恢復,但遊戲裡沒有發生幀數小於 3 幀的招數,所以這一招即便被對手防禦住,也不會有被立刻反擊的危險,這種招數就叫安全技。再比如你發一個防禦 +3 幀的招數,這招被對手防禦住後你會比對方早 3 幀從硬直中恢復,這種招數就叫做加幀技搶幀技。加幀技被防禦住後,攻擊方可以自主選擇要繼續追擊或是防禦對方返技。

再比如你發一招,這一招是防禦 -10 幀,意思就是這招被對手防禦後,自己會比對方晚 10 幀從硬直中恢復,那麼,這時如果對手出一個發生幀數小於 10 幀的招數,你就必中,這就叫確返。確返又分確投確打。投確投需要 1 幀(比如某招防禦後你有 5 幀優勢幀,這時只有 4 幀投能確投,5 幀投不能確投),確打需要 2 幀。因此確返時使用投技是比較有利的。

收招幀:收招幀指的招式打空或是被對手防禦住時,己方的劣勢幀。例如防禦 -10 幀的招式收招幀為 10 幀。每個招數有一個發出的幀數,幀數小的壓制大的,但是它也同時具備一個收招幀,如果收招幀大於對手投技的幀數,那麼它收招的時候就可以被對手確投。

自己角色常用招數的幀數要瞭解清楚,包括發出幀和收招幀,攻擊後是減幀還是加幀等,有的招數被防禦住還是可以加幀,這類招數就可以多練。

多擇:多擇就是一個技能的後段攻擊。比如說こころ的最強大擇 3K,3K 可以演變為 3KP、3K4K、3KK、3K2P ,這就表示她 3K 後面那一個動作打的可能是上中下任意一個部位。

起身:起身是這個遊戲的一個重頭戲,起身有很多方式,包括側向翻滾,後向起身,原地站起,特殊起身等,底下一個個解釋:
  • 後向起身:優點是不會被踩,如果對方踩的話我們會獲得一個絕對優勢幀,可以確投。也不會被掃地攻擊打中。缺點是不能起身攻擊,被對方貼緊時,可能站起來後是劣勢幀被繼續追擊。
  • 側向翻滾:優點是可以躲避一些掃地的攻擊,也可以起身踢獲得反擊機會。缺點是類似瑞秋的踩踏等倒地小攻擊可能躲不掉。
  • 原地起身:優點是可以起身踢。但是以上有的缺點原地起身都有。
  • 特殊起身:很多角色都有特殊起身,優點是可以攻對手措手不及。缺點是對方防禦住後容易確投。

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();


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/