Modular state configuration and folder structure with Angular UI Router

One of the problems I’d previously had while developing a rather complex mobile app with the Angular-UI-Router was that the state.config file got pretty gnarly with more than a few abstract and nested states.

In the process of looking for a different solution, I came across a merge note that mentioned there’s now a way to use modular state.config files.

“Note: You still need to manage module dependencies.”

I don’t see a clear path to using this pattern & the BaseState class constructor pattern, but I still think we should convert to the modular version.

I suggest something like this:

app/states/state.coffee

angular.module('states', [
      'feature1',
      'feature2',
      etc.....
    ])
    .config(function($stateProvider) {
        $stateProvider.state('state', {...})
    });

app/states/feature1/feature1.state.coffee

angular.module('feature1', []).config(function($stateProvider) {
       $stateProvider.state('feature1.state0', {...})
       $stateProvider.state('feature1.state1', {...})
       $stateProvider.state('feature1.state2', {...})
    });

How To Setup Grunt for Amazon EC2 Instances

If you’re not familiar with Grunt, you should know that you can get documentation at their website and you can get their code on github. There’s a great getting started tutorial at their website. This post will cover a lot of that, specially tuned for AWS EC2 instances with NodeJS, Neo4j, Angular, Stylus, Topcoat, and Express. This post assumes you’ve already got those packages and their dependencies installed on your AWS machine. Feel free to reachout if you’re in need of help.

Getting started with Grunt

From their site

Grunt 0.4.x requires stable Node.js versions >= 0.8.0. Odd version numbers of Node.js are considered unstable development versions.

First, we want the Grunt Command Line Interface utility that will be available all over our machine. npm install -g grunt-cli

npm install grunt --save

When you run grunt from ~/your/project/ directory, it will look for Gruntfile.js. So we’ll make that now. touch Gruntfile.js

open Gruntfile.js

In our grunt file, we’ll start simple:

module.exports = function(grunt){
  "use strict";

  grunt.initConfig({

  });
};

Grunt files are written in .js, and use the require() method. grunt methods typically take JSON blobs, but you can use fully formed JS in them as well.

Add your AWS keypair to SSH Known Hosts in OSX

Here’s how to add your .pem keyfile to your $KNOWN_HOSTS so that you never have to rely on being in the correct directory in order to get to AWS with SSH.

Move your .pem from Downloads to ~/.ssh:

mv keyfile.pem ~/.ssh

Then type:

ssh-add ~/.ssh/keyfile.pem

And now you’re able to easily connect without that long “-i filename” blah blah blah command. Just type:

ssh <<username>>@<<ec2-dns-info>>.amazonaws.com

3 simple tricks to make learning recursion simple

Recursion is a concept that can be made easier if you have an easy way to think about it, and what it is you’re actually doing. For this discussion, I’ll be considering the Fibonacci function, which returns a number that is the result of adding the two previous fibonacci numbers. This function is bound by zero, so starting at zero you’ll get:

f(n) = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ... 

Here’s what it looks like:

var fibberfunk = function(n){ 
      if (n > 2) {
        return fibberfunk(n-1) + fibberfunk(n-2);
      } else {
        return n;
      }
    };

1. It’s turtles all the way down… until you’re at the base:

A recursive solution is suitable for the types of problems that require you to support your next result with the outcomes of previous invocations of your function. Like the old story of what supports the flat earth, recursion can go on and on indefinitely. Unlike the fable, however, we must have a base case or our recursions will fall apart. At the base case, your recursion stops recursing and finally begins to really operate. So you always need to make sure that you have a “base case” to stop the function from depending on itself for any more arguments. In the Fibonacci example, the base case is contained in the else statement: When n <= 2 return n.

By returning a value which doesn’t require any further code execution, you enable the recusion to stop. Your base case is the first solution that your code will reach, and it’s the result which will support the final results. It’s often easiest to think of the base case as the bottom, but that won’t always be true.

2. Always send flowers (unless you don’t have to):

In life, flowers serve the function of reminding the recipient who sent them. (Yes, they serve other functions as well, but for the sake of this discussion I’m using them as a literary device.) In this example, they serve the purpose of reminding you to ALWAYS MODIFY THE INFORMATION YOU’RE PASSING FORWARD. When you’re not at your base case, you have to send flowers to the next step of the function in order to set the stage for future invocations to reach the base case. In the above example, our flowers are sent by the returnstatement when n > 2:

return fibberfunk(n-1) + fibberfunk(n-2);

Notice that each successive function calls the same function with a modified argument. In our case, we’re reducing the value of the argument we’ve passed in which brings us closer to our base case of having n <= 2. By modifying the argument we pass to subsequent invocations of the function, we’re “sending” information… in our case, the information we’re “sending” is that we’ve taken one step closer to the base case. This is called the “recursive case” and you might have more than one type of recursive case in your function, in ours there is only one.

It’s very, very important that you send flowers, or your function will never know when it’s reached the base case.

3. Recursion is not iteration

The last trick I want to share is not so goofy, but I think it should be taken seriously. I tend to remind myself that recursion isn’t the same as iteration so I don’t create for loops to solve recursion problems. In an iterative function, we say something like this:

var shmiterative = function(num) { 
      for (var i=0; i < num; i++) {
        return num + 2;
      }
    };

    schmiterative(5);
    //3
    //4
    //5
    //6
    //7

While we are passing information forward through the for loop, by incrementing the index, notice that schmiterative(); is not dependent on the result of the base case to be able to calculate all the other results. Our incrementation will continue indefinitely until i = num but we’re able to calculate the result of every return statement without relying on the result of the base case.

Let’s hope that any of the tricks will help you get a little bit closer to solving your own recursive riddle, and if you’ve got some suggestion of how to improve or correct anything I’ve written, please let me know.

← Newer Page 1 of 6