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/

最新文章

  1. 2013 duilib入门简明教程 -- 结合win32和MFC (16)
  2. elasticsearch agg
  3. ypzl药品质量不合格数据库-excel自动排版
  4. js switch 扩展
  5. 在Windows和Linux上安装paramiko模块以及easy_install的安装方法
  6. 使用MiniProfiler调试ASP.NET MVC网站性能
  7. WebSphere SSLC0008E 无法初始化 SSL 连接。未授权访问被拒绝,或者安全性设置已到期 解决方法
  8. 【hdu2196】Computer
  9. android使用webview加载flash文件
  10. [转]python集合set
  11. GoogLeNet学习心得
  12. OpenCV学习 1:OpenCV安装与第一个图像显示程序
  13. Nagios+pnp4nagios+rrdtool 安装配置nagios(一)
  14. java 守护线程
  15. js模块化加载器实现
  16. 【转载】VS写汇编程序01:VS2015配置汇编语言开发环境
  17. Google SketchUp Cookbook: (Chapter 4) Advanced Intersect and Follow Me Techniques
  18. Java网络编程中异步编程的理解
  19. pycharm 如何进行全部搜索
  20. dedecms站内搜索页面调用最新文章

热门文章

  1. bootstrap table demo
  2. Java jxl导入excel文件,导入的数字、身份证号码、手机号变成了科学计数法,解决方案
  3. C# 之程序退出的方法
  4. 最新的dubbo和zookeeper整合的问题
  5. 撩课-Web大前端每天5道面试题-Day16
  6. Gradle修改缓存路径 和 Gradle修改Maven仓库地址
  7. python中类变量和实例变量
  8. 【HTML&CSS】基本的入门
  9. js函数的节流与防抖
  10. linux shell实现守护进程 看门狗 脚本