The Blog of Josh Comer

My First Clojure Library

I just pushed my first Clojure library to CloJars called cljcolour. In my attempt to emulate Todo.txt, I needed the ability to colour my terminal output. Apparently there is no built in way to do this (I’m not even sure if there is an easy way to do this in Java). A little googling later and I found ANSI escape characters for colouring text, and they are disgusting. With cljcolour it is now easy to add both fore and background colour to your text output.

I have only tested this on my mac, so no guarantees when it comes to windows. I have also noticed that the REPL will only apply one colour (either the fore or the background) to your string. Both colours do work when running your application from the command line.


;Probably the easiest usage is to create painters
(def bee-painter (create-painter black yellow))
(bee-painter "Hello World")

;You can also define fore and background combinations
(def bee-colour (create-new-colour black yellow))
(def bee-painter2 (create-painter bee-colour))

Who Needs an Object When You Have a Map?

I had been thinking about the parsing code I initially written for TodoClj I remembered that I had initially set out to mimics Todo.txt, so I have reoriented and now I am parsing the same format accepted by Todo.txt.

File /home/josh/code/octopress/source/downloads/code/Clojure/coreV2.clj could not be found

I feel that I am getting more and more comfortable with using clojure. I have stopped using Eclipse and instead use Sublime Text 2 and the REPL provided by lein for my project.

Found Some Time to File Parse

I was able to find some time tonight to do a little programming. The following code snippit will parse the Todo.txt file. I kept the structure of the file down to a minimum.

T The First Task
D and The Second Task

The first letter of the line will represent the status of the task (T being still Todo and D being Done). The code will read the entire file in and “hackishly” convert the line in to a vector. The first item is assumed to be either a T or a D and the remainder of the vector is the description, and is interposed with spaces to be a readable string. File /home/josh/code/octopress/source/downloads/code/Clojure/coreV1.clj could not be found

Java Unit Testing

I was digging around in a project I helped created for my intro Software Engineering class, and came across some useful helper methods I wrote for unit testing.

These methods allow you to call private methods and get/set the values of private class variables. In order to call the private members, I needed to use reflection, which as you can see is horrible in Java. Luckily I was able to write the methods using generics so they can very easily be reused. The downside is that this code will not compile using the standard oracle Java 1.6 (see the bug report). This bug was supposed to be fixed in Java 7, but I haven’t tested it yet. In the mean time there is no problem compiling the code using the OpenJDK.

Now With More State

So I managed to sit down tonight and add state passing to the state machine. It now appears to be time to add support for doing actual things.

As I was writing the init function I was questioning what options will truly be required. I may want to cache the file contents in memory, which can be kept in the state hash. We shall see!

Hopefully tomorrow I will have some time to sit down and add in at least the create.

There Has to Be State

It occurred to me after I posted the prototype state machine that I had missed an important piece, State. I know that state is a naughty word when discussing functional languages, but an application must have a state. The bad taste surrounding state in functional languages is in reference to mutability of variables, as opposed to the application remembering which file it is acting upon.

It should be completely possible to enclose the current state of the application into a parameter for the state machine. Each action method will accept and return the state of the application stored in a hash. I will post a prototype of said functionality later tonight, assuming I don’t get too distracted or see a shiny object.

I have also created the first draft of the state machine (in diagram form).

On a side note, it is hard to find a good free diagramming tool. After trying out a couple I ended up settling on Google Docs and the Draw tool. I had pretty much endless storage, could make drawings as large as I wanted, and could export into all sorts of formats.

Tail Recursive State Machines

I was reading an article the other day which stated that a great method to use when learning a new language is to imitate software that you already use. In my attempt to fully grok Clojure I have decided to start small and imitate todo.txt

The first hurdle I always face when starting a new language is determining the basic architecture pattern to follow when writing an application. For some reason this is a topic which is not widely discussed or written about. Some languages, such as Objectuve-C,have a prescribed architecture which you are to follow, thereby making my difficulties moot. But clojure and the majority of other languages do not have this benefit. We are left to discover these patterns by ourselves.

Perhaps the intension is that one is supposed to trail and error and determine what works the best for themselves. Maybe the idea is that each application is different enough that there cannot be a default application pattern for a language. Web development seems to alleviate this problem by not dictating the pattern to the language, but instead to the framework with which one constructs their site.

So, in light of this I am attempting to formulate an application pattern which I think will work with my thought patterns. I am intrigued by the concept of creating applications around the state machine model. The following code is a quick prototype I whipped up.

The program loop represents the state machine which is the program. The looping is done with the trampoline command, which allows for tail-recursion optimization. This should be an easily extensible model. If states need to pass parameters, the parameters should be returned by the action function (action functions such as displayMenu) and then enclosed in the returning function. The only thing I am not overly happy with is the potential size of the program loop, but this should be something I can improve upon.

Hello World

The first post of yet another blog.

My mid-year resolution is to actively post here, both about things of current interest and about coding. I have become too passive in my voice as a programmer. So bring on the programming and lets see what happens.