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. 第二十七篇:SOUI中控件属性查询方法

    SOUI项目的SVN根目录下有一个doc目录,下面有一份控件属性表.包含了大部分控件的大部分属性,不过也不一定完全准确.最保险的办法还是查源代码. SOUI对象包含控件及ISkinObj等从SObje ...

  2. 创业方向:O2O及移动社交 from 沈博阳

    十个最大的互联网公司,六个在美国,四个在中国 中国创业的特殊 市场巨大self-contained,做足中国市场就很够 ... ... 监管   领导力,人的要求 之前成功带领过团队 有海外的工作经历 ...

  3. 3Sum——leetcode

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  4. JavaScript变量和作用域

    认识JavaScript中的变量 JavaScript中的变量有两种类型,一种是基本类型.一种是引用类型. 基本数据类型:Defined,Null,Boolean,Number,String.注意St ...

  5. BZOJ3252: 攻略

    Description 题目简述:树版[k取方格数]   众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏. 今天他得到了一款新游戏<XX半岛>,这款游戏有n个场景 ...

  6. Java_动态重新加载Class总结

    在此记载Java动态重新加载Class的点点滴滴,实现之前也在网上看了很多文章,但发现不是很清晰,后来发现总结,看源码实现还是最靠谱. 直接上代码: package com.lkb.autoCode. ...

  7. cocos2d-x初步了解

    1.渲染 2.帧率(FPS) 超过75一般就不容易察觉到有明显的流畅度提升 >50 非常好! 30~40  一般, 20~30  及格,有点勉强. 3.渲染驱动游戏 事件驱动游戏 4.cocos ...

  8. Xamarin的不归路-连接MAC失败

    昨天费了老大劲才配置连接好MAC虚拟机,今天居然又连接不上了. 记录一下最后的解决办法: 直接用“Add Mac”添加虚拟机,一定要填写ip地址,为啥要写ip?我也不知道,因为我填写“MacdeMac ...

  9. SharedPreferences介绍,用来做数据存储

    sharedPreferences是通过xml文件来做数据存储的. 一般用来存放一些标记性的数据,一些设置信息. *********使用sharedPreferences存储数据 public sta ...

  10. CI,从数据库读取数据

    1.准备数据库,(用户,密码,数据库服务的地址) 2.CI链接数据库,配置database.php(配置文件)       //application/config/database.php 3.准备 ...