ReactiveX

http://reactivex.io/

An API for asynchronous programming
with observable streams

The Observer pattern done right

ReactiveX is a combination of the best ideas from
the Observer pattern, the Iterator pattern, and functional programming

Easily create event streams or data streams.
Compose and transform streams with query-like operators.
Subscribe to any observable stream to perform side effects.
 
 
http://reactivex.io/intro.html

ReactiveX is a library for composing asynchronous and event-based programs by using observable sequences.

It extends the observer pattern to support sequences of data and/or events and adds operators that allow you to compose sequences together declaratively while abstracting away concerns about things like low-level threading, synchronization, thread-safety, concurrent data structures, and non-blocking I/O.

The ReactiveX Observable model allows you to treat streams of asynchronous events with the same sort of simple, composable operations that you use for collections of data items like arrays. It frees you from tangled webs of callbacks, and thereby makes your code more readable and less prone to bugs.

http://reactivex.io/documentation/observable.html

In ReactiveX an observer subscribes to an Observable. Then that observer reacts to whatever item or sequence of items the Observable emits. This pattern facilitates concurrent operations because it does not need to block while waiting for the Observable to emit objects, but instead it creates a sentry in the form of an observer that stands ready to react appropriately at whatever future time the Observable does so.

ReactiveX中文

https://segmentfault.com/a/1190000003632186

响应式编程是一种面向数据流和变化传播的编程范式,数据更新是相关联的。比如很多时候,在写界面的时候,我们需要对事件做处理,伴随着前端事件的增多,对于事件的处理愈发需要更加方便的处理。

设想一下,平时在处理事件的时候,一单上了复杂度,比如输入的时候,需要停止输入的时候才进行,这个时候又只能输入长度大于2才进行事件,当还是之前的数据的话不进行事件,可以考虑一下这个情况下如何去写。

例子

var keyup = Rx.Observable.fromEvent($input, 'keyup')
.map(function (e) {
return e.target.value;
})
.filter(function (text) {
return text.length > 2;
})
.debounce(750)
.distinctUntilChanged();

三秒后解除

var btn = document.getElementById('button');
var logRun = Rx.Observable.fromEvent(btn, 'click')
.merge(Rx.Observable.timer(3000))
.subscribe(e => {
console.log('run!');
logRun.dispose(); // 如果是一次性的就移除observable
});

ReactJS语言实现

https://github.com/Reactive-Extensions/RxJS

例子如上。

教程!!

http://xgrommx.github.io/rx-book/why_rx.html

为什么要使用 ReactJS, 因为 其将 Promise 和 DOM 作为一个整体对待。

One question you may ask yourself, is why RxJS? What about Promises? Promises are good for solving asynchronous operations such as querying a service with an XMLHttpRequest, where the expected behavior is one value and then completion. The Reactive Extensions for JavaScript unifies both the world of Promises, callbacks as well as evented data such as DOM Input, Web Workers, Web Sockets. Once we have unified these concepts, this enables rich composition.

ReactLua语言实现

https://github.com/bjornbytes/RxLua

Reactive Extensions for Lua.

RxLua gives Lua the power of Observables, which are data structures that represent a stream of values that arrive over time. They're very handy when dealing with events, streams of data, asynchronous requests, and concurrency.

local Rx = require 'rx'

Rx.Observable.fromRange(, )
:filter(function(x) return x % == end)
:concat(Rx.Observable.of('who do we appreciate'))
:map(function(value) return value .. '!' end)
:subscribe(print) -- => 2! 4! 6! 8! who do we appreciate!

协程异步。

local Rx = require 'rx'
local scheduler = Rx.CooperativeScheduler.create() -- Cheer someone on using functional reactive programming local observable = Rx.Observable.fromCoroutine(function()
for i = , , do
coroutine.yield(i)
end return 'who do we appreciate'
end, scheduler) observable
:map(function(value) return value .. '!' end)
:subscribe(print) repeat
scheduler:update()
until scheduler:isEmpty()

最简洁

local Rx = require 'rx'

-- Create an observable that produces a single value and print it.
Rx.Observable.of():subscribe(print)

concat例子

local Rx = require 'rx'

local first = Rx.Observable.fromRange()
local second = Rx.Observable.fromRange(, )
local third = Rx.Observable.fromRange(, , ) first:concat(second, third):dump('concat') print('Equivalent to:') Rx.Observable.concat(first, second, third):dump('concat')

观察者模式

local Rx = require 'rx'

local subject = Rx.Subject.create()

subject:subscribe(function(x)
print('observer a ' .. x)
end) subject:subscribe(function(x)
print('observer b ' .. x)
end) subject:onNext()
subject()
subject:onNext()

多道并发

local Rx = require 'rx'
local scheduler = Rx.CooperativeScheduler.create()
local timerResolution = .
local function log(message)
print('[' .. string.format('%.2f', scheduler.currentTime) .. '] ' .. message)
end -- Demonstrate Rx.Scheduler.Cooperative by running some simultaneous cooperative threads.
scheduler:schedule(function()
log('this is like a setTimeout')
end, ) scheduler:schedule(function()
local i =
while true do
log('this prints i twice per second: ' .. i)
i = i +
coroutine.yield(.)
end
end) scheduler:schedule(function()
for i = , do
log('this will print for 3 updates after 1 second')
coroutine.yield()
end
end, ) -- Simulate 3 virtual seconds.
repeat
scheduler:update(timerResolution)
until scheduler.currentTime >=

ReactiveX编程范式的更多相关文章

  1. 编程范式感想(一)——在C中进行对模板功能的实现

    最近一直在看网易公开课上的编程范式的公开课,斯坦福的教授讲的真的非常到位,感觉还是要好好学习下C还有汇编,熟悉下计算机的内存机制什么的. 大家都知道关于模板或者说范式的问题,基本在很多高级语言上都有实 ...

  2. 【编程范式】C语言1

    最近在网易公开课上看斯坦福大学的<编程范式>,外国人讲课思路就是清晰,上了几节课,感觉难度确实比我们普通大学大很多,但是却很有趣,让人能边学边想. 范式编程,交换两个数,利用 void * ...

  3. 编程范式:命令式编程(Imperative)、声明式编程(Declarative)和函数式编程(Functional)

    主要的编程范式有三种:命令式编程,声明式编程和函数式编程. 命令式编程: 命令式编程的主要思想是关注计算机执行的步骤,即一步一步告诉计算机先做什么再做什么. 比如:如果你想在一个数字集合 collec ...

  4. Linux Kernel C语言编程范式

    介绍 不同的编程语言具有不同的抽象原语(如下),有的原语抽象层次低,有的原语抽象层次高.其中函数式.DSL是这几年十分热门的编程语言概念. 过程式抽象原语:变量 对象式抽象原语:对象 函数式抽象原语: ...

  5. 冒号课堂 编程范式与OOP思想

    上篇:编程范式与编程语言 第1课 开班导言 第2课 重要范式 第3课 常用范式 第4课 重温范式 第5课 语言小谈 第6课 语言简评 下篇:抽象机制与对象范式 第7课 抽象封装 第8课 抽象接口 第9 ...

  6. Python3学习之路~6.1 编程范式:面向过程 VS 面向对象

    编程范式 编程是程序员用特定的语法+数据结构+算法组成的代码来告诉计算机如何执行任务的过程,一个程序是程序员为了得到一个任务结果而编写的一组指令的集合,正所谓条条大路通罗马,实现一个任务的方式有很多种 ...

  7. Edit Distance问题在两种编程范式下的求解

    本文已授权 [Coding博客](https://blog.coding.net) 转载 前言 Edit Distance,中文叫做编辑距离,在文本处理等领域是一个重要的问题,以下是摘自于百度百科的定 ...

  8. jQuery中的编程范式

    浏览器前端编程的面貌自2005年以来已经发生了深刻的变化,这并不简单的意味着出现了大量功能丰富的基础库,使得我们可以更加方便的编写业务代码,更重要的是我们看待前端技术的观念发生了重大转变,明确意识到了 ...

  9. jQuery 中的编程范式

    浏览器前端编程的面貌自2005年以来已经发生了深刻的变化,这并不简单的意味着出现了大量功能丰富的基础库,使得我们可以更加方便的编写业务代码,更重要的是我们看待前端技术的观念发生了重大转变,明确意识到了 ...

随机推荐

  1. MemCache

    MemCache是一个自由.源码开放.高性能.分布式的分布式内存对象缓存系统,用于动态Web应用以减轻数据库的负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高了网站访问的速度.Mem ...

  2. 1.0 iOS中的事件

    本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书”   在用户使用app过程中,会产生各种各样的事件,iOS中的事件可以分为3大类型: UIK ...

  3. WSDL项目----操作和请求

    至于现在你只看服务相关的特性,让我们尝试更多的操作和相应的请求. 操作 每个基于WSDL服务公开的操作包括一个请求和响应消息格式(可选). soapUI服务中的动作显示为节点在项目导航器的服务下 在s ...

  4. 创建控制器的方法、控制器加载view过程、控制器view的生命周期、多控制器组合

    在介绍四大对象的那篇博客中,可以基本了解到程序启动的过程: main-->UIApplicationMain-->创建UIApplication的实例和app代理AppDelegate的实 ...

  5. BZOJ3687 计算子集和的异或和

    题不知道怎么不见了,bzoj上已经没了3687这题了 题意:给你一个n 然后输入n个数 求这n个数的所有子集的和的异或和 思路:用bitset记录某个数是否在子集和中出现,利用bitset对二进制位的 ...

  6. java 将一张图片拷贝到另外一个地方。(IO流)

    package com.beiwo.inputstream; import java.io.FileInputStream; import java.io.FileOutputStream; impo ...

  7. java中接口的定义和接口的实现

    1.接口的定义 使用interface来定义一个接口.接口定义同类的定义类似,也是分为接口的声明和接口体,其中接口体由常量定义和方法定义两部分组成.定义接口的基本格式如下: [修饰符] interfa ...

  8. (学)解决VMware Taking ownership of this virtual machine failed

    原文:http://blog.csdn.net/fisher_jiang/article/details/6992588背景: 一次crash可能会造成虚拟机锁死的情况发生现象:点击take owne ...

  9. 团队作业week9 scenario testing

    1.How do you expect different personas to use your software? What’s their need and their goals,  how ...

  10. PHP基础知识之函数

    定义: <?phpclass foo-----定义类{    function do_foo()---类的方法    {        echo "Doing foo.";  ...