Input and Output-The input is all the sources of action for your app
Programs take input and produce output. The output is the result of doing something with the input. Input, transform, output, done.
This pattern is easy to see when the program is a UNIX tool. Take a string, count the words, print out the result. But it’s a lot harder to see when we’re writing an iOS app with a UI, lots of different features, periodic tasks, etc.
What’s the input and output?
The output is often a change in the app’s UI. A switch is toggled or a list gets a new item. Or it could be more than that. It could be a new file on the device’s disk, or it could be an API request. These things are the outputs of the app.
But unlike the classic input/output design, this input and output happens more than once. It’s not just a single input → work → output—the cycle continues while the app is open. The app is always consuming inputs and producing outputs based on them.
So why should we care? Because fresh perspectives are powerful and good and necessary and cool. And in this case, it gives us a fantastic new tool.
State
There’s no intrinsic idea of state from this perspective. There’s just a change in an input resulting in a new output. State might be an implementation detail with how the app handles its inputs, but it’s not necessary. It’s not intrinsic to the idea.
Most problems worth solving have some intrinsic state. State can be essential. But that’s not how we treat it. We solve everything with state. Because we treat all the inputs to our app as different things—a touch event here, a web response there—we can’t combine them in any meaningful way. We can’t transform them uniformly. And so our only tool for dealing with all these different things is state. When our only tool is state, every problem looks like a stateful nail.
We’re in the habit of constantly introducing more state into our app. New feature? New state. New complexity. New bugs.
But happily this perspective of our app’s output as a function of its inputs over time gives us a new tool: functional reactive programming. Functional reactive programming (FRP) is a paradigm built around the idea of time-varying values produced by time-varying functions.
Time
“Time-varying values” might sound like a bit of sleight-of-hand. Isn’t that just another way of saying “state?” They’re both trying to capture the same idea—that things change as the program runs. But by formalizing and reifying time variance, we can reason about change safely.
Time-varying values can be derived from other time-varying values, which are themselves derived from the time-varying inputs to the app(多条平行线的多个信号源). So while traditional state places the burden of ensuring our app is always in a known consistent state on us, the programmers, FRP lets us define our app in terms of the time-varying values and ensures changes propagate as needed.
Before, state was discrete pieces of data all moving independently. But time-varying values are cogs all fitting together in a gear. When one turns, it turns all its connected cogs, which turns their cogs, which turns… and ends up running the entire mechanism all by themselves.
It’s beautiful.5
There are a lot of other things that behave like time-varying values. The result of asynchronous work is really just a time-varying value that only has a value once the work is done.6 Or a UI element’s value could be seen as a time-varying value that changes as the user interacts with it. If my app is running on a mobile device, the device’s GPS coordinates is a time-varying value.
State, inputs, and outputs.
Fin
That’s a small, practical example of the principles of FRP in an imperative language. The whole example view is on GitHub: RACSignupDemo.
Functional reactive programming offers a way to once again view our programs as simply input and output. We get to minimize state while also embracing a unified view of what our app is doing. It’s all just inputs and outputs.
http://joshaber.github.io/2013/02/11/input-and-output/
Input and Output-The input is all the sources of action for your app的更多相关文章
- [20160704]Addition program that use JOptionPane for input and output
//Addition program that use JOptionPane for input and output. import javax.swing.JOptionPane; public ...
- Python Tutorial 学习(七)--Input and Output
7. Input and Output Python里面有多种方式展示程序的输出.或是用便于人阅读的方式打印出来,或是存储到文件中以便将来使用.... 本章将对这些方法予以讨论. 两种将其他类型的值转 ...
- [Python] Print input and output in table
Print the input and output in a table using prettyTable. from prettytable import PrettyTable import ...
- Input and Output File
Notes from C++ Primer File State Condition state is used to manage stream state, which indicates if ...
- [20171128]rman Input or output Memory Buffers.txt
[20171128]rman Input or output Memory Buffers.txt --//做一个简单测试rman 的Input or output Memory Buffers. 1 ...
- Angular4学习笔记(六)- Input和Output
概述 Angular中的输入输出是通过注解@Input和@Output来标识,它位于组件控制器的属性上方. 输入输出针对的对象是父子组件. 演示 Input 新建项目connInComponents: ...
- Python - 3. Input and Output
from:http://interactivepython.org/courselib/static/pythonds/Introduction/InputandOutput.html Input a ...
- Java中的IO流,Input和Output的用法,字节流和字符流的区别
Java中的IO流:就是内存与设备之间的输入和输出操作就成为IO操作,也就是IO流.内存中的数据持久化到设备上-------->输出(Output).把 硬盘上的数据读取到内存中,这种操作 成为 ...
- NET中调用存储过程(Output、Input)
NET中调用存储过程(Output.Input) .NET中调用存储过程(Output.Input) 带输入输出参数的存储过程 带输入输出参数的存储过程 create procedure itemCo ...
随机推荐
- [转]Vue.js 目录结构
本文转自:http://www.runoob.com/vue2/vue-directory-structure.html 上一章节中我们使用了 npm 安装项目,我们在 IDE(Eclipse.Ato ...
- [转]MVC Razor模板引擎 @RenderBody、@RenderPage、@RenderSection及Html.RenderPartial、Html.RenderAction
本文参考自下面文章整理 MVC Razor模板引擎 @RenderBody.@RenderPage.@RenderSection及Html.RenderPartial.Html.RenderActio ...
- WCF-终结点之消息路由示例
一. 在前一章中主要介绍了服务端的监听地址与逻辑地址.本节模拟消息转发机制来实际体验一把终结点的监听地址是如何使用的. 先下载一个叫做TcpTrace的小软件(108k),它能够截取端口消息,并转发消 ...
- jQuery基础---Ajax基础
内容提纲: 1.Ajax 概述 2.load()方法 3.$.get()和$.post() 4.$.getScript()和$.getJSON() 5.$.ajax()方法 6.表单序列化 发文不易, ...
- [PHP] 试题系统研究
考试科目: 添加考试科目,填写科目名称,选择科目题型(复选框/单选题,多选题,判断题,问答题,填空题) 添加科目章节,填写章节名称,添加章节知识点,填写知识点以英文逗号分隔,直接插入多条记录 开通考场 ...
- K:括号分隔符匹配问题
相关介绍: 括号分隔符匹配问题是指,判断所输入的字符串表达式中的括号是否匹配的问题,例如1+(12+2)*(1+2)便是一个括号分隔符匹配的表达式,而(12+1)*4+(12/2]就是一个括号分隔符 ...
- 微软2016校园招聘在线笔试-Professor Q's Software
题目2 : Professor Q's Software 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Professor Q develops a new softw ...
- UOJ#414. 【APIO2018】新家
传送门 首先二分答案 \(mid\),问题变成求区间 \([l-mid,r+mid]\) 在该年份的不同类型个数为 \(k\) 关于年份的限制可以离线下来 现在的问题就是区间数颜色,一个套路就是维护每 ...
- JavaScript中各存在性函数
JavaScript中有很多表示存在性和从属关系的函数,本文介绍如下几个: 1)有关实例与构造函数原型之间的关系:isPrototypeOf(),Object.getPrototypeOf(); 2) ...
- 使用ArcGIS Chef Cookbook轻松搞掂WebGIS平台部署
1.安装Chef Client v12版本. 2.复制arcgis cookbook资源到Chef安装目录. 3.考虑到一般部署的服务器环境无法连接互联网,所以需要事先部署ArcGIS Cookboo ...