Friday, December 29, 2017

Elixir: Pattern Matching - A Taste of Functional Programming

= is a match operator
After an initial assignment it then becomes a match assertion.
num = 1

To check the match assertion you can try this and it will be valid
1 = num

Furthermore, these are also valid:
[1] = [1]
[num] = [num]

[score1, score2, score3] = [89, 93, 87]
IO.puts score1  # 89
IO.puts score2  # 93
IO.puts score3  # 87

{:ok, value} = {:ok, 1000}
IO.puts value  # 1000

{:error, message} = {:error, "Uh oh"}
IO.puts message  # "Uh oh"

Error when matching:
{:foo, value} = {:bar, "nope"}

** (MatchError) no match of right hand side value: {:bar, "nope"}

Matching function arguments
Various function definitions and subsequent calls:
def sum_two_nums(num1, num2) do
  num1 + num2


def sum_two_nums(%{num1: num1, num2: num2}) do
  num1 + num2

sum_two_nums(%{num1: 2, num2: 5})

def sum_two_nums([num1, num2]) do
  num1 + num2


There's so much more you can do with pattern matching. It is one of the most powerful features you will learn and use when programming in Elixir.

Other posts in this series:

Friday, December 8, 2017

Elixir: Anonymous Functions - A Taste of Functional Programming

An anonymous function is composed of an optional parameter list, a body enclosed by fn -> and end. It returns a function definition and the potential for the function to be executed. Quite simply the anonymous function can be assigned to a variable which then can be subsequently called.

Define an anonymous function:,
greet = fn ->
  IO.puts "Hello World"
Of course that does nothing unless we call it! We can do that by using this dot notation -
[function name].([optional parameters])
Like this:
Where the output is: Hello World

Anonymous functions with parameters

add = fn(num1, num2) ->
  num1 + num2

IO.puts add.(10, 15)
IO.puts add.(12, 15)
IO.puts add.(14, 19)

subtract = fn num1, num2 -> num1 - num2 end
IO.puts subtract.(15, 10)

I hope these examples ignite an interest for you to further learn functional programming using Elixir. Cheers!

Other posts in this series:
  • A Taste of Functional Programming
  • Anonymous functions
  • Pattern matching
  • Multi-bodied functions
  • Higher order functions
  • Side effects and state
  • Composition
  • Enumerables
  • Partial function applications
  • Recursion
  • Concurrency
  • Transitioning from OOP to functional

Tuesday, December 5, 2017

A Taste of Functional Programming


  • Ignite an interest in those who have never explored functional programming before
  • Exposure to functional concepts
  • Use functional parts of your existing language of choice you've never used before 
  • Lead you to pursue a functional language more in-depth

Paradigm Evolution

  • Mathematics
  • Computer Science
  • Spawn of languages
    • functional
    • procedural
    • imperative
    • declarative
    • object-oriented programming

OOP Limitations

"We’re going to be living in a multicore, distributed, concurrent — all the buzz words — world. The conventional models we’ve been doing, the OO stuff… is not going to survive in that kind of environment." - Dave Thomas
"OOP promised a cure for the scourge of software complexity. …its weaknesses have become increasingly apparent. Spreading state all over the place leads to concurrency issues and unpredictable side effects." - Dave Thomas

Thinking in Functions

  • Functions
    • Easy to reason about
    • Reliable
    • Pure
      • Don't modify variables outside of scope
      • No side effects
      • Deterministic (reproducible results)
  • Data transformation
    • ie. Unix pipes - cat foo.log | grep bar | wc -l
  • No side-effects
    • Side effects are:
      • modifying state
      • has observable interaction with external functions
  • Immutability
    • Immutable data is known data
    • Data that is created is not changed
    • Copy and alter
      • compilers can perform optimizations because of this
      • garbage collectors are smart about this
  • Higher-order Functions
    • Functions can receive functions as arguments and return functions
  • Where is my for loop?
    • recursion
    • map, reduce, filter, reject, take, etc.

Some (impure and pure) functional languages

  • LISP
  • Scheme
  • Clojure
  • Erlang
  • Scala
  • Ocaml
  • Haskell
  • F#
  • Elm
  • Elixir


"Elixir is a dynamic, functional language designed for building scalable and
maintainable applications. Elixir leverages the Erlang VM, known for running low-latency, distributed and fault-tolerant systems, while also being successfully used in web development and the embedded software domain.
" -

Here is an out line of topics to come:

  • Anonymous functions
  • Pattern matching
  • Multi-bodied functions
  • Higher order functions
  • Side effects and state
  • Composition
  • Enumerables
  • Partial function applications
  • Recursion
  • Concurrency
  • Transitioning from OOP to functional

Wednesday, August 6, 2014

AngularJS 101: A Beginner's Tutorial

Below is the PDF article I submitted to Software Developer's Journal for their series they did on AngularJS. It was published in two of their issues: Nov. 15th 2013 - AngularJS, Java and Drupal Tips & Tricks and Nov. 7th 2013 - AngularJS Starter Kit. It's behind a paywall and thus hidden from most of the world. That sucks... which is why I'm posting my article here for all to see.


AngularJS 101: A Beginner's Tutorial

Github code for application built in the tutorial

Tuesday, March 8, 2011

Screencasts on Engine Yard

Engine Yard asked if my screencasts could be added to their listing. I said 'yes'!
It's great to know that the screencasts I made several years ago can still help others learning Ruby today!

Saturday, December 11, 2010

Introduction to Erlang

This is my second talk related to Erlang. Both have been introductory and well received. I hope you enjoy my slide presentation. I also did a chat demo using real-time updates using Comet.
 View Presentation
Presented at Silicon Valley Web Builders meetup

Introduction to Erlang Presentation
Chat Demo code

Erlang web framework used: Nitrogen

Thursday, September 30, 2010

Wednesday, September 22, 2010

RubyLearning Guest blog post is up!

I wrote a guest blog post for RubyLearning and they posted it on their blog. Please go check it out:
The blog post is intended for Ruby newbies. I hope people find it valuable as they learn a great programming language.

Monday, May 3, 2010

Setting up Sinatra and DataMapper on Windows

I use a MacBook Pro for work and pleasure on a day-to-day basis. Recently, I was asked to teach web students at a local high school. These students know html/graphics/flash/etc. The advanced students were ready for some server-side programming and database integration. I wanted the students to be able to get up and going quickly (for motivation reasons) and to create useful apps (using a database). I felt Sinatra to be a great fit for this. I created a Sinatra app for my uncle and his business. It was a joy to work with it and I was able to deploy quickly using Heroku.
My experience of using Sinatra on my Mac was straightforward. Like most things using Ruby and Mac: it just worked. However, I found out the students at the high school use MS Windows. Fortunately, I have a Windows XP virtual machine running in VMWare so I could prepare that way. I used to teach computer science and web development at Spokane Community College and am aware of teaching Ruby in a Windows lab environment. Things can get tricky at times - and preparing a Sinatra app on Windows XP was a little tricky.
Here I am to quickly document what I can about setting up Sinatra on a Windows environment.

  1. Grab the installer and install Ruby:
  2. Install Sinatra from the Command Prompt:
    1. c:> gem install sinatra
  3. Install sqlite3
    1. Go to and download the Precompiled Binaries for Windows:
    2. Unzip and look for these files. Copy them into the Ruby bin directory path c:\ruby\bin
      1. sqlite3.exe
      2. sqlite3.dll
      3. sqlite3.def
    3. Install sqlite3-ruby gem
      1. c:> gem install sqlite3-ruby
  4. Install DataMapper
    1. c:> gem install dm-core
    2. c:> gem install do_sqlite3
  5. Code Reloading:
    1. Sinatra by default does not reload your code while you are editing and saving. Thus you have to restart sinatra server after changes = annoying!
    2. On the Mac I quickly found shotgun for my code reloading needs. However shotgun does not work on Windows. :(
    3. I then found out another gem that does code reloading on Windows: sinatra-reloader whew!
      1. c:> gem install sinatra-reloader
  6. Sinatra starter code:
    1. Put the code snippet (see below) into a file and save as: sinatra_starter.rb
    2. From command prompt, start the sinatra server:
      1. c:> ruby sinatra_starter.rb

I hope this helps get some of you started with Sinatra development on Windows. Please refer to these resources to further your Sinatra knowledge: