Chapter 3Functional Programming in Dart

Some of what makes JavaScript special is its support for functional programming. Since Dart aims to be familiar, let’s examine what it is like to program functionally in Dart.

We begin with the old standby, the Fibonacci sequence. In JavaScript, this might be written like so:

 
function​ fib(i) {
 
if​ (i < 2) ​return​ i;
 
return​ fib(i-2) + fib(i-1);
 
}

The Fibonacci sequence is a wonderful example for exploring the functional programming nature of a language since it, er, is a function but also because it demonstrates how to invoke a function because of its recursive nature.

I won’t bother describing recursion or the particulars of this function.[5] Instead, let’s focus on how to use the function ...

Get Dart 1 for Everyone 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.