liveBook · Manning

Pipe

As explained in Functional Programming in Scala, Second Edition

A Pipe is a stream-processing abstraction that transforms input values into output values, potentially while evaluating effects.

A Pipe names an independent stream transformation: a function that accepts a stream of input values and produces a stream of output values. The name adds no expressiveness, but makes transformations easier to describe, reuse, and compose. section 15.2.2

Build pipes as ordinary values, then compose or apply them like functions:

  • Define focused transformations such as nonEmpty, which filters empty strings, and lowerCase, which maps each string to lowercase. section 15.2.2
  • Compose transformations with function composition: normalize = nonEmpty andThen lowerCase. section 15.2.2 section 15.6
  • Apply a pipe directly as normalize(lines), or use scala.util.chaining.pipe to write lines.pipe(normalize). section 15.2.2
  • Generalize the alias to Pipe[F, I, O] when streams may evaluate effects of F while transforming inputs I into outputs O. section 15.3 section 15.6

Converting input lines

A conversion pipeline gives each step one responsibility: trim lines, remove empty lines and comments, parse valid numbers, then convert them to Celsius. Composing these steps yields a Pipe[String, Double] that can process file input before folding the results into an output file. section 15.6

type Pipe[-I, +O] = Stream[I] => Stream[O]

The core type alias makes the abstraction concrete: Pipe[-I, +O] = Stream[I] => Stream[O]. section 15.2.2

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Defines a function alias, demonstrates composition and application, then extends it to effectful streams without detailing all stream internals.
Examples
Uses filtering, lowercasing, Boolean existence checks, line conversion, and file-to-file Celsius conversion.
What it assumes
Assumes functions and streams, while motivating function composition through concrete filtering, mapping, and file-processing examples.
What you can do after
Apply it via ordinary function calls or scala.util.chaining.pipe, and compose reusable transformations into effectful streaming programs.
How it connects
Connects Stream, Pull, function composition, effects, Monad, and resource-safe file and network processing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in Scala, Second Edition. The section links point to the parts of the book it draws on.