Clojure

Clojure (pronounced [cloushur]), is a dialect of the Lisp programming language . It is a general-purpose language that offers an interactive programming style and encourages a functional way of programming, thus simplifying concurrent programming . Clojure can run on both the Java virtual machine (JVM) and the .Net platform virtual machine, and can even be compiled to Javascript .

Summary

[ hide ]

  • 1 Principles
  • 2 Syntax
  • 3 Code examples
  • 4 Features of Clojure
  • 5 Advantages of Clojure over other languages
  • 6 Sources

Beginning

Rich Hickey created the Clojure language in 2007. He describes the development of this language as the search for a language that he could not find: a functional lisp by default, built on a robust environment instead of being his own platform, and with the concurrent programming in mind.

Likewise, object orientation is rejected in principle, offering an approach in which programs are expressed as the application of functions on data, rather than as the interaction between mutable entities that mix data representation, and behavior. Furthermore, features such as instantiation, polymorphism and interfaces are effectively part of the language.

Syntax

The Clojure syntax is based on S-expressions (list-based data structures) that are parsed and then compiled. Supports literal syntax for lists (), vectors [], maps {}, and sets # {}. It is a Lisp-1 it supports using functions in variables like a normal function but it is not compatible in code with other Lisp dialects.

Code examples

  • Square function

(defn square [x] (* xx)).

  • Recursive factorial

(defn fact [n] (if (> n 1). (* n (fact (dec n))). 1)).

  • Factorial with loop (False recursive).

(defn fact [n] (loop [current 2 accum 1) (if (<current n). (recur (inc current) (* accum current)). (* accum current)))).

  • Factorial with reduce

(defn fact [n] (reduce * (range 2 n))).

  • Java interoperability

(javax.swing.JOptionPane / showMessageDialog nil “Hello World”).

  • A unique and consecutive number generator that supports concurrent calls

(let [i (atom 0)] (defn generate-unique-id “Returns a different numeric identifier for each call.” [] (swap! i inc))).

Clojure Features

  • Simplicity: In Clojure there is a set of basic components (data types, control structures, functions offered by the language) appropriate for a language that supports the functional programming paradigm. As Clojure is a Lisp dialect it is a bit simpler than this and offers a slightly more concise syntax.
  • Orthogonality: Clojure is a language that has a high orthogonality due to the fact that with the set of primitive constructors, they can be combined in a large number of ways and thus build more complex control and data structures. The orthogonality handled by this language positively affects readability due to its degree of regularity.
  • Data types: In the functional paradigm, programs manipulate data. Basically, Clojure supports numbers, strings, characters, keywords, symbols, and collections. In addition to these collections, the language also offers the ability to import collections and classes from Java .
  • Support for abstraction: Clojure through collections allows you to create new data types with which you can better model the reality that you are trying to reflect in your code.
  • Expressivity: The expressiveness of the language is extremely high because there are very powerful operators that allow it to achieve a lot of calculation with few lines of code. This is attributed to the naturalness with which this functional paradigm is expressed.
  • Type check: During the phase in which the Reader reads the source code to translate it into the data structures, a type check is made so that the data types match those expected by the functions and expressions to evaluate. Invalid expressions generate a “read error,” which displays an appropriate error message and aborts the program. In the second phase, expressions and their return values ​​are evaluated. The code is compiled to bytecode, and runs on top of the JVM (Java Virtual Machine). This point positively affects the reliability of the language, since the type check carried out by the language is rigorous.
  • Exception handling: In Clojure exception handling is very similar to Java, but is much more simplified. It provides mechanisms that make it less necessary to explicitly catch exceptions that are generated at runtime, unless it is extremely necessary. No need to catch exceptions explicitly in Clojure You can use with-open macros that offer similar functionality to finally in Java, which guarantee that after using a resource, even if an exception is generated, the resource is deleted or closed (internally, the macro generates a try structure and takes care of it if necessary). This language feature positively affects its Reliability, since that the programmer does not have to be so careful to explicitly attend to the handling of exceptions and errors that may occur in the execution.
  • Concurrence: As Clojure techniques for concurrency support you can include agents, who perform state management and execution management actions. Since variables are treated as any language would treat a constant, that is, they remain immutable, this is where state management comes into play since we could model little if we could not modify the variables. In the handling of states what the agent does is, since all the elements internally treat them as sequences, if we need to delete or insert data in a variable, it creates a new variable and binds or deletes it temporarily. In execution handling, agents are the ones who execute, pause, or kill applets, and their purpose is obviously

Advantages of Clojure over other languages

  • The code in clojure is 1 to 10 orders of magnitude smaller than in Java, that is, 10 to 100 times less code.
  • Recursion WELL implemented.
  • It has a REPL (read eval print loop), dynamic development with an evaluation console.
  • Functions are first-class objects.
  • Lazy lists (you can have ALL natural numbers programmed without any memory problem).
  • Wide set of data structures, vectors, maps, lists, just to mention a few.
  • Produces JVMcode .
  • You can use JavaClasses to work with Clojure.
  • As a different feature from the rest of Lisps, it implements multithreading, allowing POOstyle polymorphism .

 

by Abdullah Sam
I’m a teacher, researcher and writer. I write about study subjects to improve the learning of college and university students. I write top Quality study notes Mostly, Tech, Games, Education, And Solutions/Tips and Tricks. I am a person who helps students to acquire knowledge, competence or virtue.

Leave a Comment