编程与软件工程gist.github.com
响应式编程入门
An Introduction to Reactive Programming
2014 — 2018,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这是 André Staltz 写的一篇响应式编程入门,用 RxJS 风格的事件流和自动补全、双击等 UI 交互示例,演示如何把用户输入与界面事件建模成可组合的流。文章从具体例子出发,解释如何用流操作来组合和处理事件。
为什么经久不衰:它把抽象的响应式概念落到可运行的 UI 示例,是理解 RxJS、Cycle.js 等流式框架的经典入口;评论中的实现批评也提醒读者示例代码不等于生产级方案。
HN 评论精选
常见学习卡点是很难把 UI 元素直观地理解为流。
我以前一直很难真正理解 FRP——很多聪明人都在推崇它,但我始终没开窍。这篇教程很棒,我一路跟得上,直到它把搜索建议建模成流的那部分。那一刻我完全乱了:我的大脑不愿意把这些 UI 元素当成流,立刻走神并觉得“这说不通”。我好奇有没有别人也卡在同一处,以及有没有建议能突破这个心理障碍?有没有另一种解决同一问题、可能对我更易懂的方式?
swanson查看英文原文
I've struggled to grok FRP in the past - I hear lots of smart people trumpeting it, but I haven't been able to wrap my head around it. This tutorial was great and I was following along right until it got to the part about modeling the suggestions as streams (https://gist.github.com/staltz/868e7e9bc2a7b8c1f754#modelling-the-3-suggestions-with-streams). That was where everything went haywire for me - my brain doesn't want to think about those UI elements as streams and I instantly tune out and think "this doesn't make sense". I'm curious if anyone else also got stuck at that same part, as well as if anyone has suggestions/ideas for breaking past this mental barrier? Is there a different way of solving the same problem (that might make more sense to me)?
教程的双击示例存在严重延迟问题,正确点击应即时发出并携带点击计数。
我觉得这是一篇不错的教程,但对双击那部分有点恼火。当然,你可以在 4 行代码里实现某种双击,但它的行为很糟糕。教程一带而过,我不知道是作者没意识到自己实现了一个很糟的点击系统,还是因为正确实现远不止 4 行而作者选择忽略。特别是,单击流内置了 250ms 延迟才触发点击,这很糟。双击同样内置 250ms 延迟,即使用户在 100ms 内点了两次;而且不只是从第一次点击起延迟 250ms,而是从最后一次点击起再等 250ms 才最终发出事件。这是一种完全不可用的点击处理方式。如果 FRP 里点击要这样做,那 FRP 就有严重问题。我只能假设有更好的方法,很想看到。正确行为应是点击时立即发出事件,同时记住近期历史,并根据是否有近期点击给事件关联点击次数。比如我以足够快的速度连点 7 次,应该每一次都收到点击事件,依次得到 1 次到 6 次点击,最后得到 7 次点击。
eridius查看英文原文
I thought this was a decent tutorial, but I'm a bit annoyed at the double-click bit. Yeah sure, you can implement some form of double-clicks in 4 lines of code, but it behaves badly. And since the tutorial glossed over it, I have no idea if this is just because the author doesn't realize they implemented a really bad click system, or because it takes a lot more than 4 lines of code to do it right and the author didn't like that so chose to ignore it. Notably, the single-click stream has a built-in 250ms delay before the click is fired. That's bad. Similarly, the double-click has a built-in 250ms delay even if the user clicks twice within 100ms. In fact, it's not just a 250ms delay from the first click; it's a 250ms delay from the last click before it finally emits the event. That's a completely unusable way of doing clicks. If that's how you do clicks in FRP, then that's a serious issue with FRP. I have to assume there's a better way, and I'd really like to see it. The correct behavior is to emit a click event immediately upon a click, but to remember recent history and associate a click count with the event based on whether there are recent clicks. So if I click 7 times in a row at sufficient speed, I should get a click event for every single one, getting 1x through 6x clicks before finally getting the 7x click on the last one.
多年实践发现不能把所有东西都当成流,事件与行为需要区分。
我所在的公司专做基于 Haskell reflex 库的多平台 FRP 应用。和 FRP 打交道多年后,我最大的心得是:并非所有东西都是流!你需要能混合流(也叫事件),它体现推送语义;以及行为(可采样),它体现拉取语义。这对性能和大型应用结构很重要。设计一组能高效、富有表达力地组合事件和行为的原语很难,但值得。
danharaj查看英文原文
My shop specializes in multiplatform FRP applications via the reflex haskell library [0]. After many years of wrangling FRP my biggest learned lesson is certainly this: not everything is a stream! You need to be able to mix streams (also called events), which embody push semantics and behaviors (things you can sample), which embody pull semantics. This is important for performance and for the structure of large applications. Coming up with a set of primitives to efficiently combine events and behaviors in expressive ways is really difficult but worth it. [0] https://github.com/reflex-frp/reflex-platform
作者是 Cycle.js 作者,该框架把应用状态视为事件时间线。
既然还没人提,而且文章里也体面地没提:作者是 Cycle.js 的创造者。Cycle.js 把 DOM 状态看作一个流程:<界面> -> 用户交互 -> 应用反应 -> <界面>。总体思想是,应用状态可以由整个生命周期中的事件时间线表示。因为它完全由时间和事件驱动、无状态,所以能引出很酷的“时间旅行”概念。其哲学是:如果你知道某物如何开始,并且知道它发生任何事情的确切时间,那么你就可以推断它在任何给定时期的状态。
hudbuddy查看英文原文
Since no one has mentioned it yet, and it respectably isn't mentioned in the article, the author of this is the creator of the cycle.js (cycle.js.org). It is a really cool way of looking at DOM state as a flow of: <interface> -> user interaction -> application reaction -> <interface> The general idea being that the state of an application can be represented by a timeline of events throughout the lifetime of an application. Since it is entirely time and event driven/stateless, it gives way to some pretty cool "time travel" concepts. The philosophy is this: If you know how something begins, and you know the exact times at which anything happens to it, then you can assume its state at any given period in time.
历次讨论
同类还值得读
开源应用架构
The Architecture of Open Source Applications
《开源软件架构》(AOSA)是一本在线免费书,邀请知名开源项目的作者亲自剖析其内部设计,涵盖 SQLAlchemy、LLVM、Audacity、Apache、Bash 等系统。
编程与软件工程上首页 9 次 · 2011–2024aosabook.org编辑精选编程语言简史:简短、不完整且多半错误
A Brief, Incomplete, and Mostly Wrong History of Programming Languages
一篇以戏谑、故意不准确的方式回顾编程语言史的博客文章,用夸张轶事和讽刺定义串起 Lisp、C、C++、Java、JavaScript、PHP 等语言。
编程与软件工程上首页 8 次 · 2009–2021james-iry.blogspot.comSQLite 是如何测试的
How SQLite is tested
SQLite 官方文档页,系统介绍 SQLite 的测试体系:多种测试方法(TCL 测试、模糊测试、回归测试、静态分析等)和海量测试用例,展示了这个小型开源项目如何靠极致测试获得…
编程与软件工程上首页 8 次 · 2009–2025sqlite.org编辑精选高级编程语言(2009)
Advanced programming languages (2009)
Matt Might 的经典文章,按用途推荐“高级编程语言”:如果追求就业就选主流语言,如果追求表达力、学术研究或创业,则推荐 Haskell、OCaml、Scala 等函数式语…
编程与软件工程上首页 8 次 · 2009–2022matt.might.net我的 Forth 与栈机器经历
Yosefk - My history with Forth & stack machines
作者以亲身经历回顾了使用 Forth 语言和栈式机器的历程:既赞叹其简洁优雅、代码量小、可自举的优点,也坦率指出栈操作在实际编程中的痛苦,以及 Forth 社区文化中的问题。
编程与软件工程上首页 8 次 · 2010–2024yosefk.com编辑精选