O'Reilly logo

Functional Programming: A PragPub Anthology by Michael Swaine

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A Practical Example

Functional programming emphasizes immutability, but it’s equally about designing with state transformation and function composition.

In object-oriented programming, we strive for good object composition. In functional programming, we design with function composition. Rather than mutating state, state is transformed as it flows through the sequence of functions.

Let’s construct an example to see what this difference between imperative and functional style looks like in practice. Let’s say we’re given a list of ticker symbols and our goal is to find the highest-priced stock not exceeding $500.

Let’s start with a sample list of ticker symbols.

 val tickers = List("AAPL", "AMD", "CSCO", "GOOG", "HPQ", "INTC",
  "MSFT", ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required