Angular Bliss

I’ve been rebuilding Kapsl to be a more modular website builder, and made the decision at the beginning that I’d use this time to become acquainted with AngularJS. This turned out to be a great idea for a number of reasons. I’ll start with the most surface-level; doing things in Angular just feels right. JavaScript being the language I adore most often, I find the concepts of scope adopted by Angular to be fantastic. It makes it easy for me to understand since all of the scopes that Angular creates use the same prototypical inheritance that JS has made me comfortable with. Secondly, directives are easily the most powerful tool I’ve seen in a front-end framework. They basically allow you to create your own HTML elements and attach the right events to them at the right times. The “Kapsl2″ front-end will make heavy use of reusable components, and Angular makes them trivially simple to build.

tl;dr

The bottom line, really, is that because my app consists of lots of reusable components and repeatable elements, AngularJS was an awesome choice for the job.

A 2D Plane Using Three.js

I’ve recently taken an interest in WebGL and the growth of 3d in the browser. I’ve never been a graphics programmer, but I’ve seen some really cool stuff done with libraries like three.js and have been wanting to dabble with it for quite some time. The other day I got my feet wet and built a few 3d cubes, and decided today that I’m going to dive back in and create a plane with a grid texture.

DEMO

I first setup a scene for my Three.js plane to exist within. The scene will include a WebGL renderer (because WebGL is sexy) and a camera.

	var Three = window.THREE,
			width = document.body.clientWidth,
			height = document.body.clientHeight,
			renderer, camera, scene;

	// Create a WebGL Renderer
	renderer = new Three.WebGLRenderer({antialias: true});
	renderer.setSize(width, height);

	// Add the Renderer to the body
	document.body.appendChild(renderer.domElement);
	renderer.clear();

	// Create a camera
	camera = new Three.PerspectiveCamera(45, width/height, 1, 10000);
        // Pull the camera back
	camera.position.z = 1500;
        // Raise the camera to view the plane from the 'top'
        camera.position.y = 200;

	// Create a scene
	scene = new Three.Scene();

Alright, now we’re getting somewhere. The scene is set for us to add some objects. In today’s example we’ll only be adding one object, a Three.js plane. Typically when creating a Three.js object you’ll create a `Mesh` instance. a Mesh instance constructor takes two parameters; an instance of Geometry, and an instance of Material. Our `Geometry` object instance is given to us by Three.js in the form of the `PlaneGeometry` object. We initialize the PlaneGeometry object by passing in the height an d width of the plane along with the number of units for the x and y axis. Our `Material` object instance is brought to you by the Three.js `MeshBasicMaterial` object which, in Three.js’s own words is ‘A material for drawing geometries in a simple shaded (flat or wireframe) way.’ Notice that we’ve passed in the option `wireframe:true` so that our plane will render as a grid.

// Create a plane
plane = new Three.Mesh( new Three.PlaneGeometry( 1000, 1000, 20, 20 ),
        new Three.MeshBasicMaterial( { color: '0x000000', wireframe: true } ) );

// Rotate the plane a bit so it's doesn't appear flat
plane.rotation.x = Math.PI / 2;
scene.add(plane);

renderer.render(scene, camera);

You should now see a 2d plane on the page. Sweet. I expected this to be a more manual process, however Three.js made it very simple by providing the PlaneGeometry and MeshBasicMaterial objects. I intend to extend this brief introduction soon by adding some mouse interactivity, see you then.

Living the Dream @ Mocavo

mocavo
I’ve been hired as a front-end engineer at Mocavo, the world’s first Genealogy search engine. I couldn’t be more excited to start this chapter of my career, and join such an amazing team. Being able to contribute to a startup at an early stage is a dream come true for me, and I’m really looking forward to pushing myself as a web developer and helping Mocavo see their vision through. Genealogy isn’t a field that I’m at all familiar with, and I think that will make this job all the more exciting. Building tools to help people discover their family history and historical documents is super intriguing, and I’m pumped to be able take part in a startup with such a strong, socially valuable mission. I foresee a long, amazing journey with Mocavo. In terms of career moves – I couldn’t be happier.

When adding, use -p

I had a revelation today. I’d tried to use `git add -p` before, but shied away from it due to what at first appeared to be overly verbose output. After using it one time, I can say that my commits are FAR more organized and concise. If you’re like me, you often end up with a bunch of one-off changes to a file such as removing or adding whitespace. This can lead to potentially useless diffs and commits, making it hard to pinpoint important changes. Using git add -p gives you ‘patch mode’ which allows you to go hunk by hunk and decide which ‘pieces’ of your changes you’d like to commit. I promise you, this is a Godsend. Use it.

A Quick Look at Higher Order Functions

JavaScript is a language that’s great at working with functions. In JavaScript, functions are considered “first class objects”. A function is an instance of the Object type, so it can do all of the things that other objects can do. One of the more powerful things a function can do is accept another function as a parameter. You can also create functions that return new functions within their body for later use. These language constructs are what make up the concepts behind higher order functions.

A higher order function needs to do at least one of two things:

  1. Accept a function as one of it’s parameters
  2. Return a new function

Let’s take a look at examples of both. Say we have a list of names in our overly simplified program. Our program’s job is to simply say hello to every name that is in our list. Quite the friendly little app. A common approach may be to do something like this:

names = ["Darrell", "Joe", "Ryan", "Larry", "Kayla", "Sue"]


for name in names then console.log "Hello, #{name}!"

# -> Hello, Darrell!
#	 Hello, Joe!
#	 Hello, Ryan!
# ...

That’s cool, but that whole console.log part could be a lot better. Perhaps that could be wrapped up into it’s own function. Let’s call it “sayHello”

names = ["Darrell", "Joe", "Ryan", "Larry", "Kayla", "Sue"]

# the new, clean sayHello function
sayHello = (name) ->
	console.log "Hello, #{name}!"

for name in names then sayHello name

# -> Hello, Darrell!
#	 Hello, Joe!
#	 Hello, Ryan!
# ...

This is a bit more flexible. At least now if we had another function, we could use it in place of sayHello in the program.

That’s an interesting concept. Maybe our program needs to do a lot of different things with names besides just saying hello. This is a great opportunity to create a higher order function that takes another function as one of its parameters. This will allow us to hide the for loop a little better as well as specify a function to use for each name in the list. We’ll call this function “withNames”. Hopefully when reading the function you’ll see why the name makes sense

names = ["Darrell", "Joe", "Ryan", "Larry", "Kayla", "Sue"]

# The new higher order withNames function that takes
# a list of names and a function as its parameters
withNames = (names, func) ->
	for name in names then func name

sayHello = (name) ->
	console.log "Hello, #{name}!"


withNames names, sayHello

# -> Hello, Darrell!
#	 Hello, Joe!
#	 Hello, Ryan!
# ...

Our higher order function will take any function as the second parameter as long as that function knows how to handle the list of names in some way. Let’s create another function that now says goodbye to everyone in the list and use it as a parameter in our new “withNames” function

sayGoodbye = (name) ->
	console.log "Goodbye, #{name}!"

withNames names, sayHello
withNames names, sayGoodbye

# -> Hello, Darrell!
#	 Hello, Joe!
#	 Hello, Ryan!
# ...
# -> Goodbye, Darrell!
#	 Goodbye, Joe!
#	 Goodbye, Ryan!
# ...

Clearly, the power of abstracting our loop logic into a higher order function has made the program more concise, and cleaned up what would have been a lot of repetitive code.

Let’s take a look at a higher order function that returns another function. A post on the Mozilla developer website describes a way to create a function called an “adder” which adds a specified number to the number given as its argument. So, for instance, we would want our add10 function to add 10 to the number we give it

add10 2
# => 12

We could, of course just define a function that adds 10 to the given parameter, but that wouldn’t be very exciting. Let’s, instead, define a function that let’s us create any “adder” function that we want to.

makeAdder = (x) ->
	(y) ->
		x + y

addTen = makeAdder(10)
addTwo = makeAdder(2)

console.log addTen 2
console.log addTwo 2

# => 12
# => 4

The way this works has to do with something called closure, but that’s best saved for another post. This can be thought of simply as makeAdder allowing us to tell it the amount to be added to the new number that is used as its returned function’s parameter.

The flexibility of JavaScript’s functions provides a very nice interface for abstracting programming logic into more concise chunks of code. Higher order functions can allow for greater levels of expressivity, and can often create a more clear interface within your program. Hack around with a few simple examples to get an understanding, and these concepts will provide invaluable in your JavaScript travels.