Clojure Explained

Clojure
Logo Size:120px
Paradigm:multi-paradigm
Family:Lisp
Designer:Rich Hickey
Latest Release Version:1.11.2
Memory Management:Garbage collection via a host (JVM, CLR, a JavaScript runtime such as V8 in case of ClojureScript or BEAM (see below)
Dialects:ClojureScript, ClojureDart
License:Eclipse Public
Wikibooks:Clojure Programming

Clojure (like closure)[1] [2] is a dynamic and functional dialect of the Lisp programming language on the Java platform.[3] [4]

Like most other Lisps, Clojure's syntax is built on S-expressions that are first parsed into data structures by a reader before being compiled.[5] [1] Clojure's reader supports literal syntax for maps, sets and vectors along with lists, and these are compiled to the mentioned structures directly.[5] Clojure treats code as data and has a Lisp macro system.[6] Clojure is a Lisp-1 and is not intended to be code-compatible with other dialects of Lisp, since it uses its own set of data structures incompatible with other Lisps.[6]

Clojure advocates immutability and immutable data structures and encourages programmers to be explicit about managing identity and its states.[7] This focus on programming with immutable values and explicit progression-of-time constructs is intended to facilitate developing more robust, especially concurrent, programs that are simple and fast.[8] [9] [1] While its type system is entirely dynamic, recent efforts have also sought the implementation of a dependent type system.[10]

The language was created by Rich Hickey in the mid-2000s, originally for the Java platform; the language has since been ported to other platforms, such as the Common Language Runtime (.NET). Hickey continues to lead development of the language as its benevolent dictator for life.

History

Rich Hickey is the creator of the Clojure language.[3] Before Clojure, he developed dotLisp, a similar project based on the .NET platform,[11] and three earlier attempts to provide interoperability between Lisp and Java: a Java foreign language interface for Common Lisp (jfli),[12] A Foreign Object Interface for Lisp (FOIL),[13] and a Lisp-friendly interface to Java Servlets (Lisplets).[14]

Hickey spent about two and a half years working on Clojure before releasing it publicly in October 2007,[15] much of that time working exclusively on Clojure with no outside funding. At the end of this time, Hickey sent an email announcing the language to some friends in the Common Lisp community.

Clojure's name, according to Hickey, is a word play on the programming concept "closure" incorporating the letters C, L, and J for C#, Lisp, and Java respectively—three languages which had a major influence on Clojure's design.[2]

Design

Rich Hickey developed Clojure because he wanted a modern Lisp for functional programming, symbiotic with the established Java platform, and designed for concurrency.[8] [9] [16] [1] He has also stressed the importance of simplicity in programming language design and software architecture, advocating for loose coupling, polymorphism via protocols and type classes instead of inheritance, stateless functions that are namespaced instead of methods or replacing syntax with data.[17] [18] [19]

Clojure's approach to state is characterized by the concept of identities,[7] which are represented as a series of immutable states over time. Since states are immutable values, any number of workers can operate on them in parallel, and concurrency becomes a question of managing changes from one state to another. For this purpose, Clojure provides several mutable reference types, each having well-defined semantics for the transition between states.[7]

Clojure runs on the Java platform and as a result, integrates with Java and fully supports calling Java code from Clojure,[20] [1] and Clojure code can be called from Java, too.[21] The community uses tools such as Clojure command-line interface (CLI) or Leiningen for project automation, providing support for Maven integration. These tools handle project package management and dependencies and are configured using Clojure syntax.

As a Lisp dialect, Clojure supports functions as first-class objects, a read–eval–print loop (REPL), and a macro system. Clojure's Lisp macro system is very similar to that of Common Lisp with the exception that Clojure's version of the backquote (termed "syntax quote") qualifies symbols with their namespace. This helps prevent unintended name capture, as binding to namespace-qualified names is forbidden. It is possible to force a capturing macro expansion, but it must be done explicitly. Clojure does not allow user-defined reader macros, but the reader supports a more constrained form of syntactic extension.[22] Clojure supports multimethods[23] and for interface-like abstractions has a protocol[24] based polymorphism and data type system using records,[25] providing high-performance and dynamic polymorphism designed to avoid the expression problem.

Clojure has support for lazy sequences and encourages the principle of immutability and persistent data structures. As a functional language, emphasis is placed on recursion and higher-order functions instead of side-effect-based looping. Automatic tail call optimization is not supported as the JVM does not support it natively;[26] [27] [28] it is possible to do so explicitly by using the recur keyword.[29] For parallel and concurrent programming Clojure provides software transactional memory,[30] a reactive agent system, and channel-based concurrent programming.[31]

Clojure 1.7 introduced reader conditionals by allowing the embedding of Clojure, ClojureScript and ClojureCLR code in the same namespace.[5] Transducers were added as a method for composing transformations. Transducers enable higher-order functions such as map and fold to generalize over any source of input data. While traditionally these functions operate on sequences, transducers allow them to work on channels and let the user define their own models for transduction.[32] [33] [34]

Extensible Data Notation

Extensible Data Notation, or edn,[35] is a subset of the Clojure language intended as a data transfer format. It can be used to serialize and deserialize Clojure data structures, and Clojure itself uses a superset of edn to represent programs.

edn is used in a similar way to JSON or XML, but has a relatively large list of built-in elements, shown here with examples:

In addition to those elements, it supports extensibility through the use of tags, which consist of the character # followed by a symbol. When encountering a tag, the reader passes the value of the next element to the corresponding handler, which returns a data value. For example, this could be a tagged element: #myapp/Person {:first "Fred" :last "Mertz"}, whose interpretation will depend on the appropriate handler of the reader.

This definition of extension elements in terms of the others avoids relying on either convention or context to convey elements not included in the base set.

Alternative platforms

The primary platform of Clojure is Java,[4] [20] but other target implementations exist. The most notable of these is ClojureScript,[36] which compiles to ECMAScript 3,[37] and ClojureCLR,[38] a full port on the .NET platform, interoperable with its ecosystem.

Other implementations of Clojure on different platforms include:

Tools

Tooling for Clojure development has seen significant improvement over the years. The following is a list of some popular IDEs and text editors with plug-ins that add support for programming in Clojure:[50]

In addition to the tools provided by the community, the official Clojure command-line interface (CLI) tools[63] have also become available on Linux, macOS, and Windows since Clojure 1.9.[64]

Development

The development process is restricted to the Clojure core team, though issues are publicly visible at the Clojure JIRA project page.[65] Anyone can ask questions or submit issues and ideas at ask.clojure.org.[66] If it's determined that a new issue warrants a JIRA ticket, a core team member will triage it and add it. JIRA issues are processed by a team of screeners and finally approved by Rich Hickey.[67] [68]

Impact

With continued interest in functional programming, Clojure's adoption by software developers using the Java platform has continued to increase. The language has also been recommended by software developers such as Brian Goetz,[69] [70] [71] Eric Evans,[72] [73] James Gosling,[74] Paul Graham,[75] and Robert C. Martin.[76] [77] [78] [79] ThoughtWorks, while assessing functional programming languages for their Technology Radar,[80] described Clojure as "a simple, elegant implementation of Lisp on the JVM" in 2010 and promoted its status to "ADOPT" in 2012.[81]

In the "JVM Ecosystem Report 2018" (which was claimed to be "the largest survey ever of Java developers"), that was prepared in collaboration by Snyk and Java Magazine, ranked Clojure as the 2nd most used programming language on the JVM for "main applications".[82] Clojure is used in industry by firms[83] such as Apple,[84] [85] Atlassian,[86] Funding Circle,[87] Netflix,[88] Nubank,[89] Puppet,[90] and Walmart[91] as well as government agencies such as NASA.[92] It has also been used for creative computing, including visual art, music, games, and poetry.[93]

In the 2023 edition of Stack Overflow Developer Survey, Clojure was the fourth most admired in the category of programming and scripting languages, with 68.51% of the respondents who have worked with it last year saying they would like to continue using it. In the desired category, however it was marked as such by only 2.2% of the surveyed, whereas the highest scoring JavaScript was desired by 40.15% of the developers participating in the survey.[94] It could possibly be attributed to limited professional opportunities for Clojure developers with just over 50 search results for "Clojure developer" at the U.S. section of Indeed, as of May 2024.[95]

Release history

VersionRelease dateMajor features, improvements
Initial public release
1.0[96] First stable release
1.1[97] Futures
1.2[98] Protocols
1.3[99] Enhanced primitive support
1.4[100] Reader literals
1.5[101] Reducers
1.5.1[102] Fixing a memory leak
1.6[103] Java API, improved hashing algorithms
1.7[104] Transducers, reader conditionals
1.8[105] Additional string functions, direct linking, socket server
1.9[106] Integration with spec, command-line tools
1.10[107] Improved error reporting, Java compatibility
1.10.1[108] Working around a Java performance regression and improving error reporting from clojure.main
1.10.2[109] Java interoperability/compatibility improvements and other important language fixes
1.10.3[110] prepl support for reader conditionals
1.11.0[111] New syntax for keyword argument invocation, new clojure.math namespace, namespace aliasing without loading, and new helper functions added to clojure.core
1.11.1[112] Rolling back unintended change in binary serialisation of objects of types clojure.lang.Keyword and clojure.lang.ArraySeq.
[113] Fix for CVE-2024-22871 Denial of Service vulnerability

See also

Further reading

Notes and References

  1. Web site: The A-Z of Programming Languages: Clojure . Edwards . Kathryn . 2009-08-10 . Computerworld.com.au . https://web.archive.org/web/20190826190240/https://www.computerworld.com.au/article/313989/a-z_programming_languages_clojure/ . 2019-08-26.
  2. Web site: meaning and pronunciation of Clojure . Hickey . Rich . 2009-01-05 . Google.com.
  3. Web site: Clojure inventor Hickey now aims for Android . Krill . Paul . 2012-03-22 . InfoWorld.com.
  4. Web site: Clojure . Clojure.org . 2019-07-07.
  5. Web site: The Reader . Clojure.org . 2019-07-07.
  6. Web site: Differences with other Lisps . Clojure.org . 2019-07-07.
  7. Web site: Values and Change: Clojure's approach to Identity and State . Clojure.org . 2019-07-07.
  8. Web site: Rationale . Hickey . Rich . Clojure.org . 2019-07-07.
  9. Web site: Expert to Expert: Rich Hickey and Brian Beckman – Inside Clojure . Torre . Charles . 2009-10-06 . MSDN.com.
  10. Web site: clojure/spec.alpha . 2017-04-26 . GitHub.
  11. Web site: [ANN] dotLisp: A Lisp dialect for .Net ]. Hickey . Rich . 2002-10-16 . Google.com.
  12. Web site: jfli . Hickey . Rich . 2013-04-15 . SourceForge.net.
  13. Web site: foil: Foreign Object Interface for Lisp . Hickey . Rich . 2013-04-03 . SourceForge.net.
  14. Web site: Lisplets . Hickey . Rich . 2013-03-07 . SourceForge.net.
  15. Hickey . Rich . 2020-06-12 . A history of Clojure . Proceedings of the ACM on Programming Languages . 4 . HOPL . 1–46 . 10.1145/3386321 . 219603760 . free .
  16. Web site: Economy Size Geek – Interview with Rich Hickey, Creator of Clojure . LinuxJournal.com . Elmendorf . Dirk . 2010-04-01.
  17. Web site: Simple Made Easy . 2024-05-02 . InfoQ . en.
  18. Web site: (iterate think thoughts): Advantages of Data Oriented Programming . 2024-05-02 . yogthos.net.
  19. Web site: talk-transcripts/Hickey_Rich/EffectivePrograms.md at master · matthiasn/talk-transcripts . 2024-05-02 . GitHub . en.
  20. Web site: Hosted on the JVM . Clojure.org . 2019-07-07.
  21. Web site: Java Interop . Clojure.org . 2019-07-07.
  22. Web site: edn . GitHub . Hickey . Rich . 2019-07-07.
  23. Web site: Multimethods and Hierarchies . Clojure.org . 2019-07-07.
  24. Web site: Protocols . Clojure.org . 2019-07-07.
  25. Web site: Datatypes: deftype, defrecord and reify . Clojure.org . 2019-07-07.
  26. Web site: Stewardship: the Sobering Parts . YouTube.com . Goetz . Brian . 2014-11-20.
  27. Web site: tail calls in the VM . Oracle.com . Rose . John . 2007-07-12.
  28. Web site: Some languages need to be able to perform tail calls . Java.net . Rose . John . 2009-02-11.
  29. Web site: Special Forms . Clojure.org . 2019-07-07.
  30. Web site: Refs and Transactions . Clojure.org . 2019-07-07.
  31. Web site: Clojure core.async Channels . Clojure.org . Hickey . Rich . 2013-06-28.
  32. Web site: Transducers . YouTube.com . Hickey . Rich . 2014-09-17.
  33. Web site: Transducers are Coming . Cognitect.com . Hickey . Rich . 2014-08-06.
  34. Web site: Inside Transducers . YouTube.com . Hickey . Rich . 2014-11-20.
  35. Web site: Official EDN Spec . edn-format.org . 2022-04-27.
  36. Web site: ClojureScript . ClojureScript.org . 2019-07-06.
  37. Web site: ClojureScript – FAQ (for JavaScript developers) . ClojureScript.org . 2018-02-04.
  38. Web site: ClojureCLR . GitHub . 2012-06-28.
  39. Web site: Babashka . Borkent . Michiel . Babashka.org . 2019-08-19.
  40. Web site: A Lisp on Perl . Hu . Wei . MetaCPAN.org . 2019-07-06.
  41. Web site: Clojerl . GitHub . Facorro . Juan . 2019-07-06.
  42. Web site: basilisp . GitHub . 2024-05-13.
  43. Web site: ClojureRS . GitHub . 2022-03-17.
  44. Web site: Ferret . Akkaya . Nurullah . Ferret-Lang.org . 2019-07-06.
  45. Web site: jank . Wilkerson . Jeaye . Jank-Lang.org . 2022-08-17.
  46. Web site: Joker . Bataev . Roman . Joker-Lang.org . 2019-07-06.
  47. Web site: Laz3r . Cannon . Aemon . GitHub . 2019-07-06.
  48. Web site: Pixie . Baldridge . Timothy . PixieLang.org . 2019-07-06.
  49. Web site: Rouge . Connor . Ashe . GitHub . 2019-07-06.
  50. Web site: "State of Clojure 2019" Results . Clojure.org . Miller . Alex . 2019-02-04.
  51. Web site: CIDER: The Clojure Interactive Development Environment that Rocks . CIDER.mx . Batsov . Bozhidar . ((contributors)) . 2019-07-05.
  52. Web site: Cursive: Provides full Clojure and ClojureScript language support . JetBrains.com . Fleming . Colin . 2019-07-05.
  53. Web site: Clojure Sublimed . PackageControl.io . Prokopov . Nikita . 2023-02-18.
  54. Web site: Tutkain . FlowThing.me . Helenius . Eero . 2023-02-18.
  55. Web site: fireplace.vim: Clojure REPL Support . VIM.org . Pope . Tim . 2019-07-05.
  56. Web site: Clojure and Vim: An overview – It's very possible . JUXT.pro . Monroe . Dominic . 2016-12-13.
  57. Web site: Masashi . Iizuka . 2024-04-29 . vim-elin: A Clojure development environment for Vim/Neovim, primarily written in Babashka. . live . https://web.archive.org/web/20240502002453/https://github.com/liquidz/vim-elin . 2024-05-02 . 2024-05-02 . GitHub.
  58. Web site: Neovim Clojure(Script) tooling over prepl . GitHub . Caldwell . Oliver . 2019-11-09.
  59. Web site: Getting started with Clojure, Neovim and Conjure in minutes . oli.me.uk . Caldwell . Oliver . 2019-11-06.
  60. Web site: Calva: Clojure & ClojureScript Interactive Programming . VisualStudio.com . Strömberg . Peter . 2019-07-05.
  61. Web site: Clover . Szabo . Maurício . VisualStudio.com . 2021-01-28.
  62. Web site: Overview - Clojure LSP . 2024-05-02 . clojure-lsp.io.
  63. Web site: Deps and CLI Guide . Miller . Alex . Clojure.org . 2019-07-08.
  64. Web site: Clojure 1.9 . Miller . Alex . Cognitect.com . 2017-12-08.
  65. Web site: Clojure . Atlassian.net . 2019-07-07.
  66. Web site: Clojure Forum . 2020-03-20 . clojure.org.
  67. Web site: Open Source is Not About You . Hickey . Rich . 2018-11-26 . GitHub.
  68. Web site: Workflow . Clojure.org . 2019-07-07.
  69. Web site: Brian Goetz' favorite non-Java JVM language (Part 1 of 3) . Twitch.tv . Goetz . Brian . 2020-05-24.
  70. Web site: Brian Goetz' favorite non-Java JVM language (Part 2 of 3) . Twitch.tv . Goetz . Brian . 2020-05-24.
  71. Web site: Brian Goetz' favorite non-Java JVM language (Part 3 of 3) . Twitch.tv . Goetz . Brian . 2020-05-24.
  72. Web site: Modelling Time: Eric Evans: Domain-Driven Design Europe 2018 . YouTube.com . Evans . Eric . 2018-08-14.
  73. Web site: Eric Evans on Twitter . Twitter.com . Evans . Eric . 2014-11-21.
  74. Web site: James Gosling meetup with London Java Community . YouTube.com . 2016-10-11.
  75. Web site: Paul Graham on Twitter . Twitter.com . Graham . Paul . 2016-05-06.
  76. Web site: Why Clojure? . CleanCoder.com . Martin . Robert . 2019-08-22.
  77. Web site: Unble Bob Martin on Twitter . Twitter.com . Martin . Robert . 2018-11-29.
  78. Web site: Introduction To Functional Programming . CleanCoders.com . Martin . Robert . 2018-08-01.
  79. Web site: Pragmatic Functional Programming . CleanCoder.com . Martin . Robert . 2017-07-11.
  80. Web site: Technology Radar An opinionated guide to today's technology landscape . 2024-05-20 . Thoughtworks . en.
  81. Web site: Technology Radar: Clojure . ThoughtWorks.com . 2019-02-10.
  82. Web site: JVM Ecosystem Report 2018 . Snyk.io . Maple . Simon . Binstock . Andrew . 2018-10-17.
  83. Web site: Success Stories . Clojure.org . 2018-10-27.
  84. Web site: Roman Liutikov on Twitter . Twitter.com . Liutikov . Roman . 2017-12-17.
  85. Web site: Jobs at Apple . Apple.com . 2019-07-06.
  86. Web site: Realtime Collaboration with Clojure . YouTube.com . Borges . Leonardo . 2015-07-07.
  87. Web site: Clojure in London: Funding Circle – Lending some Clojure . JUXT.pro . Pither . Jon . 2016-10-04.
  88. Web site: The New Stack Makers: Adrian Cockcroft on Sun, Netflix, Clojure, Go, Docker and More . TheNewStack.io . Williams . Alex . 2014-08-03.
  89. Web site: Nubank adquire empresa norte-americana de tecnologia Cognitect. 23 July 2020 .
  90. Web site: A New Era of Application Services at Puppet Labs . Puppet.com . Price . Chris . 2014-04-11 . 2020-08-06 .
  91. Web site: Walmart Runs Clojure at Scale . Cognitect.com . Phillips . Marc . 2015-07-14.
  92. Web site: Common-Metadata-Repository . GitHub . 2019-07-06.
  93. Web site: Creative computing with Clojure . OReilly.com . Meier . Carin . 2015-05-06.
  94. Web site: Stack Overflow Developer Survey 2023 . 2024-05-02 . Stack Overflow . en.
  95. Web site: 2024-05-02 . "Clojure developer" search results at Indeed.com [Archived due to the mutability of the source] ]. 2024-05-02 . Archive.is.
  96. Web site: Clojure 1.0 . BlogSpot.com . Hickey . Rich . 2009-05-04.
  97. Web site: Clojure 1.1 Release . BlogSpot.com . Hickey . Rich . 2009-12-31.
  98. Web site: Clojure 1.2 Release . Google.com . Hickey . Rich . 2010-08-19.
  99. Web site: [ANN] Clojure 1.3 Released ]. Google.com . Redinger . Christopher . 2011-09-23.
  100. Web site: [ANN] Clojure 1.4 Released ]. Google.com . Dipert . Alan . 2012-04-17.
  101. Web site: ANN: Clojure 1.5 . Google.com . Halloway . Stuart . 2013-03-01.
  102. Web site: Clojure 1.5.1 . Google.com . Halloway . Stuart . 2013-03-10.
  103. Web site: [ANN] Clojure 1.6 ]. Google.com . Miller . Alex . 2014-03-25.
  104. Web site: Clojure 1.7 is now available . Clojure.org . Miller . Alex . 2015-06-30.
  105. Web site: Clojure 1.8 is now available . Clojure.org . Miller . Alex . 2016-01-19.
  106. Web site: Clojure 1.9 is now available . Clojure.org . Miller . Alex . 2017-12-08.
  107. Web site: Clojure 1.10 release . Clojure.org . Miller . Alex . 2018-12-17.
  108. Web site: Clojure 1.10.1 release . Clojure.org . Miller . Alex . 2019-06-06.
  109. Web site: Clojure 1.10.2 release . Clojure.org . Miller . Alex . 2021-01-26.
  110. Web site: Clojure 1.10.3 release . Clojure.org . Miller . Alex . 2021-03-04.
  111. Web site: Clojure 1.11.0 release . Clojure.org . Miller . Alex . 2022-03-22.
  112. Web site: Clojure 1.11.1 release . Clojure.org . Miller . Alex . 2022-04-05.
  113. Web site: Clojure 1.11.2 release . Clojure.org . Miller . Alex . 2024-03-08.