Skip to content

Getting Started with Clojure CLI and tools.deps #39

Open
@practicalli-johnny

Description

@practicalli-johnny
  • Clojure CLI Getting Started - Video Script

Welcome to Practicalli, thank you for joining me.

I'm Johnny and I'll introduce you to the Clojure CLI,
which provides a simple way of running Clojure projects and of course the Clojure REPL.

Check out this video if you haven't installed the Clojure CLI and the Practicalli Clojure CLI config.

The clojure command can run a single express, a REPL, a tool or your Clojure project.

Evaluate a Clojure expression

The clojure command can evaluate a single Clojure expression,
an expression being any single piece of syntactically correct Clojure code.

clojure -M -e "(+ 1 2 3)"

This command prints out the value returned from evaluating the expression.

The -M flag uses clojure.main approach to running code, using strings as argument.

The -e flag will run the next value as a Clojure expression and return the result.

NOTE: using -e is very limited, so lets have an interactive session.

Run a Clojure REPL prompt

The REPL is your live Clojure environment,
where you can evaluate (compile & run) code as you write it and get immediate feedback.

There is no external compilation step, it all happens under the covers as soon as you evaluate the expression.

REPL is an ...

  • R is for Read, to read and expression
  • E is for Evalutate, to compile and run
  • P is for Print, to show the result
  • L is for Loop, to show the prompt again, ready to read more clojure code

Basic REPL prompt

Typing clojure into a command line terminal starts a Clojure REPL.

There is also a clj script which starts a basic readline using the rlwrap command and then starts the REPL.

The readline provides a history of expressions which can be navigated using the up and down arrows.

The clj script will fail if rlwrap is not installed.

Rebel - a rich Clojure REPL

Rebel is a more advanced readline tool which I highly recommend.

Start Rebel using the :repl/rebel alias from Practicalli Clojure CLI Config

clojure -M:repl/rebel

Rebel provides some very nice features (demo when describing them)

  • TAB completion of functions and symbol names (var's)
  • function signature & argument documentation inline
  • Inline evaluation, allowing you to evaluate code without pressing enter
  • Quick access to documentation, source, and apropos for the symbol under the cursor
  • multi-line editing & auto-indentation

NOTE: use clojure -X:deps aliases to list all available aliases from the current project and user configuration for Clojure CLI.

Simple coding

Writing code is possible in the REPL, although its a lot less efficient than using an editor connected to the REPL.

The REPL can be useful for writing simple code or quick experiments.

I also use the REPL prompt for longer running code, such as web servers serving up an API.

TODO: Code a quick random function generator in the REPL

Connecting an editor

Clojure editors typically have a 'jack-in' command, which starts the Clojure REPL and a Network REPL (nREPL) server.

The editor sends requests to evaluate one or more expressions,
from the source code file opened in the editor.

An nREPL server was started when using the :repl/rebel alias,
so we can connect our editor

TODO: Example with Neovim (Astro)
TODO: Example with Emacs (Spacemacs)


WIP

[run clj in a terminal]

** Extending Clojure CLI tool with Aliases
Review the ~/.config/clojure/deps.edn file from practicalli and discuss aliases.

clojure and clj commands can use aliase with the -A option, for example clojure -A:new uses the alias called :new, which in this case creates a project.

To create a Clojure project that we can run as an application, use the app template.
clojure -A:new app domain/main-namespace

The domain is the top level grouping for your project, like google, cognitect, clojure.org. It could be the company you work at, the business area, application suite the project is part of or your GitHub account or organisation name for a personal or open source project.

The main-namespace is usually the name of the specific application or service you are building.

This creates a project with a standard structure

[tree of project]

Other templates can be used such as lib when building a library of functions. There are many other templates defined by the community [provide a link / create a website with a curated list of templates]

** Rebel Readline Alias
Rebel Readline provides function signature information, documentation, editing styles and tools to manage the REPL, including a quit function.

Using an alias, the rebel readline library is added as a dependency and its main namespace specified so it will run when calling the clojure command.

Including an alias for rebel readline in $HOME/.clojure/deps.edn makes it available to all projects.

There are many useful aliases defined in the Practicalli deps.edn configuration.

Clojure Projects

You can run an existing Clojure project using the -m option. The -m option sets the main namespace,
the entry point into your application

The project does need to have a -main or main function defined and have (:gen-class) in the namespace.

clojure -m simple.core

This is a very simple project and prints out "Hello World!"

** In summary
This has covered the basics of evaluating Clojure code, running a REPL and working with projects so you have a good foundation on which to learn how to write Clojure code.

Thank you for listening and see you in the next video.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions