#52WeeksOfCode Week 23 – IRC Chat

Week: 23

Language: IRC Chat

IDE(s): Messages + Jabber + BitBee + Chatzilla

History (official):

(From the homepage of #irchelp)

“/I-R-C/ n. [Internet Relay Chat] – IRC provides a way of communicating in real time with people from all over the world. It consists of various separate networks (or “nets”) of IRC servers, machines that allow users to connect to IRC. IRC is very similar to text messaging, but designed around communicating with large groups of users instead of one on one.”

History (real):

Kids, way back in 1988 (ask your parents), the Internet was still mostly called the Arpanet and was not available to average people. Even if you did have access, you had to use your phone to connect to it.

Not your cell phone but a regular old-timey phone. You know, that thing plugged into the wall at your grandparent’s house that they use to  talk with you at Christmas?

Anyway, the Internet For The Rest Of Us(™) consisted of Bulletin Board Systems (BBSs). These were PCs sitting in someone’s basement connected to a phone line. Still dial-up,sure, but you could share files, play games, chat with other users and have lively discussions.

Though BBSs had a chat program, Jarkko Orkarinen, a student at University of Oulu in Finland, decided that he wanted Internet users to be able to connect with BBS users. So while he was upgrading the chat program on his university BBS, he developed IRC (Internet Relay Chat).

IRC is still around, despite having lost a lot of its user base. Its arcane command syntax makes it intimidating for non-technical users, but its flexibility and open standards make it a useful platform for automated messaging. For example, two normally incompatible software programs that have no communication mechanisms in common can easily be set up to exchange data via IRC.

Discussion:

IRC is actually a communications protocol. In other words, a set of language rules. Any programs that speak IRC can talk to each other, regardless of operating system or platform. IRC sends and receives messages in plain text which makes it pretty easy to write code for both servers and clients. That being said, it’s not exactly the Queen’s English:

/join #52weeks

is the command to join a chat room (channel) named 52weeks. On the other hand, pretty much any program that can send or receive text can speak to an IRC server.

I’d like to bring IRC kicking and screaming into the 21st century for my own amusement. In other words, I want to use the default chat application on my MacBook (Messages) to talk to someone on IRC.

The challenge is that Messages doesn’t know how to talk to IRC and vice-versa. However, there is an open messaging protocol called XMPP (eXtensible Messaging and Presence Protocol). Originally named Jabber, the idea was to create a sort of lingua franca for instant messaging.

The first step is to install a Jabber server. The easiest way to do this is with Openfire. It’s easy to setup, open source and cross-platform. I got the server running, setup an account for myself, pointed Messages to the server and I was up and chatting.

Connecting to Jabber from Messages

Connecting to Jabber from Messages

Okay, there was nobody to talk to but the point is, I was at least able to talk to myself.

Now to set up a gateway to connect my Jabber server to my IRC server. I’ve already set up an IRC server using ngircd (installed from MacPorts). I can verify the server is working by connecting to it with Chatzilla, a Mozilla Firefox addon that lets me easily connect to an IRC server using the URL irc://<server_name> in the location bar.

It’s important to use the same username on the IRC end as on Jabber. A gateway’s good but it can’t read your mind.

Now for the protocol gateway to connect Jabber with IRC. I chose BitBee, a Linux/UNIX  IRC server that knows how to connect to other instant messaging services. I installed BiBee using MacPorts, stopped the ngircd server (I didn’t need it anymore) and started BitBee.

Chatzilla connection to BitBee

Chatzilla connection to BitBee

The key to managing BitBee is the &bitlbee channel. This is where you add the accounts for your other chat services. I set up a Jabber account called tsinclair_irc with the password ‘password’ and added the account to BitBee:

account add jabber tsinclair_irc@macpro15.local password

Now to test the connection. First I sent a message from Jabber to IRC:

Connecting to IRC from Messages through Jabber

Connecting to IRC from Messages through Jabber

Now to see if the message came through on Chatzilla:

Message to Jabber from IRC

Message to Jabber from IRC

I sent “Hello yourself!” back:

Message from Jabber to IRC

Message from Jabber to IRC

Let’s look at the setup from 100,000 feet:

Overview of my messaging setup

Overview of my messaging setup

All along the way, I went for the easy option where available though parts of it were harder than they needed to be.

 

#52WeeksOfCode Week 33 – Groovy

Week: 33

Language: Groovy

IDE(s): Eclipse with Groovy plugins/Groovy Shell

 

History (official):

(From the Groovy Home Page)

Groovy…

  • is an agile and dynamic language for the Java Virtual Machine
  • builds upon the strengths of Java but has additional power features inspired by languages like Python, Ruby and Smalltalk
  • makes modern programming features available to Java developers with almost-zero learning curve
  • provides the ability to statically type check and statically compile your code for robustness and performance
  • supports Domain-Specific Languages and other compact syntax so your code becomes easy to read and maintain
  • makes writing shell and build scripts easy with its powerful processing primitives, OO abilities and an Ant DSL
  • increases developer productivity by reducing scaffolding code when developing web, GUI, database or console applications
  • simplifies testing by supporting unit testing and mocking out-of-the-box
  • seamlessly integrates with all existing Java classes and libraries
  • compiles straight to Java bytecode so you can use it anywhere you can use Java

History (real):

Since this is the third or fourth language I’ve encountered that produces code for the Java Virtual Machine (JVM), I decided to search for “java is dead”:

Searching for "java is dead"

Searching for “java is dead”

Apparently the Java Death Watch has been around since 1996. This raises the bigger question “When is a programming language actually dead?”.

According to the Linguistics Society of America, a human language is considered dead when there are no more native speakers.

Computer languages are trickier. Is a programming language dead if no computers are left to run it or is it when there are no programmers who still use it? If we decide to adapt the definition of ‘dead’ from human language (no more native speakers), we need to ask “What is the computer equivalent of a ‘native speaker’?” This puts us back to square one so that’s no help. We should probably just see how good of a case we can make for either choice.

  • Computers as ‘Native Speakers’ – Computers actually ‘speak’ machine language, 1’s and 0’s. This is very hardware and operating system specific, like different dialects of English (same characters different syntaxes). Every programming language command has to eventually end up translated (directly or indirectly) into machine language. For modern coding languages, the underlying hardware doesn’t make that much of a difference. This argument is a non-starter.
  • Programmers as ‘Native Speakers’ – This makes more sense to me. The code is still around and still runs but nobody is writing new programs with the language, then the  language is dead. Or, to use the phrase of art, in ‘maintenance mode’. (see COBOL)

Groovy is another scripting language that produces code that can run on the Java Virtual Machine (JVM). Others include JRuby, Scala, Vala, Fantom and Jython. In effect, they are all dialects of Java, since you can mix Java code with any or all of them and they run just fine.

So is Java dead? Let’s just say that the rumors have been highly exaggerated.

Discussion:

Groovy is cross-platform and there are a number of ways to install it.

  • Download the binary package and unzip it wherever you want
  • Install the Groovy plugin in either Eclipse or NetBeans
  • If you’re on a UNIX-based machine (like Mac OS X or Linux), you can install the GVM (Groovy enVironment Manager).

I already had Eclipse after installing the Scala IDE (i.e. Eclipse with the Scala plugin) so I went with the  Groovy plugins for Eclipse. It was pretty straightforward.

Once I plugged in the location of the Groovy software, I got a nice selection of add-ons. I just selected what I needed:

Minimum Groovy plugin install

Minimum Groovy plugin install

A quick download, install and restart of Eclipse and I was off to the races. When I came back, there were additional choices in my New menu:

Starting a new Groovy Project

Starting a new Groovy Project

I selected Groovy Project, took the defaults and (like the Scala project) opened up with no source code, just project scaffolding. I had to add a Groovy class to the project to get some source I could work with.

There are three ways to run your Groovy code:

  • Run as Java Application – This compiles Groovy into Java bytecode and runs it on the JVM
  • Run as Groovy Script – Don’t compile, just run the script with the Groovy interpreter.
  • Run as Groovy Console – This is the most interesting option to me. When you choose this, your code opens up into a separate window:
Running in the Groovy Console

Running in the Groovy Console

This gives you an interactive console where you can do some quick experimentation with your code. This is a good choice for rapid development, letting you test out chunks of code before rolling them back into your main project.

Since I’m also comfortable with the command line, I installed the GVM for good measure. It’s pretty easy and gives me access to a command-line Groovy interpreter. Like the Groovy Console, this lets me test out chunks of code for rapid development. Once it’s installed you just call it with the command groovysh:

Starting up Groovy Shell

Starting up Groovy Shell

A sample Hello World in groovysh:

Running Hello World in Groovy Shell

Running Hello World in Groovy Shell

This is not the preferred option for Groovy newbies. I would recommend that you stick with Eclipse or Netbeans.

One of the things that interested me about Groovy was the claim that you can drop Java code into a Groovy script and it will just work as Java and Groovy share virtually the same syntax.

I tested this out with Eclipse. I created a quick Java version of “Hello World!”, copied and pasted the code into my Groovy project (commenting out the original code) and it worked!

Java code with Groovy - it works!

Java code with Groovy – it works!

This gives Groovy an additional advantage, as Java programmers will have a much less steep learning curve when the transition to Groovy.

 

#52WeeksOfCode Week 32 – Assembly Language

Week: 32

Language: Assembly

IDE(s): SciTe + NASM

History (official):

(There is no one version of Assembly language since it is aimed at specific hardware. However, I did find this in the Forward for The Art of Assembly Language by Randall Hyde which I thought was amusing):

Egads. What kind of book begins this way? What kind of author would begin the book with a forward like this one? Well, the truth is, I considered putting this stuff into the first chapter since most people never bother reading the forward. A discussion of what’s right and what’s wrong with assembly language is very important and sticking it into a chapter might encourage someone to read it. However, I quickly found that university students can skip Chapter One as easily as they can skip a forward, so this stuff wound up in a forward after all.

So why would anyone learn this stuff, anyway? Well, there are several reasons which come to mind:

 

  • Your major requires a course in assembly language; i.e., you’re here against your will.
  • A programmer where you work quit. Most of the source code left behind was written in assembly language and you were elected to maintain it.
  • Your boss has the audacity to insist that you write your code in assembly against your strongest wishes.
  • Your programs run just a little too slow, or are a little too large and you think assembly language might help you get your project under control.
  • You want to understand how computers actually work.
  • You’re interested in learning how to write efficient code.
  • You want to try something new.

 

 

Well, whatever the reason you’re here, welcome aboard. Let’s take a look at the subject you’re about to study.

History (real):

I feel the need to lend some perspective before I dig into Assembly language, otherwise known as Assembler.

This is a picture of the first personal computer ever sold, the MITS Altair (from OldComputers.Net):

MITS Altair image, courtesy of OldComputers.net

The First Personal Computer

 

The Altair was introduced in January of 1975. It had 256 bytes of RAM (up to a maximum of 64K), a 2.0 MHz Intel 8080 CPU and was available for $395 as a kit. You got a box of parts. You had to make your own circuit boards. If you wanted it pre-assembled, that was an extra $255.

I want to draw your attention to the front panel of this computer. Yes, I mean all of the blinky lights and flippy switches. If you wanted to program this personal computer, you had to flip the switches on the front panel, entering your commands one word at a time in machine language (a series of 1’s and 0’s).

Now because there was no floppy drive, no hard drive, no secondary storage of any kind, if someone happened to trip over the power cord while you were flipping switches, you had to start over from the very beginning.

I mention this to tell you that no matter how arcane and hard to write, maintain, debug, read and understand Assembler is, things could be (and have been) much, much worse.

Discussion:

While Assembler was a step up from machine language, it wasn’t what you would call a giant leap. You still have to shuffles 1’s and 0’s around and have a deep understanding of the underlying hardware of your computer, but at least it took slightly less time to write code.

But if we now have all of these high-level languages, why bother writing code in Assembler?

  • Speed – Assembler programs are extremely efficient and very fast.
  • Size – The efficiency of Assembler also means that programs take up very little space.
  • Power – Since you’re working very close to the hardware, you can do things with Assembler that you can’t do with higher level languages

It’s not as if you have to choose between one or the other. If you have a section of code that needs to run quickly and efficiently you can write it in Assembler and hook it into your higher-level code.

For this exercise I decided to use NASM (The Netwide Assembler), a free, cross-platform assembler for Intel-compatible processors. There doesn’t seem to be a good IDE for Assembler (rumors exist of an Assembler plugin for Eclipse) so I’m going to use a text editor. Though NASM is available for Mac OS X, documentation on how to use it is limited or out-of-date. Linux, on the other hand, is well-documented so I’ll be using Debian 7 for my development platform and SciTe as my text editor. It’s free, cross-platform and does syntax highlighting for a number of common programming languages.

I looked for a “Hello World!” program in Assembler and thanks to a very nice tutorial from Ray Toal at Loyola Marymount University, this is what I found:

Hello World in Assembler

Hello World in Assembler (Using the SciTe text editor)

Compiling and running just involved a bit of typing.

Compiling and running code

Compiling and running our code

Before we look through the source, let’s see what’s happening at the command line.

nasm -felf64 hello.asm

The source code file is called hello.asm. This command tells NASM what format (-f) the output should take. In this case it’s 64-bit ELF (Executable Linkable Format, the standard binary format for Linux executables, object code and software libraries). NASM takes the source code (hello.asm) and compiles it into the object code file hello.o. Object code is machine code, a set of binary instructions.

However, as far as the computer is concerned it’s just a pile of 1’s and 0’s.

ld hello.o && ./a.out

There are actually two commands here.

ld hello.o

The first runs the linker (ld) to link the object code with the ABI (Application Binary Interface) for the target platform. It’s like putting a ‘Start’ button on our code so our processor knows how to run it. The default output is an executable called a.out. The line

./a.out

runs the program to print “Hello World!”. The ‘&&’ between the two commands simply means ‘if the first command succeeds run the second command, otherwise just stop’.

Now let’s go through the source code line-by-line:

# ----------------------------------------------------------------------------------------
# Writes "Hello, World" to the console using only system calls. Runs on 64-bit Linux only.
# To assemble and run:
#
#     gcc -c hello.s && ld hello.o && ./a.out
#
# or
#
#     gcc -nostdlib hello.s && ./a.out
# ----------------------------------------------------------------------------------------

 

Anything that starts with a pound sign (hashtag for you youngsters) is a comment and is ignored by nasm. This is pretty standard Linux syntax and you may see the semicolon (;) used for a similar purpose on other platforms.

       .global _start

The global keyword marks symbols that need to show up in the output (hello.o). As you might suspect, NASM is showing the linker where the executable starts.

       .text

This is just a name for a section of code. Remember, the output is just a string of 1’s and 0’s which could mean anything, depending on where you start reading and how many bits you read at a time. This keyword .text tells us that that the following section is runnable code, not data.

_start:

As stated above, here’s where our actual code starts, in case it wasn’t obvious.

       mov     $1, %rax
       mov     $1, %rdi
       mov     $message, %rsi
       mov     $13, %rdx

A machine language instruction consists of two parts, the opcode (what the code should do) and the operand (the data on which the opcode should act). In these four lines, .mov is the opcode for loading data into a register (a small fast chunk of memory used for temporary storage). These four instructions copy data into the registers rax, rdi, rsi and rdx.

The first (rax) holds the location of the system call we’re going to use, which is write ($1).

The second (rdi) is the memory location where we’re going to write our message which is standard output ($1).

The third (rsi) is the string we’re going to write to output. ($message is just a label for the string data, which we’ll find elsewhere in our code. Hang on.)

The fourth (rdx) is how much of our message to print, 13 bytes (or characters).

We’re loading a sequence of commands. Write 13 bytes of our message to standard output. It’s like setting up a series of dominoes, just waiting for that lone finger to start the string of activity.

And here comes the finger:

            syscall

Since our system already knows how to print strings, why reinvent the wheel? This opcode tells our processor to read the four sections of memory we’ve set up and act accordingly. We still have to tell the processor we’re done so let’s set the dominoes up again.

       mov     $60, %rax
       xor     %rdi, %rdi

The first loads the exit command ($60) into our register rax. The second is the exit code for our program to spit out, in this case 0. Ready for the finger again?

       syscall

Finally we come to our data section.

message:

       .ascii  "Hello, world\n"

This is labelled with message: so we can refer to it elsewhere in our code, as we saw above. In this case, it contains the string that we want the system to print to standard output. It’s labelled as plain old alphanumeric text, because we have to specify how we want the processor to interpret those 1’s and 0’s.

On the plus side, Assembler is a very powerful way to write fast code that takes up very little memory space. On the minus side, you have to keep track of where your data is at all times and what takes a single line of code in a high-level language takes thirteen in Assembler.

Not that there’s anything wrong with that.

Hyde, Randall. The art of assembly language. No Starch Press, 2003.

 

#52WeeksOfCode Week 30 – The R Project

Week: 30

Language: The R Project

IDE(s): R

History (official):

(From the R Project “What is R?” page)

R is an integrated suite of software facilities for data manipulation, calculation and graphical display. It includes

 

  • an effective data handling and storage facility,
  • a suite of operators for calculations on arrays, in particular matrices,
  • a large, coherent, integrated collection of intermediate tools for data analysis,
  • graphical facilities for data analysis and display either on-screen or on hardcopy, and

 

  • a well-developed, simple and effective programming language which includes conditionals, loops, user-defined recursive functions and input and output facilities.

 

Once again, this is what you get when programmers write your sales materials – nothing but facts.

Boring, tediously informative facts.

History (real):

In the Olden Days (™), if you wanted a computer to do your math homework, you had to use FORTRAN. It wasn’t what you might call ‘interactive’. You wrote your code, submitted it to the mainframe, which compiled and ran it. Assuming you didn’t have any typos, you got a printout of the results. (FORTRAN was my first programming language back in 1977. We used punch cards.)

This was always annoying and occasionally painful, but there were no good alternatives until the mid-70’s, when researchers at Bell Labs developed the programming language ‘S’. It was standard practice at the time to give programming languages single letter names.

I’m picturing the marketing meetings:

“How about ‘Bell Labs: We Don’t Have Time For This’?”

“Not bad. But I really like ‘Bell Labs: Smart But Terse’.”

“Love it!”

Moving on.

In the early 90’s, researchers at the University of Auckland, New Zealand developed a new version of S that they called R. Currently it’s being maintained by the R Development Core Team, with contributors  from all over the world. The name R is not just a play on the name S, but is also a tribute to the original developers, Robert Gentleman and Ross Ihaka, who were known at university as “R & R”.

R is free and available for Linux, Windows and Mac OS X. The source code is also freely available so you can compile it for any platform you like.

Discussion:

I’ve been looking forward to this for some time. I teach undergraduate math and occasionally blog about math education so math software is a particular interest of mine. I’m a firm believer in letting machines do the grunt work of mathematics. If you understand the problem well enough to explain it to a computer, then by definition you understand the problem.

I downloaded the Mac version of R from the main project site. R is a command line based tool so I wasn’t that surprised when I started up the program and got a window with a command prompt:

R for Mac startup window

R for Mac startup window

 

The window has a toolbar with easy access to common functions:

  • Load data or a script file
  • Open a new window (for charts and plots)
  • Authorize R to run commands as root (system administrator)
  • Show/hide R command history
  • Set R console colors
  • Open document in editor
  • Create a new empty document
  • Print this document
  • Quit

Most of your work is done at the command line.

Before we get going, I’d like to perform the traditional “Hello World!”:

Hello World! in R

Hello World! in R

This term I’m teaching an introductory statistics course so I’ve got some sample data from classroom exercises to run R through it’s paces. R stores with data tables in variables called data frames. There are pre-loaded data frames available in the software with which to experiment. You can enter data manually or just load the data from an outside source.

My data is in spreadsheet format and there are a number of ways to import spreadsheet files directly into R. Since I use Google Sheets as my primary spreadsheet program, the easiest way for me was to save off my data in CSV format. (Excel files can be imported directly.)

R assumes files are in the current working directory. The default is my home directory so I changed that setting to where I’d saved the files.

Get and set the working directory

Get and set the working directory

My first test data was a simple table comparing car prices to the age of the car. I chose a specific make and model (Toyota Sienna) and pulled these numbers straight from AutoTrader.com. I converted the worksheet and loaded the data table into R:

Car age vs. price data table

Car age vs. price data table

Now I can work with it directly. First let’s give it a once-over using the summary() function:

Summary of data table

Summary of data table

This gives me my central tendency numbers amongst others. Now let’s do a quick plot using using the pairs() function.

Data plot using pairs()

Data plot using pairs()

I got two plots, one with age as the dependent variable and other with price. I didn’t tell R which was which so it did both. I can be more specific using the plot() function:

Plot command syntax

Plot command syntax

This tells R that price is dependent on the age of the car. This gives me a single chart:

Chart using plot()

Chart using plot()

Now I can calculate the correlation coefficient with cor() to see how strongly the two sets of data relate to each other:

Correlation Table

Correlation Table

So price is negatively correlated with the age of the car, which fits what the chart told us. Older cars cost less, in other words. It’s a pretty strong correlation, too, at 85%.

Now we’d like to do some prediction so we’ll perform a linear regression on the data. First create a data structure with the regression data, then pull a summary:

Linear regression

Linear regression

Now you have a processed data set and you can continue working with it.

We can get data in and manipulate it but how do we get it out? For text data, such as the correlation summary, you can just copy and paste it from the R gui window. The plots appear in a separate window. I was able to click on the image, select Copy from the Edit menu and paste directly in a document.

R is a very powerful, interactive language for scientific and math computing. So why would you use it instead of a spreadsheet?

Frankly, if you’re not a full-on numbers nerd, you may just want to stick with spreadsheets. But they’re a general purpose tool and R has more math functionality. You can write your own functions and even groups of functions (called packages) to extend R even further.

Another advantage of R is automation. Anything you can type in at the command prompt can be saved into a file, letting you easily set up long, complex sets of calculations that can be loaded into your workspace with a single command. If you’re doing batch processing of multiple datasets, this can save a lot of time and effort.

The documentation is very good and there are plenty of tutorials and examples available at the project homepage and around the Web.

 

#52WeeksOfCode Week 29 – Clojure

Week: 29

Language: Clojure

IDE(s): NightCode

History (official):

(From the official Clojure home page)

Clojure is a dynamic programming language that targets the Java Virtual Machine (and the CLR, and JavaScript). It is designed to be a general-purpose language, combining the approachability and interactive development of a scripting language with an efficient and robust infrastructure for multithreaded programming. Clojure is a compiled language – it compiles directly to JVM bytecode, yet remains completely dynamic. Every feature supported by Clojure is supported at runtime. Clojure provides easy access to the Java frameworks, with optional type hints and type inference, to ensure that calls to Java can avoid reflection.

Clojure is a dialect of Lisp, and shares with Lisp the code-as-data philosophy and a powerful macro system. Clojure is predominantly a functional programming language, and features a rich set of immutable, persistent data structures. When mutable state is needed, Clojure offers a software transactional memory system and reactive Agent system that ensure clean, correct, multithreaded designs.

I hope you find Clojure’s combination of facilities elegant, powerful, practical and fun to use.”

 

History (real):

That was the equivalent of a marketing release for Clojure. In other words, it’s targeted at the ‘suits’. If you want the sales page for the technical crowd, see the Rationale page.

I have no personal animus against Clojure. Today was the first time I had heard of it. But I have to say that my first thought was “What? You mean I can at last combine the terse, unreadable code of Lisp with the performance hit of running Java? Sign me up!”

Of course, the true story is more complex.

One of the strengths of the Java platform is the Java Virtual Machine (JVM). A Java program compiles to bytecode which runs on the JVM. The JVM gives Jave its portability between operating systems. Microsoft’s .NET language followed suit with the CLR (Common Language Runtime).

Another advantage of the JVM, however, is that Java bytecode is an open, documented standard. That means that you can use any programming language you want to write Java applications, as long as you can compile your language to produce bytecode. Examples include Jython and JRuby that let programmers write Java programs in Python and Ruby, respectively.

Clojure lets programmers write in Lisp and produce code that can easily integrate with Java and code that uses Microsoft’s .NET framework. It can also connect to JavaScript libraries like Node.js for Web applications.

This is a cool idea. Scripting languages do make for faster prototyping and development and bytecode compilation does improve performance a bit (but not as much as going all the way down to machine code).

But why Lisp for scripting? Let’s ask the creator of Clojure, Rich Hickey. From the Clojure Rationale page:

Why did I write yet another programming language? Basically because I wanted:

 

  • A Lisp
  • for Functional Programming
  • symbiotic with an established Platform
  • designed for Concurrency

 

I think the third feature is the important one. We already have multiple dialects of Lisp. Erlang is a functional programming language that supports concurrency. But neither of these can integrate so easily with existing code as Clojure.

I think that if I had a complaint, it would be that the scripting language was based on Lisp. But that’s my own personal discomfort and unfamiliarity with functional programming and not meant to diminish Hickey’s accomplishment in any way.

Discussion:

So how do I get started coding in Clojure? After a bit of research, I found that writing the code was easy enough but a real Clojure project involves keeping quite a few metaphorical plates spinning. Managing all of the files, code dependencies and project details can get hairy very quickly, particularly if you work from the command, which is the bare-bones Clojure install default.

There is an existing tool, make, which can handle code projects but it’s finicky to configure. Java has similar project management issues which are handled by a tool called ant. Fortunately some contributors to Clojure created an automation tool named leiningen. It works so well that it’s become the default tool for working with Clojure projects.

I didn’t feel like working from the command line (again) so I searched for a nice IDE for my first Clojure project. There are Clojure plug-ins for popular Java IDEs like Eclipse, NetBeans and Intellij but I wanted a program that was built specifically for Clojure.

Enter NightCode. Not only was it written for Clojure projects, but it was written in Clojure. I felt this showed admirable brand loyalty so I grabbed a copy. The interface is pretty bare-bones:

NightCode on first startup

NightCode on first startup

I only have three options: Create a new project, import an existing project or run code interactively in the little interpreter window on the lower left. This is a pretty neat feature because you can jump out of your current project, quickly test out some scrap of code to make sure it works the way you think it does and then paste it back into your main source file without leaving the IDE. Very nice:

NightCode interactive shell

NightCode interactive shell

I created a new project and after giving my project a name was presented with a list of project templates:

NightCode template selection

NightCode template selection

I like templates. They let me get on with my life.

I picked Console and selected Create Project.  In a few moments my first project was ready to code:

A simple console application project

A simple console application project

I have a few more options now. One thing I noticed were the options Run, Run with REPL, Build and Test. Since Clojure is a scripting language, it’s possible to run code without actually creating an application. Run with REPL runs your code interactively which is useful for debugging. Test checks your code for logic and syntax errors. Build, of course, creates the standalone software application. It’s still a very sparse interface, but it looks like you could do some non-trivial projects in this IDE. Not bad for free software.

On to my trivial project. The console project template is a standard “Hello World” so I use it to take both Clojure and NightCode for a spin.

Here’s the code, in case you’re curious:

Hello World in Clojure

“Hello World!” in Clojure

I’d like to test my code before running it so Test automatically inserts test code into my program, compiles and runs it:

Automatic code testing

Automatic code testing

Selecting Run gives me this:

Running my code

Running my code

Run with REPL loads the code and drops me into an interactive shell:

Running interactively with REPL

Running interactively with REPL

Now I’m ready to create my standalone application with Build:

Building a standalone application

Building a standalone application

Clojure is a unique, versatile language and NightCode gives me enough tools to handle non-trivial projects with confidence.

#52WeeksOfCode Week 28 – Vala

Week: 28

Language: Vala

IDE(s): TextWrangler

History (official):

(From the Vala project home page)

“Vala is a new programming language that aims to bring modern programming language features to GNOME developers without imposing any additional runtime requirements and without using a different ABI compared to applications and libraries written in C..”

History (real):

Oh, there’s so much to unpack here.

new programming language” – okay, I’m with you so far.

modern programming language features” – I think I need some details on this one, bub.

GNOME developers” – Wait, I know this one! GNOME is a free, open source, cross-platform desktop environment. It’s mainly found on Linux systems, but you can also install it on Windows, Mac OS X and other UNIX variants.

The GNOME Project was started because the major desktop at the time, KDE (K Desktop Environment) used some proprietary code which made it problematic for certain open source projects. GNOME, on the other hand, is built on completely free code and all of the source code is readily available for download and hacking.

without imposing any additional runtime requirements”- A runtime system consists of any additional files required to run a program. Every piece of software has some kind of runtime requirements. In the case of Vala, this statement simply means that you don’t need to install anything extra to run applications written in Vala. (We’ll get to the reason why in a bit.)

without using a different ABI” – ABI is short for Application Binary Interface. An ABI, on a high level, is a set of rules for communication between different software modules. One use of an ABI is to allow programs to talk to the host operating system. The ABI specifies things like how to pass information from one program segment to another. Normally, a developer doesn’t have to worry about the ABI (it’s taken care of by his development system) except in certain cases when he’s writing code in different programming languages and he needs all of his programs to talk to each other. The ABI referred to in the official statement above is the GNOME ABI. (Like the runtime thing above, we’ll get to the why in a bit. Spoiler alert: it’s the same reason for both.)

To sum up, Vala is a good language for writing GNOME applications because you don’t need to install anything extra except a Vala compiler. Sounds good!

Just a minute. GNOME developers were writing software before Vala came along. Why do we need another programming language?

For the answer, we’ll have to send our imaginations back to the early ‘70s. The Summer of Love was still in recent memory, the conflict in Vietnam was winding down and the voting age was lowered from twenty-one to eighteen. (“Old enough to fight, old enough to vote”)

Almost unnoticed during all of this excitement, Bell Labs researcher Dennis Ritchie was creating a new general purpose programming language that he called C. This by itself wasn’t that exciting but C was used to write the first release of AT&T Unix.

I can sense I’m not exactly blowing your skirt up with this, but hear me out.

Prior to that time, computer operating systems (the software that runs the hardware) were written directly to the hardware using assembly language. An assembly language was very low-level code that produced software that would only run on a specific type of hardware. That meant that if you wanted to run Awesome OS (™) on a different type of computer, you had to completely rewrite the operating system. Needless to say, this slowed down innovation in the computer industry significantly.

Here’s my point.

C was the first high-level (ie. human-readable) programming language that could be used to write operating systems. The beauty of high-level languages is that as long as you have the original code, getting programs to run on different computer hardware is pretty easy.

Dennis RItchie, in short, was the father of the portable operating system.

The consequences of this were huge. Computers can’t run without an operating system and the difficulty of creating an operating system for new computer hardware was a serious limitation to computer hardware innovation. Ritchie’s work revolutionized the computer industry.

What does this have to do with Vala? True to its UNIX roots, GNOME was written in C. Now C is a solid reliable programming language (as evidenced by the fact that it’s still in use over four decades later). However, C doesn’t have the features of more modern languages and so for general purpose programming it imposes more and more limitations on the developer as the state of the art advances. They have to work harder just to stay in place.

Vala is based on a pretty clever idea. It has all of the snazzy bells and whistles you expect from a modern programming language so it’s easy for developers used to newer languages like C# to pick up.

But the Vala compiler converts Vala code to C code which is then compiled into just another GNOME application. That’s why you don’t need to install any extra software or libraries to run programs written with Vala.

Discussion:

Once you have the Vala compiler installed, all you really need to start writing code is a text editor. I installed Vala using MacPorts and I’m going to use TextWrangler to write my code.

A basic Hello World program in Vala looks like this (courtesy of the GNOME Vala tutorial):

int main () {
   print ("Hello World\n");
   return 0;
}

 

Compare this to the same program (courtesy of the Content Creation Wiki) written in C ( and Vala reveals its roots:

#include <stdio.h>
#include <stdlib.h>

 int main(void)
 {
  printf("Hello, world\n");
  return EXIT_SUCCESS;
 }

Now I just compile my code and run the program:

valac hello01.vala
./hello01
Hello World

Pretty straightforward, but not very modern. Let’s jazz it up a bit using some modern paradigms (courtesy of Dream.In.Code):

using GLib; // not required
public class HelloObject : GLib.Object {
   public static int main(string[] args) {
       stdout.printf("Hello World!\n");
       return 0;
   }
}

It still works:

valac hello02.vala
./hello02
Hello World!

The Vala Project has created a programming language that both embraces modern development techniques and honors its roots.

#52WeeksOfCode Week 26 – Erlang

Week: 26

Language: Erlang

IDE(s): TextWrangler

History (official):

(From the Erlang home page)

“Erlang is a programming language used to build massively scalable soft real-time systems with requirements on high availability. Some of its uses are in telecoms, banking, e-commerce, computer telephony and instant messaging. Erlang’s runtime system has built-in support for concurrency, distribution and fault tolerance.”

History (real):

Erlang is one of those languages like Factor or Scala or Forth that were designed to solve a very specific set of problems. As such, it’s never going to enjoy wide popularity.

That’s not to say that Erlang isn’t important. Your favorite web site may not run on Erlang code any time soon but it’s quite possible that your bank is using it.

There are two features of Erlang that I think I should explain in more detail.

“soft real-time” – A real-time system is one where data is processed and the response has to be immediate. The anti-lock braking system on your car is a good example. You don’t want to stomp on the brakes and have to wait for the software that controls your satellite radio to finish scanning channels. A soft real-time system is one where we need a quick response but not as quickly as a full-on “hard” real-time system like your car’s brakes. A good example is the software that updates flight plans for airlines. We want the plans to be kept current but we’re willing to accept a few seconds delay for processing time.

“concurrency” – We normally think of program code as executing one command after another in sequence, ie A then B then C. But what if A, B and C weren’t dependent on each other? We could do all three at the same time and get things done that much faster. Concurrent programming is designed to make it easy to split independent tasks and distribute them to different processors. The processors can be on the same computer or on multiple computers. This is also referred to as ‘parallel processing’. It’s useful if you’re processing a massive amount of data, like rendering CGI for a movie.

Erlang is also described as a declarative language. In declarative programming, the code describes what needs to be done without telling the computer how to do it. Declarative languages tend to have code that is very tight and fast but not very readable. Fans of declarative programming like it because it maps very closely to mathematical logic. So if you enjoyed doing proofs in geometry class, you’re going to love declarative programming.

Discussion:

Since Erlang programs are just plain text files, you can use almost any IDE you want. There are plugins for Eclipse, NetBeans, UltraEdit and other programming editors. I’m going to use a plain old text editor, good old TextWrangler.

First I have to install the language software. It’s available for Linux, Windows and Mac OS X. You can also compile it up from source. Since it doesn’t come with Apple’s Developer Tools, I had to install Erlang using MacPorts.

MacPorts is a project that lets you install free, open source software on Mac OS X. Most of them run from the command line so they’re not for casual users. But if you’re coming from a UNIX or Linux environment you can find all of your favorite tools and make yourself at home.

Normally, Erlang code needs to be compiled (converted to machine code) but you can also run it directly using a tool called Escript.

That being said, this is the hardest programming language I’ve approached yet. I mainly have experience with traditional imperative languages so I still have trouble changing my thinking to accommodate a declarative language like Erlang. The other problem is one of documentation.

Technical documentation falls into two broad categories. The first is what I call the ‘howto’. It walks you step-by-step through a procedure to accomplish a task. There is no context for what you’re doing and if there’s a typo or a missing step you’re stuck.

The second category is technical reference. These give you more information about what’s going on ‘under the hood’, but there’s no applied knowledge. In addition, they are written for people who don’t actually need or want to read them.

So far, Erlang documentation falls into the second category. Clearly if you want to write even a simple program in Erlang you need to be willing to make a substantial commitment. Don’t expect a lot of hand-holding.

For that reason, I’m going to just do a ‘Hello World’ program as a proof-of-concept and walk you through the steps I had to take to get there.

First the code:

-module(hello).
-export([start/0]).
start() ->
   io:fwrite("Hello, world!\n").

Let’s take this one line at a time.

-module(hello)

defines a function (or module) called hello. This has to be the first line.

-export([start/0])

This tells Erlang what part of this code can be called from outside the program. This lets you have some code that is internal to your program.

start() ->

Remember that start we exported in the previous line? The code we’re exporting begins here.

io:fwrite("Hello, world!\n").

The io module has a built-in function fwrite and we’re running it with the argument “Hello, world\n”. (The \n means print a newline at the end.)

I save this in a file called hello.erl.

I compile it.

erl -compile hello.erl

Now I can run the program.

erl -noshell -s hello start -s init stop

And the result?

Hello, world!

Mission complete! Now I have to go lie down….