practicalli / blog-cryogen

Cryogen powered blog with all the content used to generate the static website.
https://practical.li/blog-cryogen/
3 stars 2 forks source link

Article: How to understand a Clojure function #121

Open practicalli-johnny opened 2 years ago

practicalli-johnny commented 2 years ago

When learning how to develop with Clojure, an important aspect is to understand what functions already exist and how to use those functions

Finding functions

apropos clojure cli find function ?

Read the function doc-string

The doc-string should describe the function arguments and hopefully give some indication of the types of values that the function works with

doc-strings for clojure.core functions tend to be on the terse side of things and may not immediately be understood.

Look at the examples at clojuredocs.org

Each page on clojuredocs.org includes the function doc-string and several examples of using the funciton, contributed from the community

Clojure LSP describe-thing-here will return the doc-string and examples from clojuredocs.org (TODO: check Clojure LSP is using clojuredocs.org as a source of examples and if all editors support this (or if its just Emacs LSP UI)

Experiment in the REPL

calling the function in the REPL with various arguments, experiment by calling the function with different values

Evaluate the function call to a comment to keep a history of experiment results.

Read the source code of the function

For a deep understanding of a function, or to find further inspiration on how to write Clojure, take a look at the source code of a function.

Clojure LSP and Clojure aware editors allow navigation to the source code of any Clojure function on the Classpath.

clojuredocs.org also includes a link to the source code of the org.clojure/clojure project on GitHub

Clone the org.clojure/clojure project to search through the source code and run searches on the code to find use of a function.