ReactiveX编程范式
ReactiveX
http://reactivex.io/
An API for asynchronous programming
with observable streamsThe Observer pattern done right
ReactiveX is a combination of the best ideas from
the Observer pattern, the Iterator pattern, and functional programmingEasily create event streams or data streams.Compose and transform streams with query-like operators.Subscribe to any observable stream to perform side effects.
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编程范式的更多相关文章
- 编程范式感想(一)——在C中进行对模板功能的实现
最近一直在看网易公开课上的编程范式的公开课,斯坦福的教授讲的真的非常到位,感觉还是要好好学习下C还有汇编,熟悉下计算机的内存机制什么的. 大家都知道关于模板或者说范式的问题,基本在很多高级语言上都有实 ...
- 【编程范式】C语言1
最近在网易公开课上看斯坦福大学的<编程范式>,外国人讲课思路就是清晰,上了几节课,感觉难度确实比我们普通大学大很多,但是却很有趣,让人能边学边想. 范式编程,交换两个数,利用 void * ...
- 编程范式:命令式编程(Imperative)、声明式编程(Declarative)和函数式编程(Functional)
主要的编程范式有三种:命令式编程,声明式编程和函数式编程. 命令式编程: 命令式编程的主要思想是关注计算机执行的步骤,即一步一步告诉计算机先做什么再做什么. 比如:如果你想在一个数字集合 collec ...
- Linux Kernel C语言编程范式
介绍 不同的编程语言具有不同的抽象原语(如下),有的原语抽象层次低,有的原语抽象层次高.其中函数式.DSL是这几年十分热门的编程语言概念. 过程式抽象原语:变量 对象式抽象原语:对象 函数式抽象原语: ...
- 冒号课堂 编程范式与OOP思想
上篇:编程范式与编程语言 第1课 开班导言 第2课 重要范式 第3课 常用范式 第4课 重温范式 第5课 语言小谈 第6课 语言简评 下篇:抽象机制与对象范式 第7课 抽象封装 第8课 抽象接口 第9 ...
- Python3学习之路~6.1 编程范式:面向过程 VS 面向对象
编程范式 编程是程序员用特定的语法+数据结构+算法组成的代码来告诉计算机如何执行任务的过程,一个程序是程序员为了得到一个任务结果而编写的一组指令的集合,正所谓条条大路通罗马,实现一个任务的方式有很多种 ...
- Edit Distance问题在两种编程范式下的求解
本文已授权 [Coding博客](https://blog.coding.net) 转载 前言 Edit Distance,中文叫做编辑距离,在文本处理等领域是一个重要的问题,以下是摘自于百度百科的定 ...
- jQuery中的编程范式
浏览器前端编程的面貌自2005年以来已经发生了深刻的变化,这并不简单的意味着出现了大量功能丰富的基础库,使得我们可以更加方便的编写业务代码,更重要的是我们看待前端技术的观念发生了重大转变,明确意识到了 ...
- jQuery 中的编程范式
浏览器前端编程的面貌自2005年以来已经发生了深刻的变化,这并不简单的意味着出现了大量功能丰富的基础库,使得我们可以更加方便的编写业务代码,更重要的是我们看待前端技术的观念发生了重大转变,明确意识到了 ...
随机推荐
- Android开发各类常见错误解决方案
本文属于个人平时项目开发过程遇到的一些问题,记录下来并总结解决方案,希望能帮到大家解决问题,有些问题的解决方案是在StackoverFlow上找到的,建议大家遇到问题多去上面找,基本上都能找到解决方案 ...
- php 执行计划任务方式之 linux crontab 执行命令
一.crond简介 crond 是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务 工具,并且会自动启动c ...
- linux下 html转pdf
其实很简单的, 在当前文件夹中打开终端, 只需要一个命令就好 wkhtmltopdf test.html test.pdf 这样一个test.html的文件就转为test.pdf 的pdf文件啦!
- XD, XR, DR 股票
股权登记日与除权除息日 所以,如果投资者想得到一家上市公司的分红.配股权,就必须弄清这家公司的股权登记日在哪一天,否则就会失去分红.配股的机会. 股权登记日后的第一天就是除权日或除息日,这一天或以后购 ...
- JSON和JSONP (含jQuery实例)(share)
来源:http://www.cnblogs.com/dowinning/archive/2012/04/19/json-jsonp-jquery.html 前言: 说到AJAX就会不可避免的面临两个问 ...
- dojo tree edit的使用[前端]
var store = new mydata.JsonRestStore({ target: "<%=ResolveUrl("~/uieditserver.ashx" ...
- ZeroMQ接口函数之 :zmq_msg_set - 设置消息的性质
ZeroMQ 官方地址 :http://api.zeromq.org/4-1:zmq_msg_set zmq_msg_set(3) ØMQ Manual - ØMQ/3.2.5 Name zmq_m ...
- discuz sphinx全文检索搜索引擎方案
基于discuz的索引配置文件,这个配置文件比较灵活,可以根据不同的需求来配置 # # linuxTone full index search configure file # source lt_p ...
- Java技巧(代码简略)
1.将数组循环显示 int[] num = new int[]{1,3,5,7,9}; int currentNum=0; num[++current%num.length];
- jquery mobile页面跳转缓存问题解决
最近,我的一个写后端的同事因为缺前端自己做起了前端的活儿,因为对前端的不熟悉,找寻了一些现成框架想轻松了事,做一个web app他选了jquery mobile,开发效率确实高,但是这个框架的一些坑也 ...