Clojure from zero to hero (part 2) - A bit of syntax

Jul 1, 2019

Clojure syntax

As a short intro into Clojure’s syntax, this programming language is a Lisp. This means that there’s no semicolons (;) or indentation to signal line end. There’s no curly brackets ({}) to signal the context of a class, function or method. Clojure keeps it simple and has only parentheses (()) to wrap the context of a function. Curly brackets are also known as as curly braces and are used to declare a map.

An example of declaring a Clojure function:

(defn custom-addition [a b]
  (+ a b))  ;;try running this function in your REPL

Keep in mind the parentheses requirement, the above function can be called in the REPL as:

(custom-addition 3 4)
7

⚠ New term alert: REPL

REPL is an abrevation for Read-Evaluate-Print-Loop and is an interactive environment where code is evaluated and provides instant feedback. Is a great tool to evaluate code but this doesn’t replace testing. In the REPL you can test your code with custom values but do not use it as an excuse not to transfer those tests into proper unit test files. We will speak about testing in a later post.

The REPL can be started with:

lein repl

Clojure namespaces

Clojure namespace acts as a way to separate code that belongs to the same domain logic. You can think of as Java’s package declaration. A namespace is declared at the top of your file as:

(ns com.georgeracu.site.posts.fzth.two)
;; then your Clojure code here

⚠ New symbol alert: ;

In Clojure, semicolon is used to comment out a line of code. Other usage is using double semicolon to leave a comment for a line of code. Usually, code should be self explainable, you shouldn’t have to add comments to make your code easier to read and understand. If that’s the case then think again if you can remove that comment by renaming variables and functions or by extracting logic into a different function. If you still need to add a comment using double semicolons you can use it as:

(custom-addition [a b]
  (+ a b)) ;;try running this function in your REPL

⚠ New concept alert: no return keyword

Some of us are used to specify a return statement if the function or method is not having a void or unit return type. In Clojure there’s no need to use a return statement as the last line that gets executed in the function will be returned. If the function is not a pure function with data in data out and in its execution body calls other services we call it a function with side effects. We should get back on explaining more about pure functions and side effects later on.

A code editor

So far we started using Clojure without the need to use a code editor. It’s an easy start that showed how in less than five minutes you can create and run a Web project. Next in our quest is to start writing some actual Clojure. After all, we are here to learn to code but also to write good Clojure. In order to achieve our goal we need to find a code editor that can help us write Clojure code easier. This is the task of the next article.

Tags: programmingclojure

Archives

  1. February 2024
  2. Maximizing Software Development Productivity: The Power of Flow and Minimizing Interruptions
  3. December 2023
  4. Clean Code in Java: Writing Code that Speaks
  5. Clean Code in Java: A concise guide
  6. Understanding Value Objects in Java: A Brief Guide
  7. August 2023
  8. Consuming RabbitMQ Messages with Clojure: A Step-by-Step Tutorial with Tests
  9. January 2023
  10. Running a Spring Boot service with kubernetes
  11. December 2022
  12. Hosting a PWA with Jekyll and Github pages
  13. November 2022
  14. Global Day of Code Retreat
  15. Facilitating a mini Code Retreat
  16. October 2022
  17. The Curse of Optional
  18. September 2022
  19. Testing Spring Boot Microservices - Presentation
  20. March 2022
  21. TDD Workshop
  22. February 2022
  23. Value Objects in Java
  24. Efficient Java
  25. January 2022
  26. Spring Boot testing - Focus on your changes
  27. Product users - Personas
  28. December 2021
  29. Write code fit for testing
  30. November 2020
  31. Running a Spring Boot app with kubernetes
  32. September 2019
  33. Setup GPG on Mac and sign git repositories
  34. July 2019
  35. Running a Clojure Pedestal application on Raspberry Pi model B revision 2
  36. Clojure from zero to hero (part 3) - First endpoint
  37. Clojure from zero to hero (part 2) - A bit of syntax
  38. June 2019
  39. Clojure from zero to hero (1) - explaining project.clj
  40. Clojure from zero to hero (0) - creating a Pedestal app
  41. November 2017
  42. Introduction to Docker
  43. April 2015
  44. Git micro commits
  45. July 2014
  46. Google Glass Development - setup tools, environment and turn on debugging on Glass
  47. June 2013
  48. How To: Get the rendered HTML of a webpage with Python
  49. Set union of two lists in Python