Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enumerator vs Iterator in scala and java

What is the difference between Enumerator and Iterator? Per my understanding Enumerator is not a fancy alias for enum in Java. Rather, it seems to be a traversal technique similar to an Iterator. So is anyone able to compare and contrast Enumerator and Iterator? Also, I see a usage in Play as

val data = getDataStream
val dataContent: Enumerator[Array[Byte]] = Enumerator.fromStream(data)

EDIT:

I am inclined to think that Enumerator gives us actual chunks of data, whereas Iterator gives us pointers to data that is already chunked, such as a list. But I am not convinced that that's the case.

like image 488
learner Avatar asked Jan 15 '23 03:01

learner


1 Answers

Enumerator is a Play class, not a Java or Scala one.

It is part of the Iteratee I/O-handling which Play provides. Iteratees are an interesting beast -- on one hand, it "pushes" data to the handler, instead of relying to the handler to pull data, and, therefore, has better performance. On the other hand, it allows the handler to control when the flow should be stopped. Finally, one can compose iteratees so that different iteratees are responsible for different parts of the input (say, one to receive HTTP headers, other to receive its body) as well as chaining them so low level iteratees can do a pre-processing which is then fed to higher level iteratees.

There's plenty of material on them, avail yourself to it.

It is not related in any way to iterators or enumerations.

like image 58
Daniel C. Sobral Avatar answered Jan 22 '23 10:01

Daniel C. Sobral