concept control flow in category functional programming

appears as: control flows, control flow, control flow
Functional Programming in JavaScript

This is an excerpt from Manning's book Functional Programming in JavaScript.

In simple terms, functional programming is a software development style that places a major emphasis on the use of functions. You might say, “Well, I already use functions on a day-to-day basis at work; what’s the difference?” As I mentioned earlier, FP requires you to think a bit differently about how to approach the tasks you’re facing. It’s not a matter of just applying functions to come up with a result; the goal, rather, is to abstract control flows and operations on data with functions in order to avoid side effects and reduce mutation of state in your application. I know this sounds like a mouthful, but I’ll visit each of these terms further and build on them throughout the book.

You’ll also learn to use a functional JavaScript library called Lodash.js. It lets you process and understand not only the structure of your application, but also the structure of your data. In addition, I’ll discuss the important role recursion plays in functional programming and the advantages of being able to think recursively. Building on these concepts, you’ll learn to write concise, extensible, and declarative programs that clearly separate control flow from the main logic of your code.

3.1. Understanding your application’s control flow

The path a program takes to arrive at a solution is known as its control flow. An imperative program describes its flow or path in great detail by exposing all the necessary steps needed to fulfill its task. These steps usually involve lots of loops and branches, as well as variables that change with each statement. At a high level, you can depict a simple imperative program like this:

Functional Programming in Scala

This is an excerpt from Manning's book Functional Programming in Scala.

  • As we just discussed, exceptions break RT and introduce context dependence, moving us away from the simple reasoning of the substitution model and making it possible to write confusing exception-based code. This is the source of the folklore advice that exceptions should be used only for error handling, not for control flow.
  • 13.3.1. Reifying control flow as data constructors

    The answer is surprisingly simple. Instead of letting program control just flow through with function calls, we explicitly bake into our data type the control flow that we want to support. For example, instead of making flatMap a method that constructs a new IO in terms of run, we can just make it a data constructor of the IO data type. Then the interpreter can be a tail-recursive loop. Whenever it encounters a constructor like FlatMap(x,k), it will simply interpret x and then call k on the result. Here’s a new IO type that implements that idea.

    Listing 13.4. Creating a new IO type
    sealed trait IO[A] { def flatMap[B](f: A => IO[B]): IO[B] = FlatMap(this, f) def map[B](f: A => B): IO[B] = flatMap(f andThen (Return(_))) } case class Return[A](a: A) extends IO[A] #1 case class Suspend[A](resume: () => A) extends IO[A] #2 case class FlatMap[A,B](sub: IO[A], k: A => IO[B]) extends IO[B] #3 #1: A pure computation that immediately returns an A without any further steps. When run sees this constructor, it knows the computation has finished. #2: A suspension of the computation where resume is a function that takes no arguments, but has some effect and yields a result. #3: A composition of two steps. Reifies flatMap as a data constructor rather than a function. When run sees this, it should first process the subcomputation sub and then continue with k once sub produces a result.

    This new IO type has three data constructors, representing the three different kinds of control flow that we want the interpreter of this data type to support. Return represents an IO action that has finished, meaning that we want to return the value a without any further steps. Suspend means that we want to execute some effect to produce a result. And the FlatMap data constructor lets us extend or continue an existing computation by using the result of the first computation to produce a second computation. The flatMap method’s implementation can now simply call the FlatMap data constructor and return immediately. When the interpreter encounters FlatMap(sub, k), it can interpret the subcomputation sub and then remember to call the continuation k on the result. Then k will continue executing the program.