Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clojure vs c# language

I am still trying to understand the clear benefits of clojure. I understand that it is a dynamic, [almost] purely functional language that lends itself well to unit testing, concurrency and rapid development.

I was watching this presentation by its founder Rich Hickey.

Around the 25th minute mark, you can see a lot of wonderful ways in which clojure makes programming easy by ability to define lists, and vectors dynamically:

( 123) [123]

ability to use those dynamically defined vectors as functions

([ 123] 0)

ability to call functions on newly defined strings

(.ToUpperCase "foo")

so as Rich puts it, "syntax for the literals is the syntax for the language".. beautiful.

but isn't it possible to do this style of programming in .net 4.0? (albeit more chatty)

(new[] {1,2,3})[0]
"foo".ToUpper()

etc.. .net could also be used to program in a functional way (although it is not enforced)

these are trivial examples, but seems like this is the basis of clojure. What are some features that set clojure more apart, and make it a better choice in some scenarios?

like image 324
Sonic Soul Avatar asked Jun 08 '11 13:06

Sonic Soul


People also ask

Is Clojure in decline?

The Clojure community is growing stronger In fact, 25 percent of current developers have been using Clojure for a year or less, which is a great sign of the health of Clojure looking at 2022 and beyond.

Why is Clojure not popular?

Clojure may not find much popularity since it lacks sufficient Clojure-based libraries and frameworks, compared to other applications worth consideration. The lisp syntax of Clojure is also more difficult to read, and therefore could stir being unfamiliar with the tool.

Is Clojure still used?

Working with Clojure The latest version of Clojure (1.11) has a 41% adoption rate and Java 17+ shows a 49% adoption rate among Clojure users. VS Code use with Calva has continued to grow with its integration of clojure-lsp and joyride.

Is Clojure used in industry?

Clojure is a good choice for a wide variety of projects. You can use it from social networking industry to Big Data solutions. Initially, Clojure language was targeted for working with JVM. So, the most popular modern Clojure implementation uses the Java Virtual Machine.


2 Answers

I personally think that Clojure's syntax is very effective, concise and even beautiful once you get the hang of it. However in my view that isn't the most compelling reason to select a language.

My personal reasons for preferring Clojure as my "general purpose development language of choice" after many years of Java and C# are:

  • Macro metaprogramming - Clojure is a Lisp and inherits the Lisp family "Code is Data" motto. Because the language itself is written in a form that is itself a data structure within the language (this is called Homoiconicity), it's trivial to write code that generates code in whatever form you choose . You basically never need to worry about "Design Patterns" ever again- if there's something missing in the language that you want to use, you just extend the language with a macro and move on.....

  • Dynamic by default - Clojure is a dynamic language by default, which means that it automatically "does the right thing" in a flexible way for most tasks. This has the effect of making you more productive. Examples are automatic overflow to BigInteger arithmetic, being able to put any object types you like in a collection, never needing to declare parameter types etc. At the same time, you can specify type information to improve performance if needed (by directly using primitives or providing type hints for example) - so you can get really fast performance when required.

  • Emphasis on functional programming with persistent, immutable data structures and lazy evaluation - in particular all the core Clojure library is designed to support this style of development by default. Once you get the hang of it (I admit it's not easy...) FP is exceptionally powerful. While you can emulate "FP-style" code in almost any language, you only really get the full advantages when it is a pervasive feature of the language (Clojure, Haskell, ML spring most obviously to mind)

  • Excellent multi-core concurrency. Thanks to a very novel STM system, I believe Clojure has the best concurrency story of any language at the moment (see this video for more elaboration by Rich Hickey himself)

  • Open source library ecosystem - I'm a big fan of open source and Clojure is very much an "open source" friendly language. The language itself and pretty much every library is open source, and if that isn't enough for you then it's trivial to call Java libraries. Given the breadth of the whole Java/JVM open source ecosystem, this means that pretty much anything you want to do is readily available in a library (a big advantage over most new languages!)

  • JVM interoperability - you may or not care about this, but personally it's an important feature for me to be able to integrate with libraries and tools in the Java/JVM universe. Clojure makes this very easy - Clojure objects are JVM objects under the hood and calling a Java method is usually as simple as (.someMethod someObject someParameter)

  • Interactive development at the REPL - typical Clojure development style is to interact with a running Clojure program at the REPL. You can redefine virtually anything on the fly without having to do a compile / build / test cycle. It's an amazingly productive way of working - I basically build a running program at the REPL and then copy the right commands into a source file to run in the future. Stupid example - I have a one-liner that is able to visualise various data structures from a running program in a chart using Incanter. I can immediately see in a visual way if something is going wrong and this alone has saved me weeks of debugging....

  • Community - I personally like the Clojure community, which is small but fast-growing. It's got the right mix of pragmatism (i.e. getting things done), helpfulness and caring about doing things well which I think is important.

like image 155
mikera Avatar answered Oct 19 '22 22:10

mikera


The Clojure rationale gives much better details than I can, but my reasons for learning Clojure were:

  • Macros - Code is data, data is code (homoiconic). Macros give you the ability to write new code at compile time and that can allow you to add new syntax to the language.
  • Software Transactional Memory - STM allows you to write code without worrying about low level concurrency details. In a software transaction, the Clojure runtime detects whether two transactions are accessing the same data and backs one off and retries.
  • Integration with existing VM - By being able to use Java, Clojure automatically has access to millions of lines of well tested code.
like image 38
Jeff Foster Avatar answered Oct 19 '22 21:10

Jeff Foster