akaiyuki

{:title "Why Clojure" :layout :post :tags "Programming" }

Clojure

I've been using Clojure for a bit, it's the most recent language that I've really delved deep into learning it. For this blog, I wrote a markdown parser that takes the markdown layout and converts it to a static HTML website. Whenever I mention the language to most people, they have no idea what it is and ask about why I would use something besides the typical languages like Python, Java, C#, Javascript, Rust, etc etc. So, what is Clojure? And why should you use it?

What is Clojure?

Clojure is a functional programming language that has been around for about 15 years, it has a lot of unique features that set it apart most other languages, and personally I find it to be a joy to write with.

Functional Programming

I mentioned that Clojure is a functional programming language. In Functional Programming (FP), code is written in a way that emphasizes the use of pure functions and immutable data structures. What this means is that every time you run a function, you're guaranteed to get the same result from the program as long as you input the same data. This is benefit because it's easy to understand and test, you'll know exactly what the function will do and what it'll return. They also have no side effects, which just means that they can be used in parallel or in a distributed environment without worrying about concurrency issues or race conditions. Immutable data structures are data structures that can't be modified, and the only way you can 'change' them is by making a new string, list, or whatever type it is. This is just helpful for preventing bugs and getting the flow of the program better. Lazy eval is another cool thing that's in Clojure, it evaluates code only when it's needed, which is a great way to optimize the program, especially when you have to deal with large datasets or long sequences. Recursion is also heavily more emphasized in the language, which can make code more elegant and concise. I still struggle with recursion every now and then, but Clojure makes it easier for me? I'm not really sure, but I've gotten a lot better at recursion since learning it in Java. Tail-call optimization is performed automatically, which allows the compiler to reuse the stack frame to reduce memory usage and improve performance. Also, there are a ton of functions for transforming data structures, like map, filter, reverse, etc. Map applies a function to each element in a data structure and returns a new one, filter creates a new collection that only contains the elements that satisfy a certain condition, reverse returns a data structure with all the elements in reverse. All of these small things in the language just allow for complex data modification with a few lines of code.

Live Coding

This is definitely one of my favorite features about the language, Clojure provides a REPL (read, evaluate, print, loop) that allows you to interactively develop and test code. You can see exactly what the output of a function will be in the text editor without having to recompile the program and run all of the code. I also sometimes use it to better understand how a piece of code is running, which makes it awesome for debugging.

Runs on the Java Virtual Machine

Clojure is easily extensible, since it runs on the Java Virtual Machine, which allows you to incorporate Java libraries into your Clojure program. You don't have to write Java, you can just call the Java libraries in typical Clojure style. This is great, because it allows you to have access to the vast Java ecosystem to write more extensible apps, especially because Clojure doesn't have the biggest native support for a wide range of libraries.

Macros!

Macros are a super cool feature in Clojure, they pretty much allow you to write your own custom functions with it's special syntax, which can transform code once everything is compiled. It becomes much easier to write more expressive and easier to read code, and they can also generate code. This just makes writing repetitive code easier, or if you don't like how something is structured, you can just rewrite it in your own way. You have a lot of control with how the code works.

The Syntax

I really like the syntax for the language, I think it's extremely easy to learn and use. It's a LISP (List Processsing) dialect, which just means that the syntax is minimal, and makes it ideal for metaprogramming, which is just the ability to write macros and extend the language. Many people seem to find the parentheses overwhelming, but I think it's easier to tell where you are in the program when reading the code. It's the same constant style, which makes it easy to identify where a function is being called, code blocks, and other constructs.

Conclusion

I think I've shilled Clojure as much as I can in this blog post, I just think the language is a great way to build reliable and scalable programs, with little amount of code. If you have time to learn a new language, or if you're curious, I would 100% give it a shot. Take the Clojure Pill lmao