“Perfecting oneself is as much unlearning as it is learning.” - Edsger Dijkstra

Functional programming is all the rage these days. It’s hard to go to a conference, the water cooler, or your grandmas house without hearing about it. To initiates it’s an insurmountable wall containing droves of intellectual giants and the answers to all your bugs. To learners it’s the great mind twist that forces them to revisit and rethink many of the premises on which they learned the craft. Beyond that stage I can only surmise. Is programming nirvana really found in functional programming? I honestly don’t know yet, but I’m making 2015 the year I find out.

In school I learned object oriented programming in languages like Java and C#. Once I joined the work force, I landed a job that demanded SQL chops almost exclusively. SQL quickly became a gateway drug back into real programming and that’s when I relearned JavaScript. JavaScript was one of the best decisions I made early in my career. Not because it’s a particularly elegant language, but because it’s a melting pot of ideas.

In JavaScript you’re constantly bombarded with new approaches, libraries, and frameworks. There is a certain stamina needed to keep your sanity. The moment you find yourself comfortable with something, it gets replaced with something better (sometimes). For many, that is the primary complaint about web programming. For me, and many of the people I’ve worked with, it’s what gets us up in the morning. There is an underlying attitude that is frequently willing to approach problems from new angles, a constant quest for perfection.

JavaScript helped introduce me to lots of the fundamental concepts of functional programming. While JavaScript can be written in a very functional style, it’s still a very imperative language. To get around this issue people have written lots of libraries that help you write in a more functional style. My plan for this year is pretty simple:

  1. Learn Haskell
  2. Build two trivial command line applications in Haskell
  3. Use functional techniques and libraries to improve my company’s web application

I am focussing on Haskell because I constantly hear it referred to as the model functional programming language. So much of the language doesn’t compromise and forces you to learn the truly functional approach. I want to fully embrace the paradigm shift and Haskell will beat me over the head until I do.

Let’s be real here. There’s not enough time for me this year to get beyond writing trivial applications in Haskell. So, I’m setting the bar pretty low by trying to write a couple relatively simple command line applications.

My last goal integrates best with my day job. One of the challenges with the web application I work on is its rampant state mutation. The biggest offender is the DOM. People modify DOM willy nilly jQuery style and it makes our application incredibly complex. My goal is to find out if a less state based approach will improve the maintenance of our application. I dearly want to put a debugger statement in one place and observe everything that is modifying the DOM.

The end goal is to make my life easier. I want to make it easier to maintain the applications I build. I have a strong feeling that functional is going to make that happen. I don’t think it’s the silver bullet, but perhaps its better than object oriented. Stay tuned for more updates on how this year’s goals are going. Most of my posts this year will be related to JavaScript.

Feel free to reach out to me on twitter @jondelamotte.

One of the challenges of working with Git, or any source control for that matter, is managing branches. The GitHub network graph helps you visualize branches and how they fit together. I wrote a little command to open the webpage from your command line. Note: this only works on Mac, though it could easily be tweaked to work on linux.

Network graph

Put the following line in your .bashrc or .zshrc:

alias gn="git remote -v | grep 'git@github' | sed 's/origin.*:\([^.]*\).*/\1/' | head -n1 | read GH; /usr/bin/open -a \"/Applications/Google Chrome.app\" \"https://github.com/\$GH/network\""

Type gn from the root directory of your git project and it will open the GitHub network page inside of chrome.

This post was created primarily for me, and secondarily for the few people out there who are learning ruby and come from a javascript background. Here are some categorized code samples that basically do the same thing in either language. I’m using Ruby 2.0+.

Comments

// this is a single line comment

/* this is a
   multi-line comment */
# this is a single line comment

# this is a
# multi-line comment

=begin
  another way
  to do multi-line
=end

Variables

In javascript people tend to name their variables starting with a lowercase letter then camel case after that (ie thisIsMyVariable). In ruby, people tend to use all lowercase letters and underscores for separators (ex this_is_my_variable).

var myVariable = "hello there";
my_variable = "hello there"

Arrays

var arr = [1,2,3,4];

arr.push(5);

arr = arr.map(function(a) {
  return a * 2;
});
arr = [1,2,3,4]

arr.push(5)

arr.map! {|i| i * 2} # the `!` saves the result back to the variable

Hashes (objects)

var obj = {
  one: 1,
  two: 2
};

for (var key in obj) {
  // see http://bit.ly/1krXmjL for explanation
  if(obj.hasOwnProperty(key)){
    console.log(key + ': ' + obj[key]);
  }
}

obj.hasOwnProperty('one'); // true
obj = {
  one: 1,
  two: 2
}

obj.each {|key, value|
  puts "#{key}: #{value}"
}

obj.has_key? # true

Functions

function multiply(a, b) {
  return a * b;
}

function addOne(a) {
  return (a || 0) + 1;
}

console.log(multiply(5,10)) // 50

console.log(addOne())       // 1
console.log(addOne(5))      // 6
def multiply(a, b)
  return a * b
end

def add_one(a = 0)
  return a + 1
end

puts multiply(5,10)  # 50
puts multiply 5,10   # 50

puts add_one         # 1
puts add_one 5       # 6

Classes

function Car(year, brand) {
  this.year = year;
  this.brand = brand;
}

Car.prototype.dispayInfo = function() {
  return this.year + ' ' + this.brand;
};

var civic = new Car(2005, 'honda');
civic.dispayInfo(); // 2005 honda
class Car
  attr_accessor :year, :brand

  def initialize(year, brand)
    @year = year
    @brand = brand
  end

  def display_info
    "#{year} #{brand}"
  end
end

honda = Car.new(2005, 'honda')
honda.display_info # 2005 honda

Credits

Here’s a page this inspired some of the content of my post:

I grew up using Macs, was forced back to Windows for a job, worked on Ubuntu for a while, and now have finally landed back in the Mac realm. After having been around the block on just about every platform, I can safely argue that Mac’s are better for a developer who’s serious about Linux but doesn’t want to sacrifice a quality UI.

One of the hardest things about setting up a new box is figuring out all the quirky setup nuances that make your box shine. This post is the chronicle of my most recent, most ultimate setup yet.

This was all performed on a fresh MacBook Air, but really anything running Mavericks works.

Ingredients

  • iTerm2 replaces the stock Terminal app
  • Alfred replaces the default Mac Spotlight
  • Homebrew manages all your command line packages
  • Solarized Colors makes your command line pretty
  • Vim the best, and hardest, text editor and IDE that is worth learning if you are serious about computers
  • jVim my personal vim, and extras, setup
  • Tmux manages windows within your terminal. It lets you open up and resize several terminal windows without leaving the keyboard and reaching for that dirty mouse.
  • Zsh a better shell that is bash backwards compatible
  • Oh my zsh some nice default settings and themes for zsh

Getting Started

Installing

Start by downloading and install iTerm, Alfred, Homebrew, and Solarized (imported into iTerm2 from this file). Once you have that done, setup your iTerm2 profile to use your newly imported Solarized Dark theme. Also make sure your “Report terminal type” is set to xterm-256color. Then let’s keep going on the installation by hopping into iTerm2 and running some shizz.

Homebrew installs stuff in /usr/local/bin so let’s add it to our path by modifying ~/.bashrc and ~/.zshrc this might not be required

echo PATH=/usr/local/bin:/usr/local/sbin:$PATH | tee -a ~/.bashrc ~/.zshrc

Install zsh, vim, tmux, and something to connect your Mac’s clipboard to Vim:

brew install zsh tmux vim reattach-to-user-namespace

Tell your mac to permit your custom shell:

echo /usr/local/bin/zsh | sudo tee -a /etc/shells

Change your default shell:

chsh -s /usr/local/bin/zsh $USER

Move a folder to account for some Mac bug:

sudo mv /etc/{zshenv,zshrc}

Install oh-my-zsh:

curl -L http://install.ohmyz.sh | sh

Install jVim to your home directory:

git clone https://github.com/jondlm/jvim.git ~/.jvim
cd ~/.jvim
git submodule update --init --recursive
./install.sh bonus

Restart iTerm2.

Install some more stuff with brew:

brew install vim tmux reattach-to-user-namespace

Fire up Tmux:

tmux

Setup vim:

vim
# ignore errors for now
:BundleInstall

Interlude…

To be honest, I started writing this article several months ago and never finished it. I suspect these steps are correct, but your mileage may vary.

Fin.

Vim is hard. Vim is scary. So was Git the first time I used it. If you stick with it, you’ll never want to use another text editor. Also see my vimrc if you’re into that kind of thing.

General

  • :edit reloads the current file
  • ctrl-e open/close NERDTree sidebar

Selecting

  • vip visually select the current paragraph

Line breaking

  • gqq breaks lines so they don’t exceed 80 characters, AWESOME!
  • gqip break lines for the current paragraph
  • :set textwidth=80 super handy for auto line breaking, use with gqq too

Sorting

  • :sort sort all lines by string, works over visual selects too
  • :sort n sort by number
  • :sort /*.\~/ sort by anything after the first instance of ~

Folding

  • :set foldmethod=indent turn on folding by indentation
  • zM fold everything
  • zR unfold everything

Searching

  • :set smartcase case sensitive if you use caps, otherwise not recommended
  • :set ignorecase makes searching case insensitive by default
  • :set number shows line numbers
  • :noh clears you last / search

Indentation

  • :set expandtab=false use real tabs
  • :set expandtab=true use spaces instead of tabs
  • :set tabstop=2 my personal favorites
  • :set shiftwidth=2 my personal favorites
  • :retab change existing tab characters to spaces depending on expandtab

Copy / paste

  • "ay yanks your selection into register a
  • "ap pastes the contents of register a
  • :reg displays the contents of named registers

Key binding

  • imap ii <esc> bind the key combo ii to the escape key

Screen movement

  • zz center the screen on your cursor
  • zb make the bottom of your screen flush with your cursor
  • zt make the top of your screen flush with your cursor

Etcetera

Add autocmd BufNewFile,BufRead *.jade set filetype=jade to .vimrc to ensure .jade files have their type set correctly. Works for other stuff too.