Clojure Language
Clojure is a dialect of
the Lisp programming
language created
by Rich Hickey in 2007. It is a functional general-purpose language. Where dialect of a programming
language is
a (relatively small) variation or extension of the language that does not
change its intrinsic nature.
1.1 About Clojure
“Clojure is a dynamic
programming language that targets the Java Virtual Machine (and the CLR ). It
is designed to be a general-purpose language, combining the approachability and
interactive development of a scripting language with an efficient and robust
infrastructure for multithreaded programming.
- Clojure is a Lisp; Lisp variants obviously have both tremendous longevity and relevant to software development, going back to 1958.
- Clojure is a (mostly) functional programming language, and is therefore especially well suited to concurrency. The increasing number of CPU cores in modern computers will make this important to far more projects in the future.
- Clojure combines the broad platform support of Java, with a much more concise and programmable programming language.
Clojure is
high signal, low noise. As a result, Clojure programs are short programs. Short
programs are cheaper to build, cheaper to deploy, and cheaper to maintain. This
is particularly true when the programs are concise rather than merely terse.
1.2 Functional Programming Concepts
Functional
programming leads to code that is easier to write, read, test, and reuse.
Here’s how it works. A pure function has no side effects, that is, it
does not depend on anything but its arguments, and its only influence on the
outside world is through its return value. Mathematical functions are pure
functions. Two plus two is four, no matter where and when you ask. Also, asking
doesn’t do anything other han
return the answer.
Program output
is decidedly impure. For example, when you println, you change the
outside world by pushing data onto an output stream. Also, the results of
println depend on state outside the function: the standard output stream might
be redirected, closed, or broken.
1.3 Lisp programming language
Lisp
is a programming language known for its expressiveness and power, but it was
often thought of as not being well suited for general use. That has all changed
with Clojure, a Lisp dialect that runs on the Java™ platform. Now you can
harness the power of Lisp anywhere you have a Java Virtual Machine handy.
Lisp was
originally created as a practical mathematical notation for computer programs,
influenced by the notation of Alonzo Church's lambda calculus. It
quickly became the favored programming language for artificial
intelligence (AI)
research. As one of the earliest programming languages, Lisp pioneered many
ideas in computer
science, including tree
data structures, automatic
storage management, dynamic
typing, and the self-hosting compiler.
1.4 Advantages of Clojure
Clojure is used for:
- Scraping web pages
- Shell scripts
- Building websites
- Playing around with OpenGL
- Writing async webservers
- HTML Templating
- Running parallel tasks (fetching multiple URLs and process in parallel)
- Playing around with real time audio
- Simulations
1.5 Example code of Clojure
- If else condition in Clojure
( defn check_greater [ a b]
( if (< a b)
(println “b is greater”)
(println “a is greater”)
))
- Loop function
(loop [x 10]
(when (> x 1)
(println x)
(recur (- x 2))))
No comments:
Post a Comment