About Gio
I am a torrent of ingenuity (or insanity) with a myriad of innovations (sometimes fallacies) and a wealth of inspiration (possibly naiveté). My name is Gio Carlo Cielo Borje and I like puffer fish because they're just cooltalkin', highwalkin' and fastlivin'.
I'm also nineteen and a current student at UC Irvine for Computer Science.
Underscorejs: Text Processing on the Document Object Model (DOM)
Last time, we dove into higher-order functions that Underscorejs provides. This time, we’ll be utilizing those higher-order functions to process text on the DOM of a page.
The Problem
Given three paragraphs of text in HTML, find and print all big words within the text. Big words shall be defined by anything with more than eleven characters. The initial set of paragraphs will be defined within an
<article>block identified bylipsum. The solution should be printed to the<ul>identified bylong_words.Algorithm Outline
We want to outline the algorithm before we begin programming so that we’re aware of our situation, our objective and how to achieve that objective using our algorithm.
<article>.<ul>Context of the Problem
I have written the problem into an HTML5 document to demonstrate the structure as per problem definition. Note here that I am using HTML5 instead of HTML4 or XHTML2.0. Additionally, we use lipsum are our boilerplate.
Walking through the Underscorejs Algorithm
According to our algorithm, we must first extract the text from the paragraphs within the
<article>defined bylipsum.We can start by gaining control of the
<article>tag throughdocument.getElementById. Afterwards, we need to acquire a list of all paragraph nodes under our article. Underscorejs easily allows us to do this by filteringchildNodesof the article against theirnodeName; specifically, we filter against thenodeNameof the<p>tag. Beware: all HTML elements have capitalized names.At this point, we have a list of all
<p>tags that are children of the containing<article>.Now we can simply start
_.chainthe list of paragraphs and process it through a set of higher-order functions sequentially to acquire our new list of big words.Union the List of Paragraphs
First, we need to coalesce the paragraphs’ text into a single list. We can do this by first replacing all non-alphanumeric symbols and extraneous spaces with a single space through
replace. Afterwards, we simplysplitthe paragraph according to spaces such that only words are given to us in our new list.This list processing can be done with
_.mapby mapping the list of paragraph to a list of words within the specified paragraphs.Second, once the list of words have been generated, it is a deep list i.e. it contains a list of list of words originally within our paragraphs. This can easily be subverted by
_.flattenwhich turns our list into a shallow list i.e. inner lists coalesce into the containing list.Now, our paragraphs should have been successfully unioned into a list words as shown below.
Filter the List of Words
This section is trivial though necessary to our objective: filter the following list to return all big words which have more than eleven characters. Simply, we will chain the
_.filterfunction further and return the final list of words by ending our_.chainwith_.value.We have successfully acquired the list of big words. Now, the final task is to print the list into the DOM.
Printing to the DOM
Instead of polluting our function space, we will delegate the printing to an auxiliary function. In this function, we must create a document fragment to begin storing new
<li>elements.For each of the words we pass to the function, it will create a new list element, insert a long word into it as a text node and append it the document fragment.
_.eachdoes this for us trivially.Finally, we append the list of list elements to the unordered list identified by
long_words.Voila! We have successfully printed to the DOM.
Putting It All Together Now
Conclusion
Higher-order functions significantly simplify the processing of stream-based or list-based data such as text.
Using JavaScript’s interface into the DOM, we were able to show how functional programming (though impure) may have a place on the web to simplify common computational tasks and even DOM tasks.