Clojure Journey XII – Short Tip: Multi-arity functions

In our last posts of this series we have been learning everything that we need to create beautiful functions, let, destructuring, and of course, functions. In this post, we’ll talk about a simple but powerful feature, multi-arity functions, an interesting feature shipped with Clojure that among other things, allow us to make “default values” on functions parameters.

Arity can be basically defined as the number of arguments that a function can take. 

Thinking about magic, a great wizard will need to adapt his magic depending on some conditions. Using multi-arity functions, as the name says, you can write a function that accepts a different number of arguments and runs a different branch of code for each.

The syntax is simple, using the already known defn macro that is used to create functions! Just wrap each arity around parentheses.

=> (defn foo
     ([x] (+ x 1))
     ([x y] (+ x y)))
#'user/foo
=> (foo 1)
2
=> (foo 1 3)
4

These kinds of functions are heavily used among Clojure wizards to provide default argument values to its functions, just calling the same function inside a less arity branch of the function, easy right?

=> (defn foo
     ([x] (foo x 1))
     ([x y] (+ x y)))
#'user/foo
=> (foo 1)
2
=> (foo 1 3)
4

Conclusions

A short post talking about a simple but powerful feature of Clojure language that will be used almost all the time in your life as a wizard. Using multi-arity functions we can start creating default arguments to our functions, among many other possible scenarios.

Final thought

If you have any questions that I can help you with, please ask! Send an email (otaviopvaladares at gmail.com), pm me on my Twitter, or comment on this post!

Newsletter 21 – 11/2021

Async Ruby – A lot of interesting things is being released on Ruby, related to asynchronous programming, the new Fiber Scheduler is powerful and this post demonstrates it, and how to use its power using async gem.

Why The Status Quo Is So Hard To Change In Engineering Teams – Have you ever come across situations that everyone thought were obvious except you? And you just gave up trying to change them? Changing the status quo in engineering teams is hard, and this post talks about it! It’s important to identify it and fight against these situations.

How we measure Backstage success at Spotify – Measure success of internal developer tools is always hard for those who work in platform teams, this post explains how Spotify is measuring the success of its backstage platform.

Build an Elixir Redis Server that’s 100x faster than HTTP – Have you never thought to use another protocol instead of HTTP between your systems?

MISC

How Long Does It Take Ordinary People To “Get Good” At Chess? – This post not only answers an interesting question but also explains how the author reached the answer, analyzing the open data of lichess.org.

An oral history of Bank Python – Most Python systems that you find in great investments banks have a different structure than what we usually find in open source/startups code. This post is a brief view of what you would find in these companies.

Software Estimation Is Hard. Do It Anyway. – Everybody knows, software estimation is hard tô do, and probably will fail, but do It anyway.

Explaining Ruby Fibers – Ruby fibers are in the spotlight, this post explains what is fibers are and how it works.

Why Lisp? – LISP is an immortal and iconic language, being powerful over 60 years after its conception, this post tries to answer a famous question, why lisp?

Three Ways to Debug Code in Elixir -Debugging is art, and know how to debug what you’re writing is essential. This post shows three ways to debug