Category Archives: Development

Development related blogs. Anything from conference stories, development ideas, and anecdotes.

Currying and Applying Arguments

I gave a talk called Some(‘F#’) is Better Than None at Winnipeg Code Camp on January 28, 2017. The talk was catered toward developers who have never looked at F# or those who never considered F# as being a useful prototyping tool.

During the talk I made a brief mention to currying and partial application. After the talk I received a great question along the lines of: if every function only takes one argument, how is it we are able to pass multiple arguments to a function?

Currying

Currying is the process of transforming a function which takes multiple arguments into a sequence of functions where each new function takes only one argument. If we were to write the function:

let addThreeNumbers x y z = x + y + z

This function looks like it takes three arguments, however the signature of addThreeNumbers is converted to int -> int -> int -> int. We can read the function signature as: addThreeNumbers has type of int goes to int goes to int goes to int. The last type in the signature denotes the return type of the function.

Applying arguments

Using the addThreeNumbers function we defined earlier, we can call the function as:

addThreeNumbers 1 2 3

Since we are passing in the full number of arguments the function evaluates immediately giving us the result of 6.

As we are passing in arguments, the function applies what it can with the arguments given. Each time we add an argument it peels off the leading type ->, eventually reaching the point where there is nothing left to apply which then evaluates the result.

// int -> int -> int -> int
let addThreeNumbers x y z = x + y + z

let result = addThreeNumbers 1 2 3
// can be thought of as
let result' = (((addThreeNumbers 1) 2) 3)

// You can also apply the arguments in steps and create 
// intermediate functions.

// addTwoNumbersWith1 :: int -> int -> int
let addTwoNumbersWith1 = addThreeNumbers 1

// addOneNumberWith1and2 :: int -> int
let addOneNumberWith1and2 = addTwoNumbersWith1 2

// six :: int
let six = addOneNumberWith1and2 3

Currying and partial application are very powerful features. Currying is the process of transforming a function which takes multiple arguments into a sequence of functions which take one parameter. Applying arguments to a curried function peels off a function from the sequence. This allows you to build reusable functions.

Advent of Code 2016: Things I’ve Learned

I’ve been engaged in the Advent of Code 2016. Attempting to improve upon my solutions from 2015. I have improved a lot since last year. However, as of Day 8 there are some things that I have notably learned. Well, some things I’ve forgotten and learned again. Here are the highlights so far:

Seq.chunkBySize

This was added in F# 4.0. This function allows you to partition your collection into arrays of a size of your choosing.
For example:

[1 .. 6] |> Seq.chunkBySize 2;;  // seq [[|1; 2|]; [|3; 4|]; [|5; 6|]]
[1 .. 6] |> Seq.chunkBySize 3;;  // seq [[|1; 2; 3|]; [|4; 5; 6|]]

There is a caveat, if the number of items in the collection is not equally divisible by the chunk size, the last array will have fewer items.

[1 .. 6] |> Seq.chunkBySize 5;;  // seq [[|1; 2; 3; 4; 5|]; [|6|]]

Slicing

This one I seem to forget is available. I always start with a for or a skip |> take and then I remember. It can make your code a lot easier to read.

Spoilers for day 8

I am using a mutable 2D array for day 8. There was a similar problem last year and I chose to go fully immutable and got destroyed by Garbage Collection. Mutability we go! The problem called for shifting pixels (which wrap) on a display. I decided to get the current row of pixels starting at origin (I define the origin as being the length of the array minus the amount we are shifting). I then could apply the shifted row to the mutable 2D array.

My initial implementation:

member this.RotateRow (y,shift) =
  let origin = width - shift
  seq { for i in {origin .. width - 1} do
          yield display.[i, y]
        for i in {0 .. origin - 1} do
          yield display.[i, y] } 
  |> applyShift

It’s not terribly bad. We get the items from shift origin to the end of the array followed by items from the start of the array to origin. We can do better with slicing.

Slicing an array or list is a matter of using the indexer after the label.

let l1 = [1 .. 100];;
l1.[6 .. 10];; // [7; 8; 9; 10; 11]
l1.[7 .. 11];; 
l1.[.. 10];;   // [1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11]
l1.[95 ..];;   // [96; 97; 98; 99; 100]

Did you notice I said indexer? Yes, slice takes the items via their position in the collection. This is something you will need to be aware of. If you want the first ten items using slice you would use [..9], as the tenth item is in position nine.

Updating my original solution using slices instead of for constructs has made my solution more concise and readable.

member this.RotateRow (y,shift) =
  let origin = width - shift
  seq { yield display.[origin .., y]
        yield display.[.. origin - 1, y] } 
  |> applyShift

By using slices I removed the need to calculate the end of the array. This eliminated the chance of an off by one error. <insert bad off by 2 joke here>

Seq.windowed

Seq.windowed is handy when you need to look for specific patterns in a collection. This function works by taking a collection and creating a sliding window of arrays with the length you specify.

Some examples of course:

let l1 = [1 .. 5];;
l1 |> Seq.windowed 2;; // seq [[|1; 2|]; [|2; 3|]; [|3; 4|]; [|4; 5|]
l1 |> Seq.windowed 3;; // seq [[|1; 2; 3|]; [|2; 3; 4|]; [|3; 4; 5|]
l1 |> Seq.windowed 4;; // seq [[|1; 2; 3; 4|]; [|2; 3; 4; 5|]]
l1 |> Seq.windowed 6;; // seq [] 

If you try to get windows larger than the size of the collection, you end up with an empty collection.

Seq.chunkBySize, slicing, and Seq.windowed are my three most notable take aways from the first 8 days of Advent of Code 2016.

ASP.Net Encode Token

TL;DR – In ASP.Net MVC use the HttpServerUtility.UrlTokenEncode() and HttpServerUtility.UrlTokenDecode() for encoding and decoding tokens/keys with unprintable characters.

Every so often we use a throw away token for generating a link. An example of this would be a link to reset a password. You don’t want to send the password to your user in an email (you should never send a password to a user in an email). Instead, you create a link with a token they can click on to create a new password. Encoding the token is straight forward, however I always seem to forget the utility to use.

I always seem to go down this progression:

    • Base64 – Convert.ToBase64String()
      • Nope. This has issues with ?token= query params due to the potential `=` signs at the end.
    • Encoded using HttpUtility.UrlEncode()
      • Nope. UrlEncode does not handle unprintable characters, when you try to decode it you get the wrong results.
    • Encoded using HttpServerUtility.UrlTokenEncode()
      • YES. That’s the one we need.

Having to use the UrlTokenEncode has been fairly rare, which is why I can’t remember it immediately. The default IdentityProvider has the email reset feature built in with the encoding/decoding happening automatically. Knowing the UrlTokenEncode comes in handy when creating links for QR Codes or other purposes.

Setting up Gulp for compiling SASS in Visual Studio 2015

I’ve been meaning to use gulp for compiling sass files in Visual Studio for a while. Using gulp instead of a Visual Studio extension gives the advantage of not always having to be in visual studio. I’ve used gulp in other projects where it was already setup and working, but have never set it up myself. I figured it was time for that to change.

Step 1: Install nodejs if you haven’t already. This gives access to the Node Package Manager (NPM). You can download nodejs using the link below.
https://nodejs.org/en/download/

Step 2; Add the variable NODE_PATH with a value of %AppData%\npm\node_modules. If it’s already in your environment, then you don’t need to do anything. This will give you access to globally installed packages from the command line without having to type the full path.

Step 3: Install gulp using NPM. In a command window or PowerShell type the command below and press enter.
npm install -g gulp
NPM will start installing gulp globally for your system, as denoted by the -g flag.

Step 4: Start up Visual Studio 2015 and create a new project or open an existing project.

Step 5: Add two new files in Visual Studio called “NPM Configuration File” and “Gulp Configuration File”.

  • package.json – NPM Configuration File
  • gulpfile.js – Gulp Configuration File

Note: If the templates aren’t available in Visual Studio you can create a normal json and a js file, and name them package.json and gulpfile.js.

Step 6: Modify the package.json file so it looks like the configuration below. You can change the values as you like, but the important part is the devDependencies section.

{
  "name": "ASP.NET",
  "version": "1.0.0",
  "description": "",
  "main": "gulpfile.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "",
  "devDependencies": {
    "gulp": "^3.9.1",
    "gulp-plumber": "1.0.1",
    "gulp-autoprefixer": "^2.0.0",
    "gulp-sass": "2.0.4"
  }
 }

Step 7: In the solution explorer of Visual Studio, right click on the package.json file and select Restore Packages. This will download the dependencies we set up in step 7.

Note: Now would be a good time to say if you are using version control, it would be wise to add an exclusion for the node_modules directory.

Step 8: Let’s now update the gulpfile.js to compile our scss file.

var gulp         = require('gulp');
var sass         = require('gulp-sass');
var plumber      = require('gulp-plumber');
var autoprefixer = require('gulp-autoprefixer');

gulp.task("styles", function () {
  // We start with the main scss file (site.scss) and include all others from there.
  return gulp.src(["content/scss/site.scss", "content/scss/*.css"])
             .pipe(plumber())
             .pipe(sass())
             .on('error', function(err) {
                 console.error(err.message);
                 this.emit('end');
             })
             .pipe(autoprefixer("last 1 version", { cascade: true }))
             // The destination directory to output the compiled css.
             .pipe(gulp.dest("content/"));
});

gulp.task("default",["styles"]);

Step 9: At this point, it’s now just a matter of creating the content/scss/ directory and creating your sass styles.

Note: When you want to compile your css, open the “Task Runner Explorer” by right clicking the gulpfile.js in the “Solution Explorer” pane, or in the menu click on “View” -> “Other Windows” -> “Task Runner Explorer”. After doing that you should see our two tasks of “default” and “styles”. You can double click either one to compile your new files.

If you want to get really crazy, you can create a task that will watch your content/scss/ directory for changes and have them be automatically compiled.

Getting Out of the Fold

I did the Advent of Code albeit after the fact. It’s been awhile since I’ve looked at my solutions, I thought it would be good to go back and refactor. This is turning out to be a very good exercise and an indicator of how far I’ve come as a functional programmer.

In my early functional days, learning fold was the pinnacle of accomplishments. Fold can be a difficult function to learn for someone coming from strictly Object Oriented. Fold is a higher order function that takes three parameters. Higher order function is a fancy name for a function that takes another function as a parameter. The three parameters for fold are as follows, a function (known as a folder), an initial state, and a collection of elements. The signature for fold in F# is below, along with a very simple example.

Seq.fold : ('State -> 'T -> 'State) -> 'State -> seq<'T> -> 'State
Seq.fold (fun acc x -> acc + x) 0 [1; 2; 3]

My example? The folder is a function that takes two integers and adds them together. The initial state is zero, and a list that contains the integers 1, 2, and 3. That wasn’t so bad right?

More experienced developers say “That’s not a great example. We already have Seq.sum.” Absolutely, and that’s where I am going. I found I used fold in a lot of cases where there was a much better function.

Spoilers of Advent of Code Day 3

The advent of code day 3 is a perfect example of my use of fold as being a golden hammer. Day 3’s problem is a delivery person is given directions consisting of “<",">“,”v”,”^”. An example of directions could be “^^vv<><>“. That delivery person then delivers packages to the houses at each stop, some houses receive more than one package. What is the number of houses that get packages?

My initial solution using the golden fold.

let move (x,y) = function
    | '^' -> (x, y + 1)
    | 'v' -> (x, y - 1)
    | '<' -> (x - 1, y)
    | '>' -> (x + 1, y)
    | _   -> (x, y)

let countHouses dirs = 
    dirs |> Seq.fold (fun (p,(s : (int * int) Set)) d -> 
            let p' = move p d
            (p', (s.Add p'))) ((0,0), Set.ofList [(0,0)])
    |> fun (_, s) -> s |> Seq.length

The code works, but it’s not all that readable. The state is a tuple containing the current position and a set of houses visited. The folder deconstructs the tuple, moves to the new house, and adds it to the set. The final result of the fold is deconstructed and the set of houses is counted.

Let’s look at how I refactored the solution. This is after some time away from my initial solution.

let countHouses dirs = dirs |> Seq.scan move (0,0) |> Seq.distinct
                       |> Seq.length

Seq.scan is a higher order function that returns a sequence of the intermediate results. It turns out scan is exactly what I needed. You can see scan results in a much nicer solution than fold does.

Fold is a very useful and powerful function, especially when you are getting started. However, it shouldn’t be your go to function for everything. As you gain more experience you learn there are more specific functions that result in code that is a lot easier to reason about. Don’t stop learning when you get into the fold.

Some(“F#”) is better than None

I’ve been doing object oriented programming since 2002. I’ve been looking to expand my horizons for a little while. Not that I believe OOP is bad. I have found that in some cases, modelling everything in classes can be extremely difficult and you end up with an over engineered and complex solution (usually by accident).

I started tinkering with Erlang around 2013. I really enjoy Erlang. I like the syntax, and the pattern matching is absolutely fantastic. I am also a fan of the actor model. My only issue is the problems I am solving for work don’t fit Erlang solutions (yet). Currently we primarily create desktop applications. The newer applications are in C#.

This is where F# comes in. F# is a functional language which runs on the CLR. This means you can start using F# and have it interop easily with your existing .Net applications. Below are a few reasons I started using F# over C#.

REPL (Read Evaluate Print Loop)

One advantage to using F# is the REPL. This allows you to get immediate feedback during your development process. Write a function, test it in the REPL. Modify the function, test it again. No compiling and no external test runner is required. A more apt definition of REPL would be Rapid Evolving Program Logic.

Pattern Matching

I like F#’s pattern matching. It can be concise and easier to read than a bunch of if statements. An example is comparing application versions to determine if the user requires an update.

// uMaj, uMin, uBld = user's Major, Minor, Build version
// lMaj, lMin, lBld = latest Major, Minor, Build version

// C#
if (uMaj < lMaj)
    return "Major";
else if (uMaj == lMaj && uMin < lMin)
    return "Minor";
else if (uMaj == lMaj && uMin == lMin && uBld < lBld)
    return "Build";
else
    return "None";

// F# - The user's version numbers are either matched or 
// bound to x for comparison
match (uMaj, uMin, uBld) with
| (x, _, _)       when x < lMaj -> "Major"
| (lMaj, x, _)    when x < lMin -> "Minor"
| (lMaj, lMin, x) when x < lBld -> "Build"
| (_, _, _)                     -> "None"

Option Type

F# tries to eliminate null values as much as possible. Nulls are still needed for working with non F# libraries. When an F# function needs the ability to return a value or no value (if there is an error), you can use the Option type. When the function returns a value you wrap it in Some, when no value is to be returned you use None. This gives you a function having a return of either Some (value) or None. A simple example of this is a divide function.


let divide a b =
   match b with
   | 0 -> None
   | _ -> Some (a/b)

After obtaining the result, you can use pattern matching to determine whether you received Some value or None. You end up with fewer NullReferenceExceptions by using options.

There are more reasons to use F#, discriminated unions, agents, and it’s fun. After a little practice and experience with F#, you find you write less code to accomplish the same task than in other .Net languages. All you need to know to get started with F# is the keywords “let“, “|>” (pipe forward), and “fun“. Happy learning!

Great F# resources:

WP8: Linq to Sql MissingManifestResourceException

I was making a major overhaul of a windows phone 8 application yet to be released. It was my own fault, I made a lot of changes in a short time. I did make sure all my tests still passed. Running the application in the emulator gave me a MissingManifestResourceException. I never had this problem before. The error would occur in the base constructor for the DataContext.

The full error message was:
Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "Microsoft.Phone.Data.Resources.resources" was correctly embedded or linked into assembly "Microsoft.Phone.Data.Internal" at compile time, or that all the satellite assemblies required are loadable and fully signed.

I did the usual checks for Neutral Language setting in the Assembly Information. I made sure the Supported Cultures were correct. I added a resource file to the project. I thought maybe the template I had used to create the app didn’t include the appropriate resource file. Still nothing.

I couldn’t find anything that would confirm or deny the fact that linq to sql required cultural resource files.

After much thrashing, I found the cause of my issue. It was in the connection string. Can you see the problem?
"Data Source=isostore;/children.sdf"

It’s the semi-colon. The connection string should have been:
"Data Source=isostore:/children.sdf"
My typo in the connection string gave me an obscure error which lead me on a wild goose chase down the wrong path.

So, lesson learned. Always double check you are using a colon ":" in your connection string.

A Different Thought Process: OOP to Erlang

I’ve been dabbling in functional programming lately, more specifically in erlang. I am familiar with recursion. I do use recursion on occasion when code simplicity outweighs the possible performance cost. I am reading Erlang Programming by Francesco Cesarini & Simon Thompson. I am enjoying the book, although I’ll admit I’m not very far into it. I would like to share a realization that I had during one of the exercises: don’t think in objects, think pattern matching.

The exercise consisted of creating a simple database. The underlying structure was a key/value pair contained in a list. The interface consisted of new, destroy, write, delete, read, and match. I am going to focus on the read(Key, Db) function, where Db is the list obtained from calling new. After adding data consisting of key/values, I would call the read function with the specified Key.

My initial implementation was as such:

read(_Key, []) -> {error, instance} ;
read(Key, Db) -> 
  [Head | Tail] = Db,
  case Head of
    {Key, Data} -> {ok, Data} ;
    {_,_} -> read(Key, Tail)
  end.

I want it to be known that this does work. It looks horrid, but it does work. Imagine if this were a more complex operation? My problem at the time was thinking the Db argument was a list. I would then pull out the Head (first item in the list) and Tail (remaining list items) once in the function. This was so wrong of me. I was thinking OOP and not pattern matching.

When I remembered Pattern Matching can be used for deep matches. I refactored the read operation to three lines.

read(_Key, []) -> {error, instance} ;
read(Key, [{Key, Data} | _Tail]) -> {ok, Data} ;
read(Key, [_ | Tail]) -> read(Key, Tail).

The first implementation was using pattern matching. The matches were based on an empty list and a non empty list. The refactored code pattern matches on an empty list, a non empty list where the first item contains the key we were looking for, and a non empty list where the first item’s key did not match.

When pattern matching with functions in erlang, it seems less about what the arguments are, and more about what you want them to be.

ActiveRecord and MySQL Setup on Ubuntu 12.04.1

As a .Net developer I thought I would give Ruby and ActiveRecord a try for a simple web application. When I say simple I mean it’s only two operations. One consists of non-authenticated data entry and the other of data retrieval. Install of everything went fine, or so I thought. It turns out that there are some intricacies in getting the right activerecord to work on Ubuntu.

This is a quick blog post of getting activerecord to work with MySQL on Ubuntu 12.04.1 LTS. This is a result of me searching a bunch of other blogs and tutorials to figure out what I was doing wrong.

Getting the files

>sudo apt-get install mysql-server
>sudo apt-get install ruby
>sudo apt-get install rubygems
>sudo gem install activerecord
>sudo apt-get install libmysqlclient15-dev
Note: this may install “libmysqlclient-dev” instead, which should be fine.
>sudo gem install activerecord-mysql2-adapter

You should now have all the required files.

Setup test database

Connect to MySQL and create a database and table.

>create database dummy;
>use dummy;
>create table tests(id bigint primary key auto_increment, text varchar(100));

A test ruby file: atest.rb

#! /usr/bin/ruby
require ‘rubygems’
gem ‘activerecord’, ‘=3.2.9’
require ‘active_record’

class Test < ActiveRecord::Base
end

ActiveRecord::Base.establish_connection( :adapter => ‘mysql2’,
  :host => ‘localhost’,
  :username => ‘username’,
  :password => ‘password’,
  :database => ‘dummy’
)

Record = Test.create(:text => ‘new record text’)
Inserted_records = Test.where(:id => 1)
# It is recommended to use “find” instead of “where“ when selecting by the primary key.
# I used where as a test to make sure I had everything installed and working
Puts inserted_records[0].text

Make sure you have a reference to the gem in your ruby file. If you don’t, when you go to use the ”where” method you will receive a method_missing error. As of writing, the active record gem version was 3.2.9.
Also make sure the adapter in the establish_connection method is ‘mysql2’. Using the older ‘mysql’ adapter will also cause issues.

Note

I did skip creating a new user and granting access to the mysql database as that was an easy web search.

CodeStock 2012: Good Books Open Space

I wanted to share the list of good books that were suggested during the open space.

  • Life of Pi by Yann Martel
  • Go Tell It on the Mountain by James Baldwin
  • Ender’s Game by Orson Scott Card (Aside from Ender’s Shadow, apparently the rest of the series is not all that great)
  • AWOL on the Appalachian Trail
  • A Walk in the Woods by Bill Bryson
  • Blood Meridian by Cormac McCarthy
  • Atlas Shrugged by Ayn Rand
  • The Fountain Head by Ayn Rand
  • Anthem by Ayn Rand
  • River of Doubt by Candice Millard
  • Wool by Hugh Howey
  • Ready Player One by Earnest Klein
  • Selarial’s Song by Brewer
  • Hunger Games Trilogy
  • Dragon Tattoo by Stieg Larsson
  • The Monster Hunter Series by Larry Correia
  • After America by John Birmingham
  • 1632 by Eric Flint
  • Fuzzy Nation by John Scalzi
  • Old Man’s War by John Scalzi
  • Iron Druid Chron by Kevin Hearne
  • MetaGame by Sam Landstrom
  • Song of Ice and Fire by George R. R. Martin
  • Daemon by Dan Suarez
  • Blindness by Jose Saramago
  • Death Interrupted (Death with Interruptions) by Jose Saramago
  • Leadership and Self Deception
  • Flash Forward by Robert Sawyer
  • Color of Magic by Terry Pratchett
  • Lies of Locke Lamora by Scott Lynch
  • Troy Rising by John Ringo
  • The Mote in God’s Eye by Larry Niven
  • Saga of 7 Suits by Kevin J. Anderson
  • Pastwatch: The Redemption of Christopher Columbus by Orson Scott Card
  • First & Last Men by Olaf Stapledon
  • I am Legend by Richard Mathison
  • Mistborn by Brandon Sanderson
  • Now Habit by Neil Fiore
  • Name of the Wind by Patrick Rothfuss