A common way to develop code (don’t blame the messenger) is to start the application watch for the logged errors, make changes and then restart the application again, looking for more logged errors.
That’s not how it’s done in the world of Clojure, not just because it’s bad, but because the process of starting any Clojure program is horribly slow. Instead, in Clojure normally you start the application and then connect with a terminal window running a Clojure REPL. For example, if you are running a Luminus server, when you start it, it will tell you what port the server is listening for REPL connections on.
In clojure terms, destructuring is taking apart a sequence and putting the pieces you care about into variables you can more easily work with. Destructuring involves square brackets that make it look like a vector, but it is really binding a variable from a given seq to a new variable. Let’s look at an example involving a function in Clojure.
Notice the -main function has three variables in a vector: a site address, a web page, and a user name. It is much easier in the new function to unpackage these variables form the vector and bind them to variables that have meaningful names, and then use the names.
The result is as follows.
1
2
3
site=>example.com
page=>clojure.html
user=>clojure dude
What if we have more values in the seq than values we intend to bind to? For example, below we added an additional index with the words “random stuff”.
For a brief intro to setting up Redis with Clojure using Carmine, see my earlier post on setting up Redis with Clojure.
The most common killer use for Redis is as a key-value store. You simply get and set values in it like in a map. It’s very handy for site wide session storage with web apps, or even in more traditional applications.
If you’ve ever done enterprise web application development, you’ve heard of Redis. Redis has a lot of uses, but the most common is as replacement for your standard session. Even just using it’s key-value store (think hashmap on steroids), Redis provides so much power that many enterprise sites integrate it into their larger projects.
Lucky for those of us using Clojure, Redis integrates nicely using Carmine, an open source Clojure library for interacting with Redis.
When developing Swing GUIs for Clojure, understanding and using
SwingUtilities/invokeLater is crucial.
Event Dispatch Thread
Don’t confuse “thread” in this case with Clojure threading macros such as
-> and
—> . Threading here refers to concurrency.
Swing GUI components are not safe to access concurrently. If two threads modify the same Swing component at the same time, the result is unpredictable. Java solved this problem by introducing the event dispatch thread. All modification Swing displays and component values should happen on the event dispatch thread.
There is only one event dispatch thread. You access it by creating a Java Runnable and passing it to
SwingUtilities/invokeLater . Lucky for us, all Clojure functions are Java Runnables. Here’s the general format.
Clojure
1
2
3
4
5
6
(defnmy-gui-modifier[]
;; Do some Swing changes here
)
(defnmodit[]
(SwingUtilities/invokeLatermy-gui-modifier))
Warning
Remember, ALL the screen rendering and modifications to your Swing components happen on the event dispatch thread. That means, if you tie up the event dispatch thread doing something time consuming, your app will appear to hang. This creates a horrible user experience. Any runnables you create should complete quickly.
On a somewhat related note, any time a Swing component alerts you to an event or a value change, you will want to hand back that thread immediately and use a new thread to do any long tasks associated with the notifications.
Sometimes you want a pure Clojure server with a React web client. It is best if you create a new one instead of using other people’s templates, because you can also get the newest libraries, frameworks, and security fixes at the same time. If you are determined to get going now with a Clojure / React JS server, grab the latest copy from https://github.com/TGeneDavis/clojure-server-with-reactjs-template on GitHub. (more…)
These days, many apps default to use of a web interface for their user interfaces. Sometimes you just want a traditional graphic user interface (GUI) with your Clojure code without the hassle of creating a full blown web app. A lot can be said for double clicking an uberjar and seeing an appealing desktop app spring to life.
Those developing on vanilla Clojure running on the JVM have three main desktop options for GUI development: AWT, Swing, and JavaFX. AWT is considered to be an old tech with too many issues for standard development. JavaFX on the other hand is wonderful for huge projects managed by teams of dedicated developers, but a lot of work for little return when used on small projects. Swing still stands strong as the best choice for small projects that need a mature tool for GUI work.
Here are my collection of posts explaining Java Swing interop with Clojure.
Sometimes, you just need an actual code sample in a real app to see how to put together your own apps correctly. This Clojure Swing app demonstrated
JLabel s,
JTextField s, the event dispatch thread,
ActionListener s,
JFrame s and
FlowLayout s.
Here’s how you integrate
JOptionPane s into your Clojure program. You can have message dialogs, even if you don’t want a windowed application with a
JFrame .
Redis works great with Clojure when you use the Carmine API.
Some of the features of Redis could be duplicated in your app, or are features that exist in the Clojure APIs. So, knowing when to use Clojure and when to use Redis is important. Use Redis when there is a chance that multiple servers may need access to the same messaging, session, or cached data. Also, use Redis anywhere you need an extremely fast-and-simple drop-in replacement for a database.
Clojure Redis tutorials
Note: These tutorials are to help get you started, but not considered final production grade code. Redis, like any database, should NEVER be exposed directly to Internet traffic.
Redis Pub/Sub for messaging between servers or namespaces. Helps prevent circle reference issues, and communications issues between namespaces and servers.
My 2011 Tsume Shogi book. It’s proven very popular.
Most of my books are computer programming books, but I really like my Tsume Puzzles book. I am thinking of doing another one eventually.
Here’s the Amazon book blurb.
Centuries before sudoku, crossword puzzles, or word searches challenged Western minds, Japanese military and royalty were creating and enjoying tsume puzzles. Tsume puzzles are mating puzzles for shogi (Japanese chess.) Shogi’s history dates back 1000 years with archeological evidence that shogi existed in Japan at least by the 11th century. Some of the greatest know tsume puzzle books date back several centuries. Tsume puzzles have a long and honored tradition. Tsume are widely recognized to improve shogi skills. Tsume are also fun puzzles that anyone can enjoy. Whether your interest is in improving your shogi game, or just solving puzzles, this book provides many hours of entertaining shogi riddles. In this book you will find: * A complete introduction with rules of tsume puzzles and shogi rules that apply * Over 200 tsume puzzles of varying difficulty * Puzzles ranging from one move to thirteen moves * Solutions * Explanatory notes to many solutions * A quick reference section for those new to tsume and shogi