官术网_书友最值得收藏!

Observer – an iterator's dual

Clojure's sequence operators, such as map, filter, reduce, and so on, support Java iterables. As the name implies, an iterable is an object that can be iterated over. At a low level, this is supported by retrieving an Iterator reference from such an object. Java's iterator interface looks like the following:

public interface Iterator<E> { 
    boolean hasNext(); 
    E next(); 
    void remove(); 
} 

When passed an object that implements this interface, Clojure's sequence operators pull data from it by using the next method, while using the hasNext method to know when to stop.

The remove method is required to remove its last element from the underlying collection. This in-place mutation is clearly unsafe in a multithreaded environment. Whenever Clojure implements this interface for the purposes of interoperability, the remove method simply throws UnsupportedOperationException.

An observable, on the other hand, has observers subscribed to it. Observers have the following interface:

public interface Observer<T> { 
    void onCompleted(); 
    void onError(Throwable e); 
    void onNext(T t); 
} 

As we can see, an observer implementing this interface will have its onNext method called with the next value available from whatever observable it's subscribed to, hence it being a push-based notification model.

This duality[1] becomes clearer if we look at both interfaces side by side:

Iterator<E> {                       Observer<T> { 
    boolean hasNext();                  void onCompleted(); 
    E next();                           void onError(Throwable e); 
    void remove();                      void onNext(T t); 
}                                       } 

Observables provide the ability to have producers push items asynchronously to consumers. A few examples will help solidify our understanding.

主站蜘蛛池模板: 安塞县| 克拉玛依市| 云南省| 曲麻莱县| 射洪县| 贵溪市| 蒙山县| 谢通门县| 开原市| 巧家县| 丰台区| 海阳市| 华蓥市| 和田市| 灵山县| 岢岚县| 宝应县| 洮南市| 舟山市| 建德市| 红桥区| 荣昌县| 亚东县| 黄冈市| 永嘉县| 柏乡县| 蒙自县| 东阿县| 怀远县| 南和县| 通榆县| 湄潭县| 寿宁县| 华坪县| 陈巴尔虎旗| 芷江| 兴安县| 普陀区| 县级市| 南安市| 增城市|