↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

Thinking about Stream and Enum in Terms of Function Composition

The post reads Enum and Stream as tools for function composition rather than as generalized arrays. Enum is built from a collection’s basic functions, especially reduce, and its helpers are shortcuts over that mechanism.

Reading notes
#

  • Enum’s basic protocol methods are count, member?, and reduce, not an each method.
  • Enum uses reduce to implement higher-level operations such as all?.
  • These operations can stop early when the result is already determined.
  • Stream is similar, but it does not execute reduce right away.
  • Stream creates an anonymous function that transforms one collection into another.
  • A Stream-based collection can be used where a regular collection is used.
  • The example with 1..10 mapped by Stream shows lazy evaluation when Enum.take is called.
  • A collection defined by Stream does not imply state and remains immutable.
  • Each use of a Stream starts from the beginning again.
  • The choice to use Stream depends on the tradeoff between storing a collection and creating it at runtime.
  • Stream supports reduce, but count and member? from Enumerable fail directly.
  • Enum.count can emulate count by using reduce.