Wednesday, September 11, 2013

Dart's fold method for Stream

Dart M5(probably)  re-designed of Collection/Stream related classes.

Although it created  lots of incompatibility, but they were worth  doing.
They should be considered as basic contribution for Dart language although they are library level enhancement.

One interesting action was they removed super Collection class.
Dart replaced it by Iterable.
for ORM mapping, if we have relationship among entiries, such relationship are represented by List, but if it is mapped to database with huge number of relatioship, it is quite inappropriate to fetch all elements in that property. Often some lazy approach is used, but actually caching is also not so good idea as default behavior, it should be just an Iterable object.
Then the default behavior will not fetch anything, nor will not cache anything.

It is interesting to compare Iterable, Collection and Stream.
Here I'm not so much following the actual Dart class library, but at more conceptual level.

Iterable are often considered as read only finite collection with synchronous iterate operation.
this means whenever we calls 'next', we will get next object immediately. Also user can control when he call  'next'.

On the other hand, Stream is some thing like iterable, but it is something more asynchronous.
and conceptually, stream is something external system is associated. and we cannot know when next data/event come. stream is a unpredictable sequence of events.
the event may be just one character, or line of string, an event object,etc.
We can just passively receive these events when it become available.
This concepts fits well for receiving radio signals. these event are not persistent, they just come and gone.
Also the number of events may not be finite, or predefined. we may expect almost infinite number of signal from a star over infinite period time. so infinite means 'potentially' no limit.

The notion of Collection is similar to stream in a sense both they can iterate over the element, but we assume it entirety at any moment. it it should be finite set of elements.
the synchronous nature to access its elements come from this model.

Although there are some differences,  if we want to iterate all the elements in either synchronous or asynchronous way, they are very similar.
Actually similar methods were introduced for them in Dart.

fold, reduce are the 'MOST' famous functions in functional programming language.
often it is used in a text book of functional programming language.

fun foldr(as as (a::as0), b, f: (B, A)->B):B=> (as == [])? b: f(folder(as0, b, f), a);
Essentially, it allows to do sigma like operator in math.
1+2+3+..+n
we can write: fold([1...n], 0, add);

in Dart's new Stream class(actually I don't know when this feature was introduced though), it supports this fold for asynchronous stream iteration.
this is interesting idea.

we can write a code to convert the input of stream to a string .

stream.fold(new StringBuffer(), (StringBuffer sb, String  line){ sb.write(line); return sb;})
.then((StringBuffer sb)=>sb.toString()).
then((text){
....
});

The above code is not the exact  code in dart, but we see the  similar pattern in many places.

Anyway, it is interesting to see commonality of these iterator classes  beyond synchronous and asynchronous differences.

No comments:

Post a Comment