3 ms·
> 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 eve
by naasking 9y ago
> 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.
I have a creeping suspicion that after a few more years, "push" is just going to be viewed as codata, and "pull" will just be "data". I mean, that's kind of what they are, our languages just don't have codata as first-class citizens, and so we shoehorn it into stream abstractions.
- zachrose 9y agoCould you unpack this a bit for the relative layperson? I once heard a guy at a Haskell meetup say that “codata is the categorical dual of data” but I didn’t know what that means then and I still don’t know.
- Retra 9y agoData is finite data structures, while Codata is infinite data structures (like streams.) They are duals because you can generate and process data using induction, while you can use coinduction to generate and process codata. One is "built up" naturally, and the other is "torn down" naturally.
- naasking 9y agoTo elaborate a little on the other poster, the data types you're used to are finite values built incrementally, ie. build this, then build this, then build that. Codata is the inverse, in that you can see it as a finite set of observations of some already built infinite data structure, ie. unpack this value at the head, then unpack that, etc. For instance, the stream of values observed from a process that runs forever. We usually encode codata using inductive data types, but this is awkward for all of the reasons that come up in FRP and other reactive systems.
- isoos 9y agoI believe that the distinguishing property is not what the event/data/codata is, rather how you are accessing it and when you process it. In a "push" model you provide a callback that will receive events and it has no way of influencing their frequency or timing. There is no mandatory buffering here. In the "pull" model, you have a handle, that you can call to receive the next event, which is essentially an iteration over async events. It is up to you to decide the frequency or the timing, and it is up to the backing library to buffer events if needed to be. I'm linking to the Dart API pages here, but the same concept applies to every language/library out there. Stream.listen [0] is straightforward and similar to many other libs out there. The interesting part is that Dart has a StreamIterator[1], which allows you to "pull" events form the stream at your own pace. [0] https://api.dartlang.org/stable/1.24.3/dart-async/Stream-class.html#listen https://api.dartlang.org/stable/1.24.3/dart-async/Stream-cla... [1] https://api.dartlang.org/stable/1.24.3/dart-async/StreamIterator-class.html https://api.dartlang.org/stable/1.24.3/dart-async/StreamIter... It is also easy to use Stream Iterator to loop over the values unconditionally: Stream<Event> stream = whatever(); var iter = new StreamIterator<Event>(stream); while (await iter.moveNext()) { // process the iter.current value at whatever pace you'd like }
- naasking 9y ago> I believe that the distinguishing property is not what the event/data/codata is, rather how you are accessing it and when you process it. But this is exactly one of the important distinctions between data and codata. If your language supported first-class codata, you wouldn't need to encode push evaluation via a stream data type. Codata already has the required push-like semantics, and your language's evaluation model would handle the necessary plumbing for you.
- omginternets 9y ago>codata I'm not familiar with the term. Could you please elaborate?
- naasking 9y agoSee other replies to my comment for more details.
- danharaj 9y agoThe intuition is close but it's not quite the same duality as between data and codata. It's probably true that Behavior and Event are dual but they should be viewed more like modalities than as data structures. In fact, a first cut at semantics for FRP is temporal logic which has modalities like "sometimes" and "always".