Color Loving Functional Style

Who can we blame

I would like to blame all this on @edd_mann. The podcast he did with @stevenproctor on Episode #71 of was certainly the straw that broke the camel’s back, so to speak. Of course this is what led me to find out more about @stevenproctor and his fantastic functional programming podcast Functional Geekery. Like everyone who does any coding for their day job I’ve been meaning to check out what all the fuss was about ‘functional programming’. So whatever I can’t blame on Edd, I can certainly blame on Steven, because there was no turning back at this point. Once I made the decision that F# was to be the vehicle for my first functional foray, I next hit upon @ScottWlaschin‘s tour-de-force website And soon after that I learned about F# type providers from @tomaspetricek‘s brilliant talk on FSLab and doing Data Science, which totally blew my mind. So, I guess it’s at least partially Scott and Tomas’ fault as well ;)

Anyway, here’s a little F# project as a result.


This project sends a random color palette from the api at to a slack web-hook URL. At my employer we have a channel setup specifically for our graphic design department called colorlovers which is fed by this code. It is for color palette inspiration. Below is a screenshot.

Slack channel for Creatives

The code is written in F# and runs on Mac, Linux, and Windows. Essentially, the following summarizes what the code does.

  • uses the awesome F# JSON type provider to access the colour lover api found here
  • extracts, transforms, converts and stores the colors and their palette widths from the api
  • creates a graphical (400px x 50px) bitmap image of the palette and stores on disk for later web-serving
  • generates the JSON object representing the color palette image and link
  • posts all to the slack web-hook

The resulting .exe is a console application and can be run directly from the command line.

> ColorLover.exe // on windows
$ mono ColorLover.exe  // on unix

Or, like us, you may want to set it up as a cronjob and have it run every so often.

Build Requirements

The only package required is FSharp.Data. The package should install automatically when you ‘restore packages’.


This is the first thing I’ve written with F# and my first foray into functional programming. I’m sure the code could be made more succinct and improved. As I am learning, I would appreciate any comments or instruction you would care to offer. Simply open an issue on this repo.

Secure Login For MySQL Via The Command Line With No Password Prompt

The goal here is to be able to type a one word shell command at the shell’s command prompt and have that drop you into a MySQL command prompt on the host machine and database of your choosing.

For example:


mysql  Ver 14.14 Distrib 5.6.25, for osx10.10 (x86_64) using  EditLine wrapper

Connection id:      5162
Current database:   web_manager
Current user:       xxxxx@xxxxxxxxxxxxx
SSL:            Not in use
Current pager:      stdout
Using outfile:      ''
Using delimiter:    ;
Server version:     5.5.43-0ubuntu0.12.04.1 (Ubuntu)
Protocol version:   10
Connection:     dev via TCP/IP
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    utf8
Conn.  characterset:    utf8
TCP port:       3306
Uptime:         33 days 10 hours 12 min 26 sec

Threads: 1  Questions: 1210383  Slow queries: 85  Opens: 1358  Flush tables: 1  Open tables: 114  Queries per second avg: 0.419

Sure, you could do this with:

shell>mysql -uroot -p -hdev web_manager

But, you’d be prompted for the password every time and that gets old. Of course, you could just include the password on the command line but that would be insecure. Here’s how you fix that.


If you already have the MySQL command line utilities installed on your box, you’re good to go. Just type “mysql –version” to verify you have them. If not, you can download them here:

The mysql_config__editor command will create an encrypted file with our password and our databases hidden safely inside. Here’s the syntax.

mysql_config_editor set --login-path=pickanyname --host=actualmachine --user=yourusername --password

Here’s an example:

shell>mysql_config_editor set --login-path=mywebdb --user=root --password

shell>Enter password:

At this point the system is prompting you to type in the password. Enter the password for the root user at and press enter. This command creates the encrypted file and stores it in your home directory. The file name is .mylogin.cnf. You can view it’s contents by typing:

mysql_config_editor print all

To add another login for another database, just repeat the above steps.

Automating things with bash

At this point you could type:

shell>mysql --login-path=mywebdb webdb

Where webdb is the name of the actual database. But, since that’s a lot to type every time you want to login, we’re going to create a bash script to make it much simpler. Fire up an editor and put the above line in a file, save it to your home directory as mywebdb, then from a shell prompt type:

shell>chmod +x mywebdb

This will make the script directly executable. Now just type:


This should log you into your mysql database. If you move the script to a directory that’s in your path for executables, you can drop the ./ before the script name.

Commodore 64 Core War


In 1985 I wrote an implemention of CoreWar on the Commodore 64 using the built in BASIC language. Just the other day I was contacted out of the blue by John Metcalf who is writing a book on the history of CoreWar (next year, it seems, is Corewar’s 30th anniversary). John had seen my reference to a long ago written C64 Corewar implementation in my blogging bio and was contacting me to see if I still had the source for the program.

The Source

My C64 BASIC source code for CoreWar did not make the digital leap to the Internet, LAN networking, or even modern storage technology I’m sad to say. However, it did survive in the form of a printed listing. The print out combined with my pseudo code notes should all but guarantee success to anyone feeling up to the challenge of typing in the nearly three pages of source.

The pseudo code consists of a varible listing with explanations and a top-down design of key modules of the program. The actual source code is a print listing from a vintage Commodore printer. You can download everything at once as a tarball by clicking here.


My inspiration for writing the implementation was firstly, having a computer with 64K of Ram. In 1984 with a purchase of a Commodore 64 I felt sure I’d died and gone to heaven. I immediately began to cast about for a good programming idea to put the Commodore to it’s limits. Secondly, a College Professor friend of mine, Dr. Steve Badger told me about an article he’d read in Scientific America about some kind of Core War wherein combatent programs duked it out to see who would be victor. Having a competitive streak in me a mile long and considering myself the equal of any programmer I knew led me to the Eudora Welty Library where I purchased a reprint of the article for perusing and consideration.

Computer Recreations

The article turned out to be a monthly column entitled Computer Recreations written by A. K. Dewdney. The May 1984 issue was subtitled with “In the game called Core War hostile programs engage in a battle of bits.” I knew immediately it was an idea ripe for my task.

Licensing and Feedback

Please consider my source code free for personal use. You may copy and distribute any way you please. If you do get the source going again I’d love to see a screen shot!

Progressive PHP: See What You’ve Been Missing

I don’t often blog, but when I do…  I always try to come up with something truly useful. A bit of code, a tip, or, as in this case, The Right Way to do something.

So what exactly is The Right Way when it comes to PHP? Since PHP is the hammer I’ve used to nail up solutions everyday for the past 5 years, I’ve tried to stay abreast of the language’s features and offerings. Recently I stumbled across Josh Lockhart’s website PHP: The Right Way.

PHP: The Right Way is a veritable treasure trove of concepts, best practices, and recommendations for PHP coders of all experience levels. As Josh so succinctly puts it, “There’s a lot of outdated information on the Web that leads new PHP users astray, propagating bad practices and bad code”. That’s certainly a truism, and one Josh aims to correct with this website.

PHP5 includes features long decried as essential to a ‘good’ language by critics. This holds especially true for v5.3 and v5.4. OOP, anonymous functions (i.e. closures or blocks), dynamically invoked functions, recursion, and many, many other features, have made their way into PHP5.  Subsequently PHP5 is not only a viable web language solution, it’s also an elegant one.

As evidence, take a look at Slim.php. Slim is a PHP micro framework designed for writing RESTful APIs. If you’ve ever looked at Ruby’s Sinatra framework, you’ll recognize Slim right away. Micro frameworks like Slim are changing the way a modern web solution is coded using PHP.

So, too do other tools like Composer, a PHP dependency manager similar to npm for node.js, and NotORM, a PHP library simplifying database coding,

Even though I’ve referred to it here as Josh’s site, the fact is it’s very community driven.  The source for the site is on github and can be forked by anyone. Josh openly solicits your additions and pull requests.

So let’s help the PHP community and direct new PHP coders to do PHP: The Right Way.  Heck, dare I say it, there are a few of us veteran PHP guys that could stand a visit there too.

Stay thirsty my friend… ;) Front End

A simple JSON example

Availio.fe is a native Mac client I wrote for the domain-search api. Originally developed for mobile web clients by Jonathan Stark and Kelli Shaver of fame, the API provides a web back-end service for domain-search. I wrote a Mac client for primarily as an exercise to familiarize myself with handling JSON within Cocoa.

This program demonstrates a simple example of handling JSON requests between a Mac and a web api. Inspirational credits go to Ray Wenderlic and his awesome iOS 5 tutorial website.

The Code

You can find this source in Xcode project format on github. Following is a short explanation of what’s going on in the source.

Let’s start with a screen shot of the app which we’ll make reference to later.

screen shot of front end

And now a look under the hood.

Continue reading Front End

Easier Email Validation With PHP 5.2.0 or Later

OK, in my never ending quest to NOT burden the SMTP servers with unnecessary work, I’m always on the look out for a more efficient email address validation algorithm. Beginning with PHP v5.2.0 and the introduction of filter_var it’s now as easy (and as quick) as the below code example.

//Validate an email address in PHP 5.2.0 onwards
$email_address = "me@example";
if (filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
    // The email address is valid
    echo "validn";
} else {
    // The email address is not valid
    echo "not validn";

The above example will output: not valid. Hope this helps, Jimmy…

Using PHP to Check a Domain or URL

Just like to jot a quick line when I find a good function or class. This is about the best PHP code I’ve come across to test a URL and see if a Website is available.  The source is from the CSS Tricks site but I found it on CatsWhoCode.

       if (isDomainAvailible(''))
               echo "Up and running!";
               echo "Woops, nothing found there.";
       //returns true, if domain is availible, false if not
       function isDomainAvailible($domain)
               //check, if a valid url is provided
               if(!filter_var($domain, FILTER_VALIDATE_URL))
                       return false;
               //initialize curl
               $curlInit = curl_init($domain);
               //get answer
               $response = curl_exec($curlInit);
               if ($response) return true;
               return false;