clojure

Using Clojure to Delete Directories Containing Symlinks

As part of repository maintenance, I often use a script to delete a group of directories, even if those directories are not empty. It looks something like this: (require '[clojure.java.io :as io]) . . . (defn delete-children-recursively! [f] "Given a file, delete it. If the file is a directory delete its contents first. This version does not handle symlinks." (when (.isDirectory f) (doseq [f2 (.listFiles f)] (delete-children-recursively!

Undo/Redo with Clojure using the Memento Pattern

Because of the ability to attach “watch” functions to Clojure atoms, it is very easy to create and use a simple undo/redo mechanism. The approach is to create a stack that holds every change to the state of the atom, as detected by a watch function. To undo something you just pop the last state off the stack. You can repeat it until you recover the original state of the atom as it existed when you attached the watch function.

clown -- A Clojure/Script Outliner with Notes

Questions about note-taking are very popular on Hacker News. I am always interested in the responses. It seems like developers are always interested in the best tools and methodologies. Me too. I’ve been researching and using different approaches for decades. Over all that time, I’ve written thousands of notes, both short and long. Most notes become uninteresting after awhile and are just erased. But thousands have persisted for many years because I still refer to them.

Letting the User Change the Sidebar Width in CWiki

Recently, I’ve been working on what I thought was a simple feature – changing the width of the sidebar in the CWiki wiki program. There were two ways the user could do it. First, they could set it manually in the “Preferences” page. Second, and more naturally in my opinion, they could use the mouse to drag the separator between the sidebar and the main article area to the location they wanted it.

Writing a flexmark Extension in Clojure

Writing wiki pages using Markdown is a great way to do things because it is simple, well-known, and capable. One of the annoyances of using Markdown is that it has no default syntax to create wikilinks, a type of hyperlink that links one page in a wiki to another. I’ve been working on a home-grown, personal wiki, CWiki, for almost a year now off and on. It’s written in the Clojure programming language, which is a pleasure to use.

Stuart Sierras Component System

Starting the implementation of user options (preferences) in CWiki (a personal wiki program) got me thinking about refactoring the project into a shape that is more compatible with Stuart Sierras reloaded workflow. Naturally, that led to thinking about his component architecture. Here are some more resources related to the component architecture. Stuart has a blog post about his reloaded workflow, linked above. The component code repository is on Github here.

CWiki-Next

For the past few months, I’ve been beating my head against a brick wall. The problem was that I was trying to get an all-server-side wiki built using Clojure. It actually works pretty well. I’ve been using it for personal information for months now. It works well except for one aspect, arguably the most important – editing new or existing content is not that pleasant. It’s all a variation of Markdown, which is nice.

Notes on ClojureScript Regular Expressions

There’s an old saying about regular expressions – paraphrasing: “If you try to solve a problem with regular expressions, you then have two problems.” Even though regular expressions are present in most programming languages, I have never become particularly proficient with them. Those that I am most familiar with are the Java version. Since ClojureScript compiles to JavaScript and runs without the Java underpinnings, it’s version of regular expressions are different.

Using Anonymous Functions in the Clojure/Script Thrush Macro

The other day, I was putting together a sequence of operations to transform one piece of text into another form of that same text. The functions took a text argument, and the result was a slightly tweaked version. Put all those functions together to get the fully transformed result. What could be more natural than to string those pieces of code together with one of Clojure’s threading macros: ‘->’ or ‘-».

Another Useful Function -- convert-seq-to-comma-separated-string

This is a little note about a function I find myself using frequently in Clojure. User interface code often needs to display a list of things as a comma-separated list, e.g. “a, b, c, d”. If all of the things are strings, you can use the built-in string/join function to build such a string. When you have a sequence of things that are not strings, I suppose you could convert each element to a string and then use string/join.

sorted-set-by

This is just a little note about one of my favorite datatypes in Clojure. Like most Lisps, Clojure has a very useful group of datatypes built in including some set types. I use sorted-set a lot. Until recently, I hadn’t noticed sorted-set-by, a function that returns a sorted set using a comparator you specify. I found myself needing to create a sorted set of strings where the sorting was case-insensitive. The sorted-set-by function was exactly what I needed.

A New Version of the Confidence Interval Program

Recently, I wrote about updating an old program that did the Sign Test. Well, I have lots of old programs that could stand a bit of refreshing. Another of the simple ones calculates the confidence interval around the proportion of successes in a series of Bernoulli trials. I wrote about it way back in 2011. The original was written in Java and Swing many years ago. It is still available in a repository on Bitbucket.

A Titled JavaFX Separator

In the process of updating some old programs, I had to change the GUI frameworks used. The old programs were written in Java using the Swing GUI framework and the JGoodies Forms and Looks libraries. Nowadays, the official GUI framework for Java is JavaFX. Making the transition from Swing to JavaFX was relatively painless because the programs were so small. However, one of the things I missed from the JGoodies Forms library was the “titled separator”, that is a separator with a label in front of it.

An Updated Sign Test Program

Long ago, I wrote a post about a small program to calculate the probabilities of a sign test. A lot has happened since then. The sign test is still useful to me on occasion, but the application framework used to write the original program is now unsupported. Too, the original program used Java’s Swing framework for the GUI. The new official GUI framework for Java is JavaFX. So I’ve updated the program a bit.

Leiningen passing Invalid Flags to Java Compiler

Just a note about some weirdness in my work process and it’s solution. A few weeks ago, I started noticing some weirdness in trying to use some tools with Leiningen while developing a program in Clojure. When running tools like kibit, lein would fail with an error from javac about an invalid flag. Initially these flags were for attempts to set the file encoding. And the file encoding kept changing.

Clojure/Script has Ruined Me for Other Languages

The Elm language is often cited as an up-and-comer for web front end development. I was attracted to it largely because of the compiler’s friendly and extremely helpful error messages. It’s really attractive in many ways. But when I started looking at examples, I often found myself thinking things like “Why is this so inconsistent?” or “Why is this syntax so complicated?”. And it finally occurred to me that I’ve been ruined by the way Clojure/ClojureScript/Lisp/Scheme do things.

Using Local Java JARS in Clojure Projects

Recently, I’ve been working on a Sudoku game program. Part of the program provides a user with the ability to generate new puzzles of a particular difficulty. Generating a puzzle usually requires two puzzle solvers: one that solves puzzles (slowly) like a human would, the other that solves puzzles (very quickly) like a computer would.

Rather than write my own from scratch, for this part of the development, I wanted to use an existing implementation of the machine-like solver. After a little research (more on this some other time), I found one I liked a lot – the Kudoku solver written in Java from attractive chaos.

But how does one use a local jar file in a Clojure Project? Read on…

Saving and Restoring Program Configuration across Sessions in Clojure

I like to use programs that can remember what I was doing the last time I was working with them. They should restore the window just as I had it, remember which file(s) I was working with, what preferences I had selected, and so on. Naturally, I want the programs I write to be just as considerate of the user.

For some time, I’ve been fretting over the best way to do this in a Clojure program. Should I provide wrappers around the Java Preferences API? Some other mechanism? Turns out I should just embrace simplicity.

JavaFX Still Not Ready?

Just a short rant about JavaFX because I’m pissed about it at the moment. I enjoy using it for the most part but it sometimes throws up surprising obstacles in otherwise routine work. The latest for me was an unexpected lack of a spinner control. There are alternatives in some open source projects, but, really? No spinners built in? This is almost as gob-smacking weird as the lack of dialogs. (Ok, there are some dialogs, like for opening/saving files, but not much in the way of user-programmable dialogs built in.

More on Loading Fonts

A couple days ago, I posted a little snippet showing how to load and font from a list of preferred fonts using Clojure and JavaFX. Well, I’ve extended the demo a bit to show how to load both fonts installed on the OS and fonts from a resource file. Here’s the new snippet. (ns clojure_font_loading.core (:gen-class :extends javafx.application.Application) (:import [javafx.application Application] [javafx.event EventHandler] [javafx.

Loading Fonts Like CSS

Just wanted to pass along a little snippet I have found myself using fairly frequently. CSS has the ability to specify the appropriate font to use in displaying a document. It handles the tag in such a way that it can gracefully degrade from a “preferred” font through a series of less ideal typefaces depending on what’s available on the machine doing the display. That’s a handy facility to have, even on Windows, which can have different fonts available depending on the version of Windows and what software has been installed.

Paths with Spaces, I Give Up

I’ve wanted to look into the Pedestal framework for creating web-based applications in Clojure. However, one of the requirements is Leiningen 2.2.0 or greater. And, as I’ve written before, version 2.2 will not install on my system because of spaces in the path of the user home directory. ("C:\Users\David Clark" on my system.) My user profile name is “david”. That’s what I use to sign on with. The fact that my home directory uses “David Clark” is an unfortunate result of how the computer was set up at the factory when I custom ordered it.

N-Queens

The N-Queens puzzle is a classic computer science problem. In fact, it’s much older than discipline of computer science. It is usually used as a problem to introduce students to backtracking algorithms in computer science. I was first introduced to the problem in Niklaus Wirth’s Algorithms + Data Structures = Programs back in the ’70s.

I thought it might be interesting to write an updated version in my continuing effort to become proficient in Clojure. My intent was to write a simple working version, then use the concurrency features of the language to write a parallel version and see what kind of performance gain was possible.

Java-Clojure Interop: An Update

My most popular answer on Stack Overflow has to do with Clojure-Java interop. Since that answer was written, some of the tools used in the answer, specifically enclojure(Broken Link), have been deprecated. Because many of the follow-up questions related to how to build a working version of the answer, I thought it might be a good idea to update the post with modern tools. As this is written, the tools used include:

Keyboard Shortcuts for JavaFX Buttons

Most programs written for graphical user interfaces still provide a way to operate with the keyboard, requiring minimal mouse usage. The thought is that expert users will want to speed through their work keeping their fingers on the keyboard rather than devote an entire hands worth of fingers to controlling the mouse. I’ve been learning JavaFX, the eventual replacement for the Swing UI framework on Java, and wanted to explore how shortcut functionality had changed.

JavaFX KeyCodeCombinations in Clojure

I’ve been experimenting with adding keyboard accelerators to some of the Clojure programs I’ve written with JavaFX-based user interfaces. As part of that investigation, I tried to translate the Java program here (Broken Link) to Clojure. The program just puts up a window with a menu bar containing only a “File” menu which itself contains one item, “Exit”. Most programs provide a keyboard shortcut or accelerator to close the program with a Ctrl-X (on Windows). Figuring out how to add that functionality was a bit of an issue for me.

The Clojure Development Toolchain

One of the things about Clojure that is difficult for beginners is the process of creating and running programs. I would argue that it is more difficult than learning the language itself. There is no “one-button” provisioning system that would set up some sort of canonical development environment. This long post will talk about setting up Leiningen and Emacs to make a comfortable environment for developing in Clojure.

Clojure, JavaFX and Tic-Tac-Toe

Recently, I have been experimenting with JavaFX in Clojure. Initially, in one of my experiments, I wanted to learn how to re-size a game-board interface as it’s containing window was re-sized. In the past I’ve had medical device interfaces that draw a representation of a physical device and these drawings must re-size as their window is re-sized. The initial experiment was with a simple interface for Tic-Tac-Toe. Since I had such a nice interface, I thought, why not program the complete game.

Re-sizing an Interface in JavaFX and Clojure

Since JavaFX is the future of the user interface for Java, I’ve started trying to learn it. Since I’m also learning Clojure, I’m doing the work in that language. One of the things I’ve been looking into is how the interface responds to resizing. If you have all of your controls in a nice layout, that is usually taken care of for you. But how do you handle things if the interface is not made up of standard components, something like a graphical game interface for example?

Favorite Programming Books

Every programmer seems to have their own list of favorite programming books. The lists are very personal and seem to be influenced by the age of the programmer, their training, and their field of endeavor. My own list follows.

The Sign Test

Sometimes weakness is a strength. That certainly seems to be the case for the lowly sign test. It is about the simplest statistical significance test imaginable. But if it tells you something is important, it probably is. Usually when you hear people talk about the “power” of a statistical test, they are referring to the ability of the test to detect a significant difference when one exists. For example, Student’s t test is a favorite and very powerful test for differences in means when you have data meeting the underlying assumptions of the test.

Spare Time Projects

It’s pretty common to see discussions about how to determine if a candidate for a programming job has a “passion” for programming and software. One of the usual pieces of advice is to ask about the projects someone does in their “spare time.”

Getting Started with Lisp/Scheme/Clojure

Ya know, this point just keeps slapping me in the face. It seems that people don’t stop trying to use Lisp because they don’t like the language. A lot of people stop because they don’t like the programming environment. Looking around the Q&A sites there seem to be many more questions about setting up a programming environment for the Lisp family of languages than there are for the more mainstream languages like Java and C++.

Clojure and Java Interaction

One of my most-upvoted answers on Stackoverflow is a simple example of how to call Clojure functions from Java. It doesn’t require calling through the Clojure run-time as so many responses do. But there is more to writing programs than calling static functions, as in my answer. You also might need to call methods of objects and on objects across the Clojure/Java divide.

A Closure in Clojure

Back when closures were first explained to me, a long time ago, I thought “sounds like a language with pass-by-reference semantics like Pascal.” Of course, it isn’t quite that simple.

Clojure has a lot of nice features that work naturally to give you a “better Java than Java”. Here’s an example of using a closure that is not at all easy in Java.

Getting enclojure 1.4 to work

I’ve used enclojure (Broken Link) for a long time (in internet years). It has always seemed a bit finicky. However, with the 1.4 release and the switch to using Maven as the build tool, things stopped working. Projects that had worked fine before no longer compiled or executed. The “Getting Started” section of the enclojure web page appears to be hopelessly out of date and actually misleading. Here’s what I had to do.