Gettin’ Freaky Functional w/Curried JavaScript

By on in Web

Partial application refers to the practice of filling in a functions parameters with arguments, deferring others to be provided at a later time. JavaScript libraries like Underscore facilitate partial function application – but the API isn’t for everyone. I, for one, feel icky sprinkling calls to _.partial and _.bind throughout my application.

Curried functions (found in Haskell and supported by Scala, among others) can be partially-applied with little ceremony; no special call format is required. In this blog post, I’ll demonstrate an approach to currying JavaScript functions at the time of their definition in a way that enables partial function application without introducing lots of annoying parens and nested function expressions.

Continue reading …


Vintage Terminal Effect in CSS3

By on in Web

Recently I revamped my personal website. For the most part I kept it simple, having it generated by Middleman with styling and layout provided by Bootstrap. However, I wanted my header to reflect my love of the command-line and 80s nostalgia by having it act like a vintage terminal similar to Cathode app: scanlines, screen burn, green glow, with blinking cursor. Most importantly, I wanted to do it with CSS3 animations and no Javascript to minimize the impact on browsers. Building on the work of Lea Verou and Anders Evenrud, here’s how I did it. Continue reading …


Tidying Up a JavaScript Application with Higher-Order Functions

By on in Web

Higher-order functions are functions that can do one or both of the following:

  1. Take a function or functions as arguments
  2. Return a function as an argument

Most programmers by now are familiar with higher-order functions such as map, reduce, and filter. These functions abstract away the boilerplate when processing collections. For example…

Given a string-capitalizing function and an array of arrays of strings:

function capitalize(s) {
  return s.charAt(0).toUpperCase() + s.slice(1).toLowerCase();
}

var fruitsOfTheWorld = [
  ["apple", "pineapple", "pear"],
  ["manzana", "pera", "piña"],
  ["poma", "perera", "ananàs"]
];

…we can turn this:

var results = [], 
    i = fruitsOfTheWorld.length;

while (i--) {
  results.unshift([]);
  var j = fruitsOfTheWorld[i].length;
  while (j--) {
    results[0].unshift(capitalize(fruitsOfTheWorld[i][j]));
  }
}

return results;

…into this:

return fruitsOfTheWorld.map(function(fruits) {
  return fruits.map(capitalize);
});

In this post, I’ll demonstrate the usage of higher-order functions outside of a collection-processing context – with the ultimate goal of reducing boilerplate code in your real-world applications (as filter did above). We’ll start with partial function application (facilitated by Underscore’s _.partial) and move on to writing our own higher-order functions.

Continue reading …


Composing Data Pipelines: (Mostly) Stateless Web Applications in Clojure

By on in Web

I describe building an application in a functional style as the act of composing many smaller, context-free functions into pipelines of data transformations which map from system inputs to outputs.

marble

Compojure (a Clojure web application library similar to Bottle and Sinatra) represents this input as a simple, immutable hash map which is transformed through a pipeline of middleware functions to a new hash map representing an HTTP response. The code of the resulting application resembles one in which we’re applying a mapping function to values in a static collection rather than ones in which we’re working with HTTP requests. By favoring expressions over statements, immutable data and functions over mutable objects and methods, our web application will share many of the positive characteristics of its static sequence-mapping cousins – in terms of simplicity (we can think about each transformation step independently from others) and robustness (our functions are small; their behavior easy to reason about and test).

Continue reading …


WebRTC Made Simple

By on in Web

WebRTC is a set of JavaScript APIs that enable peer-to-peer, realtime communication between web browsers. It offers some pretty amazing capabilities, but getting through even a basic introduction to it can be daunting. The variety of new APIs and protocols is a lot to digest. This post is a simple guide to getting up-and-running with WebRTC.

pushrtc

Continue reading …


Node.js in Production

By on in Ops, Web

When running a node application in production, you need to keep stability, performance, security, and maintainability in mind. Outlined here is what I think are the best practices for putting node.js into production.

By the end of this guide, this setup will include 3 servers: a load balancer (lb) and 2 app servers (app1 and app2). The load balancer will health check and balance traffic between the servers. The app servers will be using a combination of systemd and node cluster to load balance and route traffic around multiple node processes on the server. Deploys will be a one-line command from the developer’s laptop and cause zero downtime or request failures.

It will look roughly like this:

Photo credit: Digital Ocean

Continue reading …


An Incremental Migration from Rails Monolithic to Microservices

By on in Everything Else, Microservices, Rails, Ruby, Web

Your Rails application has become a monolith. Your test suite takes 30 minutes to run, your models grow to several hundred (or thousand!) lines, and feature development slows to a crawl. New members of your team must read through massive amounts of code before they can feel confident making changes. You’ve watched Uncle Bob’s Architecture the Lost Years, Matt Wynne’s Hexagonal Rails and read Martin Fowler’s Microservices and are convinced that the time has come to start breaking things up into smaller, simpler, faster components – but you’re unsure of where to begin.

In this article, I will demonstrate an approach to breaking a monolithic-style Rails application apart into microservices using plain Ruby classes and a simple RPC framework – all deployed to Heroku. I’ll show you how to work through this migration incrementally; you’ll be able to decompose things down as far as makes sense for your codebase. As a special bonus, I’ll show you how you can use Barrister RPC to alleviate much of the pain associated with documenting and creating API clients for the components of your now-distributed system. I’m skipping the advocacy; if you’re here it’s because you already love yourself some microservices but don’t know how to implement them yourself.

Continue reading …