Databases Reference
In-Depth Information
10.4.2
Using recursion to process unstructured document data
If you're familiar with LISP , you know that recursion is a popular construct in functional
programming. Recursion is the process of creating functions that call themselves. In
our experience, people either love or hate recursion—there's seldom a middle
ground. If you're not comfortable with recursion, it can be difficult to create new
recursive programs. Yet once you create them, they seem to almost acquire a magical
property. They can be the smallest programs that produce the biggest results.
Functional programs don't manage state, but they do use a call stack to remember
where they are when moving through lists. Functional programs typically analyze the
first element of a list, check whether there are additional items, and if there are, then
call themselves using the remaining items.
This process can be used with lists as well as tree structures like XML and JSON files.
If you have unstructured documents that consist of elements and you can't predict the
order of items, then recursive processing may be a good way to digest the content. For
example, when you write a paragraph, you can't predict the order in which bold or
italic text will appear in the paragraph. Languages such as XQuery and JASONiq sup-
port recursion for this reason.
10.4.3
Moving from mutable to immutable variables
As we've mentioned, functional programming variables are set once but not changed
within a specific context. This means that you don't need to store a variable's state,
because you can rerun the transform without the side effects of the variable being
incremented again. The downside is that it may take some time for your staff to rid
themselves of old habits, and you may need to rewrite your current code to work in a
functional environment. Every time you see variables on both the left and right side of
an assignment operator, you'll have to modify your code.
When you port your imperative code, you'll need to refactor algorithms and
remove all mutable variables used within for loops. This means that instead of using
counters that increment or decrement variables in for loops, you must use a “for each
item” type function.
Another way to convert your code is to introduce new variable names when you're
referencing a variable multiple times in the same block of code. By doing this, you can
build up calculations that nest references on the right side of assignment statements.
10.4.4
Removing loops and conditionals
One of the first things imperative programmers do when they enter the world of func-
tional programs is try to bring their programming constructs with them. This includes
the use of loops, conditionals, and calls to object methods.
These techniques don't transfer to the functional programming world. If you're
new to functional programming and in your functions you see complex loops with lay-
ers of nested if/then/else statements, this tells you it's time to refactor your code.
Search WWH ::




Custom Search