<s>
In	O
functional	B-Language
programming	I-Language
,	O
an	O
iteratee	B-Application
is	O
a	O
composable	O
abstraction	B-Application
for	O
incrementally	O
processing	O
sequentially	O
presented	O
chunks	O
of	O
input	B-General_Concept
data	I-General_Concept
in	O
a	O
purely	B-Application
functional	I-Application
fashion	O
.	O
</s>
<s>
With	O
iteratees	B-Application
,	O
it	O
is	O
possible	O
to	O
lazily	O
transform	O
how	O
a	O
resource	O
will	O
emit	O
data	O
,	O
for	O
example	O
,	O
by	O
converting	O
each	O
chunk	O
of	O
the	O
input	O
to	O
uppercase	O
as	O
they	O
are	O
retrieved	O
or	O
by	O
limiting	O
the	O
data	O
to	O
only	O
the	O
five	O
first	O
chunks	O
without	O
loading	O
the	O
whole	O
input	B-General_Concept
data	I-General_Concept
into	O
memory	O
.	O
</s>
<s>
Iteratees	B-Application
are	O
also	O
responsible	O
for	O
opening	O
and	O
closing	O
resources	O
,	O
providing	O
predictable	O
resource	O
management	O
.	O
</s>
<s>
On	O
each	O
step	O
,	O
an	O
iteratee	B-Application
is	O
presented	O
with	O
one	O
of	O
three	O
possible	O
types	O
of	O
values	O
:	O
the	O
next	O
chunk	O
of	O
data	O
,	O
a	O
value	O
to	O
indicate	O
no	O
data	O
is	O
available	O
,	O
or	O
a	O
value	O
to	O
indicate	O
the	O
iteration	O
process	O
has	O
finished	O
.	O
</s>
<s>
The	O
latter	O
types	O
of	O
values	O
in	O
effect	O
represent	O
the	O
possible	O
"	O
states	O
"	O
of	O
an	O
iteratee	B-Application
.	O
</s>
<s>
An	O
iteratee	B-Application
would	O
typically	O
start	O
in	O
the	O
"	O
continue	O
"	O
state	O
.	O
</s>
<s>
Iteratees	B-Application
are	O
used	O
in	O
Haskell	B-Language
and	O
Scala	B-Application
(	O
in	O
the	O
Play	B-Language
Framework	I-Language
and	O
in	O
Scalaz	O
)	O
,	O
and	O
are	O
also	O
available	O
for	O
F#	B-Operating_System
.	O
</s>
<s>
Various	O
slightly	O
different	O
implementations	O
of	O
iteratees	B-Application
exist	O
.	O
</s>
<s>
For	O
example	O
,	O
in	O
the	O
Play	B-Language
framework	I-Language
,	O
they	O
involve	O
Futures	B-Operating_System
so	O
that	O
asynchronous	O
processing	O
can	O
be	O
performed	O
.	O
</s>
<s>
Because	O
iteratees	B-Application
are	O
called	O
by	O
other	O
code	O
which	O
feeds	O
them	O
with	O
data	O
,	O
they	O
are	O
an	O
example	O
of	O
inversion	B-Language
of	I-Language
control	I-Language
.	O
</s>
<s>
However	O
,	O
unlike	O
many	O
other	O
examples	O
of	O
inversion	B-Language
of	I-Language
control	I-Language
such	O
as	O
SAX	B-Application
XML	O
parsing	B-Language
,	O
the	O
iteratee	B-Application
retains	O
a	O
limited	O
amount	O
of	O
control	O
over	O
the	O
process	O
.	O
</s>
<s>
It	O
cannot	O
reverse	O
back	O
and	O
look	O
at	O
previous	O
data	O
(	O
unless	O
it	O
stores	O
that	O
data	O
internally	O
)	O
,	O
but	O
it	O
can	O
stop	O
the	O
process	O
cleanly	O
without	O
throwing	O
an	O
exception	B-General_Concept
(	O
using	O
exceptions	O
as	O
a	O
means	O
of	O
control	O
flow	O
,	O
rather	O
than	O
to	O
signal	O
an	O
exceptional	O
event	O
,	O
is	O
often	O
frowned	O
upon	O
by	O
programmers	O
)	O
.	O
</s>
<s>
The	O
following	O
abstractions	B-Application
are	O
not	O
strictly	O
speaking	O
necessary	O
to	O
work	O
with	O
iteratees	B-Application
,	O
but	O
they	O
do	O
make	O
it	O
more	O
convenient	O
.	O
</s>
<s>
An	O
Enumerator	O
is	O
a	O
convenient	O
abstraction	B-Application
for	O
feeding	O
data	O
into	O
an	O
iteratee	B-Application
from	O
an	O
arbitrary	O
data	O
source	O
.	O
</s>
<s>
Because	O
the	O
enumerator	O
knows	O
exactly	O
when	O
the	O
iteratee	B-Application
has	O
finished	O
reading	O
data	O
,	O
it	O
will	O
do	O
the	O
resource	O
cleanup	O
(	O
such	O
as	O
closing	O
a	O
file	O
)	O
at	O
exactly	O
the	O
right	O
time	O
–	O
neither	O
too	O
early	O
nor	O
too	O
late	O
.	O
</s>
<s>
However	O
,	O
it	O
can	O
do	O
this	O
without	O
needing	O
to	O
know	O
about	O
,	O
or	O
being	O
co-located	O
to	O
,	O
the	O
implementation	O
of	O
the	O
iteratee	B-Application
–	O
so	O
enumerators	O
and	O
iteratees	B-Application
form	O
an	O
example	O
of	O
separation	O
of	O
concerns	O
.	O
</s>
<s>
An	O
Enumeratee	O
is	O
a	O
convenient	O
abstraction	B-Application
for	O
transforming	O
the	O
output	O
of	O
either	O
an	O
enumerator	O
or	O
iteratee	B-Application
,	O
and	O
feeding	O
that	O
output	O
to	O
an	O
iteratee	B-Application
.	O
</s>
<s>
Iteratees	B-Application
were	O
created	O
due	O
to	O
problems	O
with	O
existing	O
purely	B-Application
functional	I-Application
solutions	O
to	O
the	O
problem	O
of	O
making	O
input/output	B-General_Concept
composable	O
yet	O
correct	O
.	O
</s>
<s>
Lazy	O
I/O	B-General_Concept
in	O
Haskell	B-Language
allowed	O
pure	B-Application
functions	I-Application
to	O
operate	O
on	O
data	O
on	O
disk	O
as	O
if	O
it	O
were	O
in	O
memory	O
,	O
without	O
explicitly	O
doing	O
I/O	B-General_Concept
at	O
all	O
after	O
opening	O
the	O
file	O
-	O
a	O
kind	O
of	O
memory-mapped	B-General_Concept
file	I-General_Concept
feature	O
-	O
but	O
because	O
it	O
was	O
impossible	O
in	O
general	O
(	O
due	O
to	O
the	O
Halting	O
problem	O
)	O
for	O
the	O
runtime	O
to	O
know	O
whether	O
the	O
file	O
or	O
other	O
resource	O
was	O
still	O
needed	O
,	O
excessive	O
numbers	O
of	O
files	O
could	O
be	O
left	O
open	O
unnecessarily	O
,	O
resulting	O
in	O
file	B-Application
descriptor	I-Application
exhaustion	O
at	O
the	O
operating	B-General_Concept
system	I-General_Concept
level	O
.	O
</s>
<s>
Traditional	O
C-style	O
I/O	B-General_Concept
,	O
on	O
the	O
other	O
hand	O
,	O
was	O
too	O
low-level	O
and	O
required	O
the	O
developer	O
to	O
be	O
concerned	O
with	O
low-level	O
details	O
such	O
as	O
the	O
current	O
position	O
in	O
the	O
file	O
,	O
which	O
hindered	O
composability	O
.	O
</s>
<s>
Iteratees	B-Application
and	O
enumerators	O
combine	O
the	O
high-level	O
functional	B-Language
programming	I-Language
benefits	O
of	O
lazy	O
I/O	B-General_Concept
,	O
with	O
the	O
ability	O
to	O
control	O
resources	O
and	O
low-level	O
details	O
where	O
necessary	O
afforded	O
by	O
C-style	O
I/O	B-General_Concept
.	O
</s>
<s>
Iteratees	B-Application
are	O
used	O
in	O
the	O
Play	B-Language
framework	I-Language
to	O
push	O
data	O
out	O
to	O
long-running	O
Comet	B-General_Concept
and	O
WebSocket	B-Protocol
connections	O
to	O
web	B-Application
browsers	I-Application
.	O
</s>
<s>
Iteratees	B-Application
may	O
also	O
be	O
used	O
to	O
perform	O
incremental	O
parsing	B-Language
(	O
that	O
is	O
,	O
parsing	B-Language
that	O
does	O
not	O
read	O
all	O
the	O
data	O
into	O
memory	O
at	O
once	O
)	O
,	O
for	O
example	O
of	O
JSON	B-General_Concept
.	O
</s>
<s>
Iteratees	B-Application
are	O
a	O
very	O
general	O
abstraction	B-Application
and	O
can	O
be	O
used	O
for	O
arbitrary	O
kinds	O
of	O
sequential	B-General_Concept
information	O
processing	O
(	O
or	O
mixed	O
sequential/random	O
-access	O
processing	O
)	O
-	O
and	O
need	O
not	O
involve	O
any	O
I/O	B-General_Concept
at	O
all	O
.	O
</s>
<s>
This	O
makes	O
it	O
easy	O
to	O
repurpose	O
an	O
iteratee	B-Application
to	O
work	O
on	O
an	O
in-memory	O
dataset	O
instead	O
of	O
data	O
flowing	O
in	O
from	O
the	O
network	O
.	O
</s>
<s>
In	O
a	O
sense	O
,	O
a	O
distant	O
predecessor	O
of	O
the	O
notion	O
of	O
an	O
enumerator	O
pushing	O
data	O
into	O
a	O
chain	O
of	O
one	O
or	O
more	O
iteratees	B-Application
,	O
was	O
the	O
pipeline	B-Operating_System
concept	O
in	O
operating	B-General_Concept
systems	I-General_Concept
.	O
</s>
<s>
However	O
,	O
unlike	O
a	O
typical	O
pipeline	B-Operating_System
,	O
iteratees	B-Application
are	O
not	O
separate	O
processes	O
(	O
and	O
hence	O
do	O
not	O
have	O
the	O
overhead	O
of	O
IPC	B-Operating_System
)	O
-	O
or	O
even	O
separate	O
threads	O
,	O
although	O
they	O
can	O
perform	O
work	O
in	O
a	O
similar	O
manner	O
to	O
a	O
chain	O
of	O
worker	O
threads	O
sending	O
messages	O
to	O
each	O
other	O
.	O
</s>
<s>
This	O
means	O
that	O
iteratees	B-Application
are	O
more	O
lightweight	O
than	O
processes	O
or	O
threads	O
-	O
unlike	O
the	O
situations	O
with	O
separate	O
processes	O
or	O
threads	O
,	O
no	O
extra	O
stacks	O
are	O
needed	O
.	O
</s>
<s>
Iteratees	B-Application
and	O
enumerators	O
were	O
invented	O
by	O
Oleg	O
Kiselyov	O
for	O
use	O
in	O
Haskell	B-Language
.	O
</s>
<s>
Later	O
,	O
they	O
were	O
introduced	O
into	O
Scalaz	O
(	O
in	O
version	O
5.0	O
;	O
enumeratees	O
were	O
absent	O
and	O
were	O
introduced	O
in	O
Scalaz	O
7	O
)	O
and	O
into	O
Play	B-Language
Framework	I-Language
2.0	O
.	O
</s>
<s>
Iteratees	B-Application
have	O
been	O
formally	O
modelled	O
as	O
free	O
monads	O
,	O
allowing	O
equational	O
laws	O
to	O
be	O
validated	O
,	O
and	O
employed	O
to	O
optimise	O
programs	O
using	O
iteratees	B-Application
.	O
</s>
<s>
Iterators	O
may	O
be	O
used	O
instead	O
of	O
iteratees	B-Application
in	O
Scala	B-Application
,	O
but	O
they	O
are	O
imperative	B-Application
,	O
so	O
are	O
not	O
a	O
purely	B-Application
functional	I-Application
solution	O
.	O
</s>
<s>
In	O
Haskell	B-Language
,	O
two	O
alternative	O
abstractions	B-Application
known	O
as	O
Conduits	O
and	O
Pipes	B-Operating_System
have	O
been	O
developed	O
.	O
</s>
<s>
(	O
These	O
Pipes	B-Operating_System
are	O
not	O
operating	B-General_Concept
system	I-General_Concept
level	O
pipes	B-Operating_System
,	O
so	O
like	O
iteratees	B-Application
they	O
do	O
not	O
require	O
the	O
use	O
of	O
system	B-Operating_System
calls	I-Operating_System
)	O
.	O
</s>
<s>
Conduits	O
in	O
particular	O
are	O
associated	O
with	O
substantially	O
richer	O
libraries	O
of	O
primitives	O
and	O
combinators	O
than	O
iteratees	B-Application
;	O
conduit	O
adapters	O
for	O
incremental	O
functionalities	O
such	O
as	O
parsing	B-Language
HTML	O
,	O
XML	O
,	O
generalised	O
parsing	B-Language
,	O
making	O
HTTP	O
requests	O
and	O
processing	O
the	O
responses	O
,	O
exist	O
,	O
making	O
conduits	O
more	O
suitable	O
than	O
iteratees	B-Application
for	O
industrial	O
software	O
development	O
in	O
Haskell	B-Language
,	O
out	O
of	O
the	O
box	O
.	O
</s>
<s>
There	O
is	O
also	O
a	O
high-level	O
abstraction	B-Application
named	O
.	O
</s>
<s>
In	O
Scala	B-Application
there	O
is	O
a	O
package	O
called	O
,	O
whose	O
ancestry	O
can	O
be	O
traced	O
back	O
to	O
machines	O
via	O
several	O
ports	O
,	O
renames	O
and	O
refactors	O
.	O
</s>
<s>
In	O
Haskell	B-Language
,	O
the	O
package	O
exists	O
.	O
</s>
<s>
It	O
provides	O
a	O
simpler	O
solution	O
to	O
some	O
of	O
the	O
same	O
problems	O
,	O
which	O
essentially	O
involves	O
being	O
"	O
strict	O
enough	O
"	O
to	O
pull	O
all	O
data	O
that	O
is	O
required	O
,	O
or	O
might	O
be	O
required	O
,	O
through	O
a	O
pipeline	B-Operating_System
which	O
takes	O
care	O
of	O
cleaning	O
up	O
the	O
resources	O
on	O
completion	O
.	O
</s>
