Computer Science

Clojure language

Clojure is a modern, dynamic, functional programming language that runs on the Java Virtual Machine (JVM). It is designed to be simple, concise, and expressive, with a focus on immutability and concurrency. Clojure is known for its powerful data structures, seamless Java interoperability, and functional programming features.

Written by Perlego with AI-assistance

7 Key excerpts on "Clojure language"

  • Programming Language Explorations
    • Ray Toal, Rachel Rivera, Alexander Schneider, Eileen Choe(Authors)
    • 2016(Publication Date)
    CHAPTER 8
    Clojure
    Clojure is a “dynamic … general-purpose language, combining the approachability and interactive development of a scripting language with an efficient and robust infrastructure for multithreaded programming.” [56 ]
    First appeared 2007
    Creator Rich Hickey
    Notable versions 1.0 (2009) • 1.7 (2015)
    Recognized for Macros, Lisp-ness, Concurrency support, Java interoperability
    Notable uses Data mining, Artificial intelligence
    Tags Functional, Dynamic, Concurrent, Homoiconic
    Six words or less A Lisp for the JVM
    In the 1950s John McCarthy created Lisp. In doing so, he was said to have “[done] for computing something like what Euclid [had done] for geometry,” [46 ] written the “Maxwell’s Equations of software,” [34 ] and given the world “the greatest single programming language ever designed.” [113 ] Lisp, remarked Edsger Dijkstra, has “assisted a number of our most gifted fellow humans in thinking previously impossible thoughts.” [26 ]
    This chapter introduces Clojure. Clojure is a Lisp—a modern Lisp. It has a richer set of data structures than the original Lisp. It does a better job than most (if not all) other Lisp dialects in isolating side effects and implementing concurrent features. Clojure interoperates with Java, so your Clojure program has access to the entire Java platform. Your Clojure code can call Java libraries. Your Java code can execute Clojure-written code.
    After presenting our usual three introductory scripts, we will cover the basic data types of Clojure (there are quite a few of these) and devote some time to persistent data structures, and the use of transients
  • Introduction to JVM Languages

    Clojure

    Clojure is a language that is rather different from the other languages covered in this book. It is a language largely inspired by the Lisp programming language, which originally dates back to the late 1950s. Lisp stayed relevant by being up to date with both technology and times. Common Lisp and Scheme are arguably the two most popular Lisp dialects in use today. Clojure is a dialect of Lisp, but its design is influenced by both.
    Unlike Java and Scala, Clojure is a dynamic language. Variables do not have fixed types and when compiling, no type checking is performed by the compiler. When a variable is passed to a function that is not compatible with the code in the function, an exception will be thrown at runtime. What's also noteworthy is that Clojure is not an object-orientated language (OOP), unlike all the other languages in this book. Clojure still offers interoperability with Java and JVM, as it can create instances of objects. And, as we will soon see, it can also generate classes to let other Java-compatible languages on the JVM run the bytecode compiled by Clojure.
    We will cover the following topics here:
    • Installing Clojure
    • Clojure's interactive shell - Read-Eval-Print-Loop (REPL)
    • Clojure language basics
    • Working with classes
    • Clojure agent system
    Passage contains an image

    Installing Clojure

    From the official project's website (https://clojure.org/ ), download the latest version:
    It is recommended that you have the documentation available while trying the examples in this book. Both the online official documentation and the community-driven documentation sites are recommended:
    • http://clojure.org/reference/
    • http://clojure-doc.org
    At the time of writing, the latest version is Clojure 1.8.0. Extract the archive and write down the full path to the directory where you've extracted the archive; you'll need it to create a start script.
    To validate the installation, start the interactive shell. In the Command Prompt (Windows) or Terminal (macOS and Linux), replace the directory with Clojure's main directory and type the following command (replace the version number with your installed version):
  • The Joy of Clojure, Second Edition
    expressive. It packs a punch, allowing you to perform highly complicated tasks succinctly without sacrificing comprehensibility.
    One key to delivering this freedom is a commitment to dynamic systems. Almost everything defined in a Clojure program can be redefined, even while the program is running: functions, multimethods, types, type hierarchies, and even Java method implementations. Although redefining things on the fly might be scary on a production system, it opens a world of amazing possibilities in how you think about writing programs. It allows for more experimentation and exploration of unfamiliar APIs, and it adds an element of fun that can sometimes be impeded by more static languages and long compilation cycles.
    But Clojure’s not just about having fun. The fun is a byproduct of giving programmers the power to be more productive than they ever thought imaginable.
    1.1.3. Empowerment
    Some programming languages have been created primarily to demonstrate a particular nugget of academia or to explore certain theories of computation. Clojure is not one of these. Rich Hickey has said on numerous occasions that Clojure has value to the degree that it lets you build interesting and useful applications.
    To serve this goal, Clojure strives to be practical—a tool for getting the job done. If a decision about some design point in Clojure had to weigh the trade-offs between the practical solution and a clever, fancy, or theoretically pure solution, usually the practical solution won out. Clojure could try to shield you from Java by inserting a comprehensive API between the programmer and the libraries, but this could make the use of third-party Java libraries clumsier. So Clojure went the other way: direct, wrapper-free, compiles-to-the-same-bytecode access to Java classes and methods. Clojure strings are Java strings, and ClojureScript strings are JavaScript strings; Clojure and ClojureScript function calls are native method calls—it’s simple, direct, and practical.
  • Clojure for Java Developers
    We will get to know Clojure by comparing each feature to what you already know from Java. You will see that there are lists, maps and sets just like in Java, but they are immutable. To work with these kinds of collections, you need a different approach; a different paradigm.
    This is what we will try to accomplish in this book, to give you a different way to approach problems. We hope you end up using Clojure in your every day life, but if you don't, we hope you use a new approach toward problem solving.
    In this chapter, we will cover the following topics:
    • Getting to know Clojure
    • Installing Leiningen
    • Using a Read Eval Print Loop (REPL )
    • Installing and using Cursive Clojure
    • Clojure's simple syntax
    • Clojure's data types and their relationship to the JVM's data types
    • Special syntax for functions

    Getting to know Clojure

    Before getting started with Clojure, you should know some of its features and what it shares with Java.
    Clojure is a programming language that inherits a lot of characteristics from Lisp. You might think of Lisp as that weird programming language with all the parentheses. You need to keep in mind that Clojure chooses to embrace functional programming. This makes it very different from current mainstream programming languages. You will get to know about immutable data structures and how to write programs without changing variable values.
    You will also find that Clojure is a dynamic programming language, which makes it a little easier and faster to write programs than using statically typed languages. There is also the concept of using a REPL, a tool that allows you to connect to a program running environment and change code dynamically. It is a very powerful tool.
    At last, you will find out that you can convert Clojure to anything you like. You can create or use a statically typed system and bend the language to become what you like. A good example of this is the core.typed
  • Mastering Concurrency Programming with Java 9 - Second Edition
    • Javier Fernandez Gonzalez(Author)
    • 2017(Publication Date)
    • Packt Publishing
      (Publisher)
    http://clojure-doc.org , where you can find the community-driven documentation site for the Clojure programming language.
    In this section, we will show you the most important concurrency elements of the Clojure programming language and how to use them. We are not going to make an introduction to the Clojure programming language. You can review the commented webs to learn how to program in Clojure.
    One of the design objectives of the Clojure programming language was to make concurrent programming easier. With this objective in mind, two important decisions were taken:
    • Clojure data structures are immutable, so they can be shared between threads without any problem. This does not mean that you can't have mutable values on concurrent applications as you'll see later.
    • Clojure separates the concepts of identity and value, almost deleting the need for explicit locks.
    Let's describe and work with the most important concurrent structures provided by the Clojure programming language. Passage contains an image

    Using Java elements

    You can use all the Java elements when you're programming in Clojure, including the concurrency ones, so you can create Threads or Executors or use the fork/join framework. This is not good practice, because Clojure makes easier concurrent programming, but you can explicitly create a Thread, as you can see in the following block of code:
    (ns example.example1) (defn example1 ( [number] (println (format "%s : %d"(Thread/currentThread) number)) )) (dotimes [i 10] (.start (Thread. (fn[] (example1 I)))))
    In this code, first, we define a function called example1 that receives a number as a parameter. Inside the function, we write information about the Thread that is executing the function and the number we have received as a parameter.
  • The Clojure Workshop
    eBook - ePub

    The Clojure Workshop

    A New, Interactive Approach to Learning Clojure

    • Joseph Fahey, Thomas Haratyk, Scott McCaughie, Yehonathan Sharvit, Konrad Szydlo(Authors)
    • 2020(Publication Date)
    • Packt Publishing
      (Publisher)
    JVM ). The JVM is a host platform. Any programming language that compiles to Java bytecode can run on the JVM. Because Clojure compiles to Java bytecode and runs on the JVM, we call it a hosted language. Java dates from the 1990s and is now one of the most popular backend languages. We can leverage existing Java libraries instead of writing a lot of code on our own. This helps us deliver new features faster.
    As we will see, importing Java classes in Clojure is a bit different than using Clojure libraries. In this chapter, we will learn how to import and call Java classes in Clojure by writing an application that performs I/O operations—reading and writing from a file.
    In the second part of this chapter, we will look into ClojureScript and JavaScript interoperability. JavaScript is a scripting language that runs in browsers. It is the most popular frontend language at the moment. ClojureScript compiles to JavaScript. In ClojureScript, we can use JavaScript libraries. This gives us access to a huge amount of code written by other developers. A great boost to our productivity.

    Using Java in Clojure

    Any code written by a developer needs to be converted to code that is understood by a machine. An interpreter uses code from a developer and compiles it into machine code. Each operating system is different, hence the need for platform-specific compilers and interpreters. One of the reasons why Java is so successful is that it provides the JVM, which takes human-understandable code and converts it into machine code. Developers are not usually interested in the JVM. They can focus on writing code in Java without interacting with the underlying operating system. This job is done by the JVM.
    Clojure is a hosted language. It means that it uses the JVM instead of creating a new runtime environment. Clojure cleverly reuses facilities provided by the JVM. This is a very powerful approach. Things such as garbage collection, threading, concurrency, IO operations (all of which will be explained in the following paragraphs) are JVM battle-tested technologies that Clojure relies on.
  • The Well-Grounded Java Developer

    Chapter 10. Clojure: safer programming

    This chapter covers
    • Clojure’s concept of identity and state
    • The Clojure REPL
    • Clojure syntax, data structures, and sequences
    • Clojure interoperability with Java
    • Multithreaded development with Clojure
    • Software transactional memory
    Clojure is a very different style of language from Java and the other languages we’ve studied so far. Clojure is a JVM reboot of one of the oldest programming languages—Lisp. If you’re not familiar with Lisp, don’t worry. We’ll teach you everything you need to know about the Lisp family of languages to get you started with Clojure.
    In addition to its heritage of powerful programming techniques from classic Lisp, Clojure adds amazing cutting-edge technology that’s very relevant to the modern Java developer. This combination makes Clojure a standout language on the JVM and an attractive choice for application development.
    Particular examples of Clojure’s new tech are its concurrency toolkits and data structures. The concurrency abstractions enable programmers to write much safer multithreaded code. These can be combined with Clojure’s seq abstraction (a different take on collections and data structures) to provide a very powerful developer toolbox.
    To access all of this power, some important language concepts are approached in a fundamentally different way from Java. This difference in approach makes Clojure interesting to learn, and it will probably also change the way you think about programming. Learning Clojure can help to make you a better programmer in any language.
    We’ll kick off with a discussion of Clojure’s approach to state and variables. After some simple examples, we’ll introduce the basic vocabulary of the language—the special forms that can be used to build up the rest of the language. We’ll also delve into Clojure’s syntax for data structures, loops, and functions. This will allow us to introduce sequences, which are one of Clojure’s most powerful abstractions. We’ll conclude the chapter by looking at two very compelling features: tight Java integration and Clojure’s amazing concurrency support.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.