Functional JavaScript – Applying functional techniques and Underscore.js – #programming #bookreview

Functional JavaScript
Introducing Functional Programming with Underscore.js
Michael Fogus
(O’Reilly – paperback, Kindle)

When I first started tinkering with JavaScript, object-oriented programming (OOP) was the rage, and JavaScript frequently was touted as one of the key object-oriented languages. After all, according to many online descriptions, almost everything within JavaScript “is OO.”

Now, in this enjoyable new book, JavaScript is hailed as a functional programming language. Of course, functional programming recently has been the rage in many programming circles. And “enjoyable” may seem an odd word to attach to a software text. Yet, it fits. Michael Fogus shows here that he is a technical writer who can be entertaining, effective and well-organized.

“This book,” he states, “is not about spewing dogma regarding the virtues of functional programming. I think there are many functional techniques that offer ways to rein in the complexities of software development, but I realize that at times, there are better ways to implement any given individual part.”

It is possible, of course, to debate object-oriented vs. functional JavaScript until the digital cows are called back home and put into infinite loops. But, for those who don’t know or care much about the differences, here are some basic views.

The Mozilla Developers Network (to simply pick  one example) discusses objected-oriented JavaScript on its site and declares: “Object-oriented programming may be seen as the design of software using a collection of cooperating objects, as opposed to a traditional view in which a program may be seen as a collection of functions, or simply as a list of instructions to the computer. In OOP, each object is capable of receiving messages, processing data, and sending messages to other objects. Each object can be viewed as an independent little machine with a distinct role or responsibility.”

Meanwhile, HaskellWiki offers this explanation of functional programming: “In functional programming, programs are executed by evaluating expressions, in contrast with imperative programming where programs are composed of statements which change global state when executed. Functional programming typically avoids using mutable state.

“Functional programming requires that functions are first-class, which means that they are treated like any other values and can be passed as arguments to other functions or be returned as a result of a function. Being first-class also means that it is possible to define and manipulate functions from within other functions.”

Some of the programming languages typically labeled “functional” include Clojure, OCaml, Erlang,  Haskell, Scala, and F#.

Here is how Michael Fogus defines functional programming:

“As a bare-bones introduction, functional programming can be described in a single sentence:

Functional programming is the use of functions that transform values into units of
abstraction, subsequently used to build software systems.

“This is a simplification bordering on libel,” he continues, “but it’s functional (ha!) for this early stage in the book. The library that I use as my medium of functional expression in JavaScript is Underscore, and for the most part, it adheres to this basic definition.”

(Underscore.js can be obtained from and imported into “the applicable project directories.”)

Fogus refuses, in his text, “to create a false dichotomy and say that functional and object-oriented styles should stand in opposition.” Indeed, he notes that JavaScript supports both models and “systems can and should be composed of both models.”

He also points out that JavaScript can be used with other paradigms, including imperative programming, prototype-based object-oriented programming, and metaprogramming.

“In no way does this book represent even a modicum of original thinking regarding functional programming in JavaScript,” he states. Yet, it is a worthy effort.

It is well written, smoothly organized, and nicely illustrated with short code examples and helpful diagrams. And Fogus notes that JavaScript does have “[l]anguage oddities, unsafe features, and a sea of competing libraries” that raise concerns when it is selected for a project. Functional programming, he contends, can be one way to help ensure that JavaScript “can be not only safe, but also simple to understand and test, in addition to being proportionally scalable to the size of the code base.”

Here is the chapter lineup for Fogus’s 237-page book:

  • Chapter 1. Introducing Functional JavaScript
  • Chapter 2. First-Class Functions and Applicative Programming
  • Chapter 3. Variable Scope and Closures
  • Chapter 4. Higher-Order Functions
  • Chapter 5. Function-Building Functions
  • Chapter 6. Recursion
  • Chapter 7. Purity, Immutability, and Policies for Change
  • Chapter 8. Flow-Based Programming
  • Chapter 9. Programming without Class

Functional JavaScript is a winner on at least two counts: (1) as a how-to text for long-time JavaScript programmers wishing to learn more about functional programming; and (2) as a how-to text for long-time functional programmers desiring to learn more about JavaScript.

The book is not recommended for JavaScript newcomers who are still at the level of typing console.log(“The answer to everything in the universe is 42”). However, experienced beginners can learn from it, and so can those of us who just tinker with JavaScript periodically and use it mainly to work with Node.js, Backbone.js, Ember.js, CoffeeScript, HTML and other choices.

Si Dunn

Developing Web Applications with Haskell and Yesod – #programming #bookreview #in

Developing Web Applications with Haskell and Yesod
Michael Snoyman
paperback, list price $34.99; Kindle edition
, list price $27.99)

Haskell and Yesod? Aren’t they that comedy team playing at the new club downtown?

No, but they do work together. And they can work together very well, as this new book shows.

Haskell is “a powerful, fast, type-safe, functional programming language” used in Web development. It is said to be a great language for “pure computation,” but it’s rated “not so hot” for scripting.

Yesod, on the other hand, is a Haskell web framework for productive development of type-safe, RESTful, high performance web applications.

Some people say you don’t really need to know Haskell if you use Yesod. And others recommend that you do indeed need to understand “what your tools are doing.” And that includes having some experience with Haskell.

Michael Snoyman, who created Yesod, states that his new book “takes as an assumption that you are already familiar with most of the basics of Haskell.” If you are not, he recommends getting some Haskell basics first from two other books: Real World Haskell and Learn You a Haskell for Great Good! (That’s really the title.)

Developing Web Applications with Haskell and Yesod is well written and appropriately illustrated with many code examples. The 280-page book is divided into four parts, including 21 chapters and seven appendices. The first 14 chapters focus on working with short code examples. And the pace ramps up to writing “a real site” starting in Chapter 15.

A few of the key topics covered in the book include:

  • Building a simple application to learn Yesod’s foundation data type and Web Application Interface (WAI).
  • Using Shakespearean template languages for HTML, CSS, and JavaScript output.
  • Learning how Yesod monads interact, so you can produce cleaner, more modular code.
  • Learning how to implement the yesod-form declarative API so you can build forms on top of widgets.
  • How Yesod and Haskell store session data and handle persistence.

Along with showing you how to create “a production-quality web application with Yesod’s ready-to-use scaffolding,” Snoyman’s book also helps you examine several real-world examples, including “a blog, a wiki, a JSON web service, and a Sphinx search server.”

Si Dunn