Databases Reference
In-Depth Information
Let's take a look at a single operation on an object. In this example, the objective is to
increment a single variable. The code might read x = x + 1 , meaning “x is assigned a
new value of itself plus one.” What if one computer or part of a network crashes right
around this line of code? Does that code get executed? Does the current state repre-
sent the correct value or do you need to rerun that line of code?
Keeping track of the state of an ever-changing world is easy on a single system, but
becomes more complex when you move to distributed computing. Functional pro-
gramming offers a different way of approaching computation by recasting computa-
tion as a series of independent data transformations. After you make this transition,
you don't need to worry about the details of state management. You move into a new
paradigm where all variables are immutable and you can restart transforms again with-
out worrying about what external variables you've changed in the process.
10.1.2
Functional programming is parallel transformation
without side effects
In contrast with keeping track of the state of objects, functional programming focuses
on the controlled transformation of data from one form to another with a function.
Functions take the input data and generate output data in the same way that you cre-
ate mathematical functions in algebra. Figure 10.4 is an example of how functional
programs work in the same way mathematical functions work.
It's important to note that when using functional programming there's a new con-
straint. You can't update the state of the external world at any time during your trans-
form. Additionally, you can't read from the state of the external world when you're
executing a transform. The output of your transform can only depend on the inputs to
the function. If you adhere to these rules, you'll be granted great flexibility! You can
execute your transform on clusters that have thousands of processors waiting to run
your code. If any processor fails, the process can be restarted using the same input
data without issue. You've entered the world of side-effect-free concurrent program-
ming, and functional programming has opened this door for you.
The concepts of serial processing are rooted in imperative systems. As an example,
let's look at how a for loop or iteration is processed in a serial versus a parallel process-
ing environment. Whereas imperative languages perform transformations of data
Figure 10.4 Functional programming works similarly to mathematical functions. Instead of methods
that modify the state of objects, functional programs transform only input data without side effects.
Search WWH ::




Custom Search