Bulldog in the Dream

Read this first

Breaking out of the For Loop: Pragmatic Functional Techniques in JavaScript

While JavaScript’s simplicity and small size is often an advantage, JS programmers sometimes must restort to techniques that are too low-level and convulted for the problem at hand. With no standard library to speak of, convinient abstraction mechanisms available in many other languages are often lacking from JavaScript.

A common scenario for many JS developers is extracting information from a list of objects. Consider the following datastructure.

var sales = [{ customer: 'Kurt', item: 'cup', price: 14.99 }, 
             { customer: 'Siri', item: 'drone', price: 1095.00 }, 
             { customer: 'Kurt', item: 'book', price: 22.50 }, 
             { customer: 'Lisa', item: 't-shirt', price: 25.00 }];

Let’s say we want to know if a particular item has been sold. JavaScript has no any-function, so we have to iterate over the list ourselves, either using recursion or a loop. In this...

Continue reading →


The How to Design Functions Recipe

There’s an interesting course at Coursera — Introduction to Systematic Program Design — that teaches a spesific design process for programming. The course uses Racket, a Scheme dialect, but the design techniques are easily transferable to any language.

One of the fundamental ideas is a design method for constructing functions. The link describes the process in detail, I will illustrate a simplified process using JavaScript.

Let’s say you are to implement a function that computes the factorial of a number.

Step 1: Signature, purpose and stub

The first step is creating the signature for the function. A signature is simply a statement of the type of the input argument and the output argument. In our case that is: Number -> Number.

The purpose is a short description of what the function does, in our case: computes the factorial of n

Nest we make a stub, the outer shell of the function...

Continue reading →


Page Specific JavaScript in Rails

Rails and the Asset Pipeline

The Rails asset-pipeline compresses and minifies your JavaScript (or CoffeeScript) and CSS files. It combines your separate JavaScript files into one big file that is delivered by the server, and thereby reduces the number of requests the browser needs to make.

This is well and good, but what happens when you have page-specific JavaScript-files, i.e. scripts you only want to run when certain pages are requested? By default Rails will include these scripts in all the pages.

Page Specific JavaScript

Let’s say you have a controller users and a corresponding view. You also have a file app/assets/javascripts/users.js that you only want to run when the users page is requested. To make this happen you need to do two things.

First, remove the line //= require_tree . from app/assets/javascripts/application.js. This line instructs Rails to combine all the JS...

Continue reading →


Rapid Prototyping with Meteor.js

This tutorial assumes that you have Node.js and Meteor installed.

Step 1: Create a meteor app

Creating a new app in Meteor is very easy:

 $ meteor create chat

To start Meteor we cd into the newly created directory and simply type meteor:

 $ cd chat
 $ meteor

If we now go to http://localhost:3000/ in our browser, we are greeted by the following welcome message from Meteor.

Skjermbilde 2014-02-24 kl. 22.52.51.png

If we inspect the contents of the chat directory with the ls command, we see that Meteor has created one directory .meteor for us and three files: chat.css, chat.html and chat.js

The .metor directory is used for internal Meteor files, and we need not concerne ourselves with this directory now.

The css file is where we put any stylesheet information. This file is empty for now. The html file has the necessary HTML to display the welcome message:

<head>
  <title>chat</title>
</head>

<body>
  {{> hello}}
...

Continue reading →