Learn Functional Programming by Implementing SQL with Underscore.js

Kris Jordan (New Media Campaigns)
Programming
Location: Portland 256
Average rating: ***..
(3.00, 2 ratings)

When web developers first approach functional programming libraries, like underscore.js, they are presented a scattered set of pieces without guidance on how they might fit together to form a complete and beautiful picture. As it turns out, most web developers are familiar with a “big picture” composition of functional programming pieces in the form of SQL.

The basic execution of a SQL query can be decomposed into a series of steps that join, filter, order, group, and transform collections of data. We will breaking down a SQL query into a pipeline of steps. At each step we’ll write an implementation in JavaScript using basic, functional techniques leveraging a breadth of the underscore.js library along the way. As we move along we will recompose our JavaScript pieces to fit beautifully together.

By the conclusion of the talk we will have implemented a basic SQL-like DSL for querying collections of in-memory objects in JavaScript. By translating your knowledge of SQL into functional programming terms you will more easily identify practical uses and compositions of functional programming concepts. You will also approach SQL from a fresh perspective of an implementer and viscerally learn things like why web scale databases avoid JOINs like the plague and why that one query of yours is might be running so slow.

Outline:

  1. Why care about functional programming in JavaScript?
    1. Small, simple, testable pieces
    2. Powerful, practical, flexible compositions
  2. How does a SQL query work?
    1. `from` and `join` identify the sources
    2. `where` filters data based on criteria
    3. `order by` sorts data based on values and direction
    4. `select` projects a subset of the data
  3. Translating our SQL knowledge into Functional Programming techniques
    1. Brief overview of higher-order JavaScript functions and function generators
    2. Applying underscore.js’ `filter` function to write `where`
    3. Wraping around `sortBy`
    4. Rolling our own `project` with `each` and `map`
  4. Composing our pieces into a simple DSL
    1. Bringing it all together
    2. So why are JOINs so scary for web scale databases?
  5. Conclusion and Questions
Photo of Kris Jordan

Kris Jordan

New Media Campaigns

Kris is founder and CTO of New Media Campaigns, a development consulting firm based in North Carolina’s Research Triangle. He leads the technical direction of the company. He is the architect of GetHiFi.com, the company’s SaaS CMS, built on an open source stack, and is trusted by customers including Bandwidth.com, The Rhodes Scholarship Foundation, Gannett, Saranac Beer, EpiPen, CloudSwitch, Country Crock, Fleet Feet Sports, UNC Chapel Hill, and hundreds of other businesses, non-profits, and universities.

Kris has developed at all tiers of the web development stack, but currently prefers back end architecture and RESTful HTTP API design with node.js. He recently contributed to the WebKit project to fix a bug in its Accept header preventing RESTful APIs from being able to properly support content negotiation. Kris’ framework Recess was the first open source PHP framework to focus on RESTful design and was used by developers worldwide including companies like Disney.

Prior to founding New Media Campaigns, Kris earned a Masters in Computer Science from Brown University and a BS with Honors in CS from UNC Chapel Hill. Kris has been programming professionally since his first high school job hacking on e-commerce sites powered by PHP4 and MySQL at a small dial-up ISP in rural North Carolina. He loves his wife, his crazy dog, and a good IPA.

Comments on this page are now closed.

Comments

Grant Musick
07/20/2012 12:20pm PDT

Good and approachable introduction to functional programming for those who haven’t done it before.

Ben Standefer
04/25/2012 5:31pm PDT

Is this an elaborate troll? Props if it is.

Sponsors

For information on exhibition and sponsorship opportunities at the conference, contact Sharon Cordesse at (707) 827-7065 or scordesse@oreilly.com.

View a complete list of OSCON contacts