DEV Community

ivan.gavlik
ivan.gavlik

Posted on

Introduction to Clojure

Clojure in one sentence

Clojure is a dynamic, functional programming language that runs on the Java Virtual Machine (JVM).

  • Functional Programming Language

In Clojure, functions are first-class citizens, allowing them to be passed as arguments, returned from other functions, and assigned to variables. This facilitates a declarative coding style, emphasizing the "what" over the "how."

  • Hosted Language on the JVM

Clojure code is compiled to java bytecode and then run on JVM.
This means that developers can use existing Java libraries and frameworks when working with Clojure.

Leiningen: Build Automation Tool

Leiningen is a build automation and dependency management tool for Clojure projects, similar to Maven or Gradle in the Java ecosystem. It simplifies project setup, compilation, testing, and packaging.

Creating a "Hello World" Project with Leiningen

  1. Install Leiningen: Follow the installation instructions on the official Leiningen website.

  2. Create a New Project: Open a terminal and run: lein new app hello-world

  3. Navigate to the Project Directory: cd hello-world
    and Run the Project: lein run

This command compiles and executes the project, displaying the output.

Project Directory Structure

A typical Leiningen project has the following structure:

hello-world/
├── src/
│ └── hello_world/
│ └── core.clj
├── test/
│ └── hello_world/
│ └── core_test.clj
├── resources/
├── target/
├── project.clj

src/: Contains the source code.
test/: Contains test files.
resources/: Stores non-code resources.
target/: Output directory for compiled files.
project.clj: Configuration file for the project, specifying dependencies and build settings.

REPL: Read-Eval-Print Loop

The REPL is an interactive programming environment that allows developers to execute Clojure code in real time.

Key Features of the REPL

  • Interactive Coding: Developers can type in code and see results immediately, enabling quick experimentation.

  • Dynamic Evaluation: Modify, test, and reload parts of your program without restartingwich the application.

  • Error Handling: Errors are displayed immediately, which makes debugging more efficient.

  • Exploratory Programming: The REPL encourages exploring ideas and libraries in real time

To start a REPL session, navigate to your project directory and run:

lein repl

This command launches the REPL, allowing you to interact with your code in real-time.

Basics of Clojure Syntax

The base of syntax is expression/form which format is

(operator operand1 operand2 ...)

which is evaluated to produce a value. For example, to add two numbers:

(+ 2 3)
Enter fullscreen mode Exit fullscreen mode

Defining Values

Use def to bind a value to a name:

(def pi 3.14159)
Enter fullscreen mode Exit fullscreen mode

Functions

Functions are the primary building blocks in Clojure. They are defined using the defn:

(defn greet [name]
  (str "Hello, " name))
Enter fullscreen mode Exit fullscreen mode

This defines a function greet that takes a parameter name and returns a greeting string

Data Structures

Clojure provides several immutable data structures:

  • Numbers: Integers and floating-point numbers. e.g., 1, 1.1
  • Strings: Sequences of characters. e.g., (str "Hello")
  • Lists: Ordered collections, e.g., (1 2 3)
  • Vectors: Indexed collections, e.g.,[1 2 3]
  • Maps: Key-value pairs, e.g., {:a 1 :b 2}
  • Sets: Collections of unique values, e.g., #{1 2 3}

Conditional Statements

The if form is used for conditional branching:

(if condition
  then-expression
  else-expression)
Enter fullscreen mode Exit fullscreen mode

Example

(defn is-small? [number]
  (if (< number 100) "yes" "no"))

user=> (is-small? 50)
"yesple 
Enter fullscreen mode Exit fullscreen mode

Example program

Generates a personalized greeting message.

(ns example-app.core)

(defn greet [name]
  "Generates a greeting message."
  (str "Hello, " name "! Welcome to Clojure."))

(defn -main [& args]
  "Main function of the application."
  (let [name (if (empty? args)
               "Guest"
               (first args))]
    (println (greet name))))
Enter fullscreen mode Exit fullscreen mode

Top comments (0)