Skip to Content
Mastering Functional Programming
book

Mastering Functional Programming

by Anatolii Kmetiuk
August 2018
Intermediate to advanced
380 pages
10h 2m
English
Packt Publishing
Content preview from Mastering Functional Programming

A naive implementation of the heterogeneous list

Consider that we have the following list:

val list: List[Any] = List(0, 2.0, "3", Fraction(4, 2))

So, in the preceding list, we have elements of different types, and hence we are forced to declare this list as List[Any]. Fraction is defined for the purpose of our example as follows:

case class Fraction(numerator: Int, denominator: Int)

Notice that in the preceding list, each element can be represented as a floating-point number. They have different types, but it is possible to define certain common behavior on all of these types. Since the elements are very similar to each other, we might want to perform a common operation on them. For example, we may want to find the sum of all the numbers ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Functional Programming

Learning Functional Programming

Jack Widman

Publisher Resources

ISBN: 9781788620796Supplemental Content