Stream (abstract data type)

{{Short description|Potentially infinite analog of a list, in type theory and functional programming}}

{{for|the more applied concept in computing|Stream (computing)}}

{{More references|date=February 2020}}

In type theory and functional programming, a stream is a potentially infinite analog of a list, given by the coinductive definition:{{Citation |last=Yamagiwa |first=Shinichi |title=Stream-Based Lossless Data Compression |date=2022 |work=Sublinear Computation Paradigm: Algorithmic Revolution in the Big Data Era |pages=391–410 |editor-last=Katoh |editor-first=Naoki |place=Singapore |publisher=Springer |language=en |doi=10.1007/978-981-16-4095-7_16 |isbn=978-981-16-4095-7 |editor2-last=Higashikawa |editor2-first=Yuya |editor3-last=Ito |editor3-first=Hiro |editor4-last=Nagao |editor4-first=Atsuki|doi-access=free }}{{Cite journal |last1=Sangma |first1=Jerry W. |last2=Sarkar |first2=Mekhla |last3=Pal |first3=Vipin |last4=Agrawal |first4=Amit |last5=Yogita |date=2022-04-01 |title=Hierarchical clustering for multiple nominal data streams with evolving behaviour |journal=Complex & Intelligent Systems |language=en |volume=8 |issue=2 |pages=1737–1761 |doi=10.1007/s40747-021-00634-0 |issn=2198-6053|doi-access=free }}

data Stream α = Nil | Cons α (Stream α)

Generating and computing with streams requires lazy evaluation, either implicitly in a lazily evaluated language or by creating and forcing thunks in an eager language. In total languages they must be defined as codata and can be iterated over using (guarded) corecursion.

File:UML dotnet streams.svg

Java provides the {{Mono|Stream}} interface under the {{Mono|java.util.stream}} namespace.

JavaScript provides the {{Mono|ReadableStream}}, {{Mono|WritableStream}} and {{Mono|TransformStream}} interfaces.{{cite web |title=Streams API - Web APIs {{!}} MDN |url=https://developer.mozilla.org/en-US/docs/Web/API/Streams_API |website=developer.mozilla.org |access-date=28 January 2025 |date=26 July 2024}}

Python have the {{Mono|StreamReader}} and {{Mono|StreamWriter}} classes in the {{Mono|asyncio}} module.{{cite web |title=Streams |url=https://docs.python.org/3/library/asyncio-stream.html |website=Python documentation |access-date=17 December 2024 |language=en}}

.NET provides the abstract class {{Mono|Stream}}{{cite web |title=Stream Class (System.IO) |url=https://learn.microsoft.com/en-us/dotnet/api/system.io.stream?view=net-9.0 |website=learn.microsoft.com |publisher=Microsoft |access-date=17 December 2024 |language=en-us}} which is implemented by classes such as {{Mono|FileStream}} and {{Mono|MemoryStream}}.{{cite web |title=File and Stream I/O - .NET |url=https://learn.microsoft.com/en-us/dotnet/standard/io/ |website=learn.microsoft.com |publisher=Microsoft |access-date=17 December 2024 |language=en-us |date=15 September 2021}}

In Rust a struct can implement the {{Mono|Read}} trait.{{cite web |title=Read in std::io - Rust |url=https://doc.rust-lang.org/std/io/trait.Read.html |website=doc.rust-lang.org |access-date=28 January 2025}} There is also the {{Mono|Cursor}} struct wraps an in-memory buffer.{{cite web |title=Cursor in std::io - Rust |url=https://doc.rust-lang.org/std/io/struct.Cursor.html |website=doc.rust-lang.org |access-date=28 January 2025}}

See also

References