What is FS2 (Functional Streams for Scala)?
Answer
FS2 is a purely functional streaming library for Scala built on Cats Effect. The core type is Stream[F[_], O] — a potentially infinite stream of values of type O, with effects in F. Key features: Resource safety: streams acquire/release resources safely even in the presence of errors or cancellation. Concurrency: stream.parEvalMap(n)(f) processes n elements concurrently while preserving order. Pull-based: consumers control the rate. Backpressure is automatic. Common operations: stream.through(pipe), merge, zip, chunks. Use case: reading a large file line by line without loading into memory: Files[IO].readUtf8Lines(path).filter(_.nonEmpty).evalMap(processLine).compile.drain. FS2 is used in http4s for HTTP streaming, Doobie for streaming database results, and general event processing pipelines. It is the Cats Effect ecosystem's answer to Akka Streams.