In this installment of “Functional programming without feeling stupid” I would like to show you how to define things in Clojure. Values and function applications are all well and good, but if we can’t give them symbolic names, we need to keep repeating them over and over.
Before we start naming things, let’s have a look at how Clojure integrates with Java. I’m assuming you are still in the REPL, or have started it again with
Track 1: “If anyone should ask / We are mated”
As it happens, Clojure’s core library is lean and focused on manipulating the data structures of the language, so many things are deferred to the underlying Java machinery as a rule. For example, mathematical computation is typically done using the static methods in the
user=> (java.lang.Math/sqrt 5.0) 2.23606797749979
As you can see, this is a function application like we have already seen in Part 1, but this time the function we are using is the
sqrt static method in the
Java 7 acquired Unicode character names, and they are accessed through
getName method in the
java.lang.Character class. This is no mean feat, since there are over 110,000 characters in the Unicode standard, and each of them has a name (although some of them are algorithmically generated). To find out the canonical character name of a Unicode character, such as the euro currency symbol, you would use the
getName static method:
user=> (java.lang.Character/getName \u20ac) ClassCastException java.lang.Character cannot be cast to java.lang.Number user/eval703 (NO_SOURCE_FILE:1)
Hey, what’s wrong? Well, if you look up the documentation of
java.lang.Character.getName, you will find out that it takes an
int value as an argument, not a character. You can actually do this check from inside the REPL:
user=> (javadoc java.lang.Character) true
The REPL doesn’t seem to do much, but you should now have a new web browser window or tab open, with the JavaDoc of the java.lang.Character class loaded up. That’s what the REPL meant when it said
Javadoc: (javadoc java-object-or-class-here)
when it started up. The
getName method does need an
int value, so let’s try something else:
user=> (java.lang.Character/getName (int \u20ac)) "EURO SIGN"
All right! How about another one:
user=> (java.lang.Character/getName 67) "LATIN CAPITAL LETTER C"
Well, some say that Clojure is the new C.Read more →
In my recent post Functional Programming Without Feeling Stupid I took a quick look at how functional programming can be a little off-putting for the non-initiated. I promised to provide some examples of my own first steps with FP, and now I would like to present some to you.
If you are thinking about getting intimate with Clojure, you will need to get to know the REPL. It is your playground, and will always be, even if you later start packaging and organizing your code.
Clojure depends on the Java Virtual Machine (JVM) and is actually distributed as a normal JAR file (Java ARchive), like most Java libraries are. You can start Clojure from the JAR, but you will save yourself some trouble and prepare for the future if you install Leiningen, the dependency management tool for Clojure. It is simple to install and run, and I will assume that you will follow the instructions on the Leiningen web site sooner or later. Now would be a good time.
When you’re done with the installation, you only need to say
to start a Clojure REPL. I’m using OS X, so what I describe here was done from Terminal. You don’t need to create a project with Leiningen if you just want to play around in the REPL.
Of course, if you don’t have Java installed, you need to get it first. Refer to the Java web site of Oracle for details as necessary. Furthermore, some of the things I will describe require Java version 7 or later.Read more →
Apart from hard-coding “Hello, world!”, another bad habit you may have picked up when you learned programming is constructing user-visible messages from parts: strings, numbers and other data, concatenated together.
For example, say that you had to show the user how many unread messages there are in a given mailbox. Let’s assume that
mailboxName contains the name of the mailbox, and
messageCount holds the number of unread messages. In Java, you might be tempted to whip up a user-visible message like this:
String message = "There are " + messageCount + " unread messages in mailbox '" + mailboxName + "'";
This is not the way to do it in an international application. Let’s find out why, and have a look at a better and much more future-proof way of doing it.Read more →
Many introductory programming courses start with a very simple first program, the traditional “hello world”. It is intended to teach how to edit, compile and run a program in a new programming environment using a simplified context, and it serves its purpose quite well. Unfortunately, the simple practice used in storing and displaying the text is quite contrary to a basic principle of software internationalization.Read more →