Commit cdf345d8b3dea76064009d6d5aae653bab7092aa

[pt_BR] Started working on the chapter Streams.

Commit diff

PortugueseBook/Streams/Streams.tex

 
2727 \sloppy
2828\fi
2929%=================================================================
30\chapter{Streams}\label{cha:streams}
31
32\clsindmain{Stream}
33Streams are used to iterate over sequences of elements such as sequenced
34collections, files, and network streams.
35Streams may be either readable, or writeable, or both.
36Reading or writing is always relative to the current position in the stream.
37Streams can easily be converted to collections, and vice versa.
30\chapter{Fluxos}\label{cha:streams}
31
32\clsindmain{Fluxos} (\foreign{streams}) são usados para iterar em sequências de elementos como coleções sequenciadas, arquivos e fluxos de rede.
33Fluxos podem ser de leitura, escrita, ou ambos.
34A leitura ou escrita é sempre relativa a posição atual no fluxo.
35Fluxos podem ser facilmente convertidos para coleções e vice-versa.
3836\lr{"Streams can easily be converted into collections." I wouldn't say it like this, because it is not true for all streams (infinite streams). According to Kent Beck we should only talk about conversion when the same protocol is supported. Collections and Streams do not support the same protocol. (p. 249)}
3937
4038%=============================================================
41\section{Two sequences of elements}
42A good metaphor to understand a stream is the following: A stream
43can be represented as two sequences of elements: a past element sequence
44and a future element sequence. The stream is positioned between the two
45sequences. Understanding this model is important since all stream
46operations in Smalltalk rely on it.
47For this reason, most of the \clsind{Stream} classes are subclasses of \clsind{PositionableStream}.
48\figref{_abcde} presents a stream which contains five characters. This stream is in its original position, \ie there is no element in the past. You can go back to this position using the message \mthind{PositionableStream}{reset}.
39\section{Duas sequências de elementos}
40Uma boa metáfora para entender um fluxo é a seguinte: Um fluxo pode ser representada como duas sequências de elementos: a sequência de elementos visitados e a sequência de elementos a visitar. O fluxo é posicionado entre as duas sequências. Entender este modelo é importante uma vez que todos as operações de fluxo em \st dependem dele.
41Por esta razão, a maioria das classes de \clsind{Fluxo} são subclasses de \clsind{PositionableStream}.
42\figref{_abcde} apresenta um fluxo que contém cinco caracteres. Este fluxo está na sua posição original, \ie não há elementos já visitados. Você pode voltar a esta posição usando a mensagem \mthind{PositionableStream}{reset}.
4943
5044\begin{figure}[ht]
5145\centerline{\includegraphics[scale=0.5]{_abcdeStef}}
52\caption{A stream positioned at its beginning.}
46\caption{Um fluxo posicionado em seu início.}
5347\label{fig:_abcde}
5448\vspace{.2in}
5549\end{figure}
5650
57Reading an element conceptually means removing the first element of the future element sequence and putting it after the last element in the past
58element sequence. After having read one element using the message \ct{next}, the state of your stream is that shown in \figref{a_bcde}.
51A leitura de um elemento conceitualmente significa remover o primeiro elemento da sequência de elementos a visitar e colocá-lo na sequência de elementos visitados. Após ter lido um elemento usando a mensagem \ct{next}, o estado do seu fluxo fica semelhante ao mostrado na \figref{a_bcde}.
5952
6053\begin{figure}[ht]
6154\centerline{\includegraphics[scale=0.5]{a_bcdeStef}}
62\caption{The same stream after the execution of the method \ct{next}: the character \ct{a} is ``in the past'' whereas \ct{b}, \ct{c}, \ct{d} and \ct{e} are ``in the future''.}
55\caption{O mesmo fluxo após a execução do método \ct{next}: o caractere \ct{a} está ``no passado'' enquanto \ct{b}, \ct{c}, \ct{d} e \ct{e} estão ``no futuro''.}
6356\label{fig:a_bcde}
6457\vspace{.2in}
6558\end{figure}
6659
67Writing an element means replacing the first element of the future sequence by the new one and moving it to the past. \figref{ax_cde} shows the state of the same stream after having written an \ct{x} using the message \mthind{Stream}{nextPut:} \ct{anElement}.
60A escrita de um elemento significa substituir o primeiro elemento da sequência de elementos a visitar por um novo e movê-lo para a lista de elementos já visitados. A \figref{ax_cde} mostra o estado do mesmo fluxo após ter sido escrito um \ct{x} usando a mensagem \mthind{Stream}{nextPut:} \ct{anElement}.
6861
6962\begin{figure}[h!t]
7063\centerline{\includegraphics[scale=0.5]{ax_cdeStef}}
71\caption{The same stream after having written an \ct{x}.}
64\caption{O mesmo fluxo após ter escrito um \ct{x}.}
7265\label{fig:ax_cde}
7366\vspace{.2in}
7467\end{figure}
7568
7669%=============================================================
77\section{Streams vs. collections}
78
79The collection protocol supports the storage, removal and enumeration
80of the elements of a collection, but does not allow these operations
81to be intermingled. For example, if the elements of an
82\clsind{OrderedCollection} are processed by a \mthind{OrderedCollection}{do:} method, it is not
83possible to add or remove elements from inside the \ct{do:} block.
84Nor does the collection protocol offer ways to iterate over two
85collections at the same time, choosing which collection goes forward
86and which does not. Procedures like these require that a traversal index or
87position reference is maintained outside of the collection itself:
88this is exactly the role of \clsind{ReadStream}, \clsind{WriteStream} and
70\section{Fluxos vs. coleções}
71
72O protocolo de coleções suporta armazenamento, remoção e enumeração de elementos de uma coleção, mas não permite que essas operações sejam misturadas. Por exemplo, se os elementos de um \clsind{OrderedCollection} são processados por um método \mthind{OrderedCollection}{do:}, não é possível adicionar ou remover elementos dentro desse bloco \ct{do:}.
73Tampouco o protocolo de coleções fornece uma forma de se iterar em duas coleções ao mesmo tempo, escolhendo qual coleção avança e qual não avança. Procedimentos como este exigem que o índice transversal ou posição de referência seja mantido fora da própria coleção: é exatamente este o papel de \clsind{ReadStream}, \clsind{WriteStream} e \clsind{ReadWriteStream}.
8974\clsind{ReadWriteStream}.
9075
9176These three classes are defined to \emph{stream over} some collection.
toggle raw diff