Understanding how to call Clojure function


#1

When I look at Clojure code, for example a Clojure library on Github. How do I know what kind of arguments to pass to a arbitrary function? I can only “guess” looking to the parameter names.


#2

That’s a really good question.

I know how nice it is to see a type signature and know that the compiler will make sure you give it the correct arguments. This mindset simply won’t work in an untyped language.

What I rely on instead is a combination of convention, documentation, experimentation, and code reading.

There are many conventions in the clojure.core library that are pretty useful. For instance, f, g, etc stand for functions. coll stands for collection. n stands for an integer. They aren’t enforced statically, but they give a good idea.

Sometimes, though, there is no convention, and documentation is needed. The most useful is examples. Often a library will have some useful example uses in the Readme. Those help you get an idea for how to use things.

Experimentation is trying to run stuff at the REPL. That gives you a great idea of what can be used where.

As a last resort, I’ll read the code. It’s usually pretty clear.

All that said, remember in a dynamically typed language, the types are not as important as the abstraction. It’s not about lists, it’s about seqs. It’s not about clojure.lang.Fn (the type of closures) but it’s about clojure.lang.IFn (the interface). It’s a bit like type classes but less formal and more flexible.

Again, great question. It was a pleasure to add in my two cents.

Eric