Chapter 7. Using, Optimizing, and Testing Functional Techniques

image with no caption

This chapter shows some recursive and other functional solutions to simple problems, as well as some ways we can test and improve these solutions. Two very common programming topics that demonstrate functional programming are the factorial and Fibonacci mathematical series—largely because they’re so easily described using recursive means.[20]

The factorial of a given positive number is the product of all the integers from 1 to that number, so factorial(3) = 3 x 2 x 1, factorial(5) = 5 x 4 x 3 x 2 x 1, and so on. This can be expressed generally as:

factorial(x) = x x (x – 1) x (x – 2) ...

Get Ruby by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.