Category: Clojure

  • Installing Clojure with Lein

    Most of the Clojure tutorials and Clojure examples on this site require at the minimum Clojure to be installed. Usually, you want Leiningen installed too.

    Easy peasy.

    Seriously, do it once and ignore it for the rest of your learning experience. Here are the steps for getting Lein and Clojure up and running.

    (more…)

  • Hello Clojure

    What is Clojure?

    Clojure is a modern programming language. Depending on your skill level, you can create any type of software with Clojure. This includes games, web apps, mobile apps, and desktop applications.

    The first step in programming Clojure is to install it and other things needed for creating Clojure programs.

    (more…)

  • Clojure Swing JFrame Introduction

    (See my full list of Clojure Swing examples and tutorials at Clojure Swing Interop)

    JFrame s are the root windows of all modern Clojure Swing applications. In the old days, there were other options, but polite society does not speak of them.

    The visible components that you use in your JFrame  are added to the content pane of the JFrame . You request the content pane and then add your components to that content frame.

    Clojure JFrame Example

    Before adding components to a content pane, you need to decide one which of several layouts you will use for arranging your components. BorderLayout  is the default, so we’ll stick with that one for this simple example.

    Border Layouts are divided into NORTH , SOUTH , EAST , WEST , and CENTER . We are going to use one label for each of the five locations in the content pane’s BorderLayout . We center the text and change the colors as needed so it is easy to see the locations of each label in the BorderLayout .

    We set the size of the JFrame  to 600  pixels wide by 400  pixels tall, then set the JFrame  to visible equals true .

    Note that we do all our layout in the event dispatch thread by using invokeLater .

  • Clojure ActionListener Interop

    (See my full list of Clojure Swing examples and tutorials at Clojure Swing Interop)

    When working with UI elements in Clojure Swing, your “go to” event handler is the ActionListener  found in the java.awt.event  package. This is a one-size-fits-all handler that works for many general situations where you just need to know **something** happened.

    The fist hurdle to overcome is what Clojure technique should you use to implement the ActionListener  interface? The correct answer is reify .

    Reify gives you an anonymous implementation of an interface. It is perfect for implementing Java event interfaces that are rarely named once created. Reify takes the interface name followed by method implementations as it’s arguments. The one trick that may throw off seasoned Java programmers implementing methods in Clojure’s reify, is that the first argument to functions used to implement methods is always an explicit this . this  is usually hidden in Java implementations of methods. It’s there, just not visible.

    For example, here is reify implementing an ActionListener for a typical JButton  called my-button .

    Here’s an example of a JFrame containing a JButton that prints “Clicked” to the console when clicked.

    JFrame with JButton running in Clojure

    Here is the full source code.

     

  • Clojure Example Swing App

    (See my full list of Clojure Swing examples and tutorials at Clojure Swing Interop)

    Here is a full app demonstrating use of Swing from Clojure. Toy apps usually gloss over import code. For example, real Swing requires proper use of the event thread, and combining multiple layout types to get more complex behavior.

    An example app using Clojure Swing to convert temperatures.

    Here’s the full source.

    Note that #(<some UI code>)  is used to pass in a function to the event dispatch thread, otherwise you have null pointer exceptions due to the code being executed BEFORE it is passed to the event dispatch thread.

  • Clojure JOptionPane

    (See my full list of Clojure Swing examples and tutorials at Clojure Swing Interop)

    Every complex GUI uses message dialogs. When using Clojure Swing interop, you will use the JOptionPane  for your message dialogs. Two keys to remember when using JOptionPane s, is that they should always be called from within the Event Dispatch Thread (EDT). Sometimes the JOptionPane  is shown by an ActionListener  that is already on the EDT, and sometimes you need to call invokeAndWait , because your current thread is not the EDT.

    The other key point to remember with JOptionPane s is that you can associate the message dialog with a frame, or set the frame to nil . If a JOptionPane ‘s frame is set to nil , then the message dialog will center itself on the screen when it displays. If a JFrame  is provided to the JOptionPane , then the message dialog will center itself on the JFrame  it is associated with.

    Here is a basic example that provides a button to click, and that displays a plain JOptionPane  with a message. The ActionListener  is called from the EDT, so the JOptionPane  is being called from the EDT.

    Here is an example that shows a JOptionPane  that displays from a normal thread. The EDT must be explicitly called with invokeAndWait . The four most common types of message dialogs are shown.

  • File Dialogs in Clojure

    (See my full list of Clojure Swing examples and tutorials at Clojure Swing Interop)

    When dealing with files, its very handy to use file dialogs. Here’s a quick example of using a JFileChooser  to select a file to slurp  and print.

    Note the use of  (into-array [“txt”]) as an argument to the FileNameExtensionFilter constructor. This is because the file extensions are provided as a String...  meaning a java.lang.String  array.

    Also note, the .showOpenDialog  call uses nil  instead of a JFrame . We do that because we don’t have a JFrame  showing that we want to associate the file dialog with.

     

  • Clojurcises: hash-map

    Good practice. Use each of the following functions and macros to create or manipulate hash-maps in Clojure.

    {}
    hash-map

    get
    get-in (one keyword)
    get-in (<map> [<key1> <key2>])

    (<map> <key>)
    (:<keyword> <map>)
    (:<kw1> (:<kw2> <map>))

    (-> <map> :<k1>)
    (-> <map> :<k1> :<k2>)

    assoc (change value)
    assoc-in

    assoc (add new key-value pair)
    assoc-in

    update (function only)
    update (function with args)

    update-in (function only)
    update-in (function with args)

  • Learn Clojure Quick

    If you need to understand basics of Clojure quickly …

    Watch these videos

    Clojure for Java Programmers part 1
    https://youtu.be/P76Vbsk_3J0?si=oWDHQwjHWt8qvbpT

    Clojure for Java Programmers part 2
    https://youtu.be/hb3rurFxrZ8?si=sUsf_67h3EqC_e-I

    Simple Made Easy
    https://youtu.be/LKtk3HCgTa8?si=9K4sZvThdJKJQhad

    Clojure Concurrency
    https://youtu.be/dGVqrGmwOAw?si=Twr5pfeJtgMH-FM4

     

    Use these reference sites

    Official Clojure Home Page
    https://clojure.org/

    ClojureDocs
    https://clojuredocs.org/

    Leiningen
    https://leiningen.org/

    Clojure Web Development
    https://luminusweb.com/

    Choose code puzzles to practice with from the following sites

    Project Euler
    https://projecteuler.net/

    Code Golf
    https://codegolf.stackexchange.com/

    Advent of Code
    https://adventofcode.com/2023/events

  • Clojure Selmer Basics

    Eventually I’ll finish this, … maybe.

    Want to make advanced web pages? Try Selmer. Selmer is a powerful templating system that saves time and money during complex website development.

    Selmer is a Clojure based templating system that not too surprisingly works great with Luminus framework. We’ll use Luminus to host our Selmer examples, because it is easy to get running fast.

    Prerequisite: Leiningen

    Begin with the command …

    Now you have a fully functional server with Selmer templates preconfigured. (Told you it was easy.)

    cd  into your project root

    Run your new server.

    In a web browser visit http://localhost:3000/ You should see something similar to the following.

    Default Selmer Template
    The default Selmer template included in a Luminus project.

    The web page you are looking at is a Selmer template.

    Explaining Selmer

    Selmer is a templating language for creating web pages. Selmer integrates with Clojure. Clojure is one of the most concise and powerful general-purpose computer programming languages available. How concise is Clojure? There are many examples of Clojure rewriting common Java and Python functions of 10-20 lines as one line of Clojure code. Selmer templates get to take advantage of Clojure when creating web pages.

    For those new to templated web pages, …

    The very simplest web pages are written in HTML. If all you need to do is present a restaurant menu, a short list of services, or a description of  some research, simple HTML may be enough for you. More advanced and complex web pages often have information that changes based off external factors. For example, if a user of your website needs to log in to see information, a Selmer template can change the information on the page to match the information that user should see. Or if users visiting the page from a different country should see a custom price list, Selmer templates can help with displaying custom information based off regions of the visitors. Selmer templates can also leverage session information to help users of your website navigate more easily to find the information they need.

    From a technical view, Selmer templates create enough of a Separation Of Concerns (SoC) that you can hire less expensive junior developers to layout your website and have more expensive senior developers handle the complex server and database code. Using Selmer templates saves your business money, because senior developers are not entangled in tasks that junior developers can handle.

    To be fair, there are tons of templating systems associated with various languages and servers. All have similar benefits, except very few exploit the power of Clojure.

    Taking the Guided Tour

    Above, we created a web server in a Luminus Clojure project with Selmer templates. Let’s have a look around the Selmer related bits-and-pieces of our project.

    I suggest opening the mycoolapp  project in Visual Code or your favorite code editor.

    A Little Code

    Let’s add a Selmer template just to get started.