concept function in category clojure

This is an excerpt from Manning's book Clojure: The Essential Reference.
Figure 1.1. The template for a function as it is illustrated in the book, with ovals explaining what each section is about.
![]()
The content of the single namespace clojure.core(what is commonly referred as "core"). Core contains the functions that have evolved to be the main public API for the language, including basic math operators, functions to create and manipulate other functions, conditionals. Core currently contains around 700 definitions between functions and macros. Functions in core are always available without any explicit reference from any namespace.
Namespaces other than "core" (still shipped as part of Clojure). These are usually prefixed with clojurefollowed by a descriptive name, likeclojure.test,clojure.zippersorclojure.string. Functions in these namespaces are sometimes available just prefixing their namespace (likeclojure.string/upper-case) but in other cases they need to be imported in the current namespace using “require” [8].
Without too much surprise, a functional language is specifically good at providing developers with tools and syntax support for creating and composing functions. This chapter groups together the functions in the Clojure standard library that are dedicated to manipulate or generate other functions. The chapter splits them into 4 broad categories:
Function Definition. A function is the fundamental unit of composition in Clojure. This section contains the main macros dedicated to declaring new functions.
Higher order functions. This section describes functions and macros whose main goal is to produce new functions guided by an user defined computation or other existing definitions.
We can see from the contract that
defnaccepts metadata definitions, with slightly different syntax and meaning, in multiple positions. Some metadata are available as part of the resulting var definition, others inform the compiler during expression parsing, others are used at runtime during evaluation of the function. We can see all of them at once in the following (admittedly contrived) example:(defn ^{:t1 1} foo ; #1 "docstring" {:t2 2} (^{:t3 3} [a b] {:t4 4} (+ a b)) ; #2 {:t5 5}) ; #3 (meta #'foo) ; #4 ;; {:ns #object[clojure.lang.Namespace 0x33f17f86 "user"], ;; :name foo, ;; :t2 2, ;; :file ;; "/private/var/form-init5372276344059831979.clj", ;; :column 1, ;; :line 1, ;; :arglists ([a b]), ;; :doc "docstring", ;; :t1 1, ;; :t5 5} (meta (first (:arglists (meta #'foo)))) ; #5 ;; {:t3 3, :t4 4}It’s worth noting that metadata at the end of the function are available only if when arities (the argument vector followed by the body) are wrapped by parenthesis. It’s important to remember this aspect when the function has a single arity, as this is commonly written without the pair of surrounding parenthesis. For instance, the example above works because
([a b] (+ a b))is wrapped in parenthesis.Along with reserved keys like
:doc,:tag,:preor:post, we can use metadata to annotate functions with any sort of contextual information. Annotations can be later analyzed and acted upon by libraries or tools. Metadata are used extensively incore.testnamespace for example, to mark a function as "test" and run it accordingly. Key-value pairs in metadata can be of any type, such as strings, numbers or other structured data. However,booleanvalues are so frequent that they are commonly referred as "tags". Tags are provided with and additional prefix syntax that uses the caret symbol "^:".The following example showcases a profiling feature that scans a namespace in search of functions tagged
^:bench. When such a tag exists, the corresponding function is redefined to check execution time:

This is an excerpt from Manning's book Get Programming with Clojure.
But if you think about it, you will see that this notation has the merit to be symmetric. It treats in the same way the function and its arguments: all go inside the parenthesis.
In Clojure, functions are first-class citizens. It means that in addition of being able to create functions in some global scope, we can manipulate them in the language like other objects. For example,
We can create a function in the middle of another function
when a function returns a boolean, it is quite common to add a question mark at the end of the function name, like for instance: zero? and odd? functions. We prefer to name a function zero? than is-zero like you might have seen in other languages. when a function modifies the state of the program (e.g. when it calls def), it is quite common to add an exclamation point at the end of the function name. For instance, if we want to create a function that checks whether a number is greater than 42, a good name for it would be greater-than-42? and the function definition would look like that:
(defn greater-than-42? [x] (> x 42))If we want to create a function that modifies the value of a var named my-counter , we would create a function like this one:
Figure 13.4 A box diagram for impure functions. Arrows that are directed from the outside toward the the box indicate a factor that influences the behaviour of the function. Arrows that are directed from the box toward the outside indicates an influence of the function. Impure functions either have side effects or depend on external factors.
![]()

This is an excerpt from Manning's book Clojure in Action, Second Edition.
All of the code so far has shown examples of the first purpose—to call functions. Inside a set of parentheses, the first language form is always a function, macro, or special form, and all subsequent forms are its arguments. Figure 1.3 is a simple example of this use of parentheses. We’ll cover what macros and special forms are as we encounter them, but for now you can think of them as functions that get special treatment.
Start training your brain to associate left parenthesis with function invocation. That left parenthesis is like a phone being held up to the function’s ear, getting ready to call it with the rest of the items up to the matching right parenthesis. It will become increasingly important to have this association firmly planted in your mind once we start looking at higher-order functional programming patterns. Also remember that arguments to functions won’t always be simple values but, as in the earlier examples, will be nested expressions—see figure 1.4 for an example.
This implementation is far more readable, because it expresses the intent with simplicity and clarity. The select-if function is another useful, low-level function that you can use with any sequence. In fact, Clojure comes with such a function, one that you’ve seen before: filter. Here, for instance, is the same selection of odd numbers you just saw:
(filter odd? [5 7 9 3 4 1 2 8]) ;=> (5 7 9 3 1)Note that although filter returns a lazy sequence here, and the select-if function returned a vector, as long as your program expects a sequence, either function will work.
Over the last few pages, you’ve created the functions do-to-all, compute-across, and select-if, which implement the essence of the built-in map, reduce, and filter functions. The reason for this was two-fold: to demonstrate common use cases of higher-order functions and to show that the basic form of these functions is rather simple to implement. The select-if isn’t lazy, for instance, but with all the knowledge you’ve gained so far, you can implement one that is. With this background in place, let’s explore a few other topics of interest of functional programs.
It’s also worth noting that this is the only way to access the internals of arjun. This is a form of message passing: the keywords are messages that you’re sending to a receiver object, which in this case is the function object arjun. You can implement fewer of these should you choose to. For instance, you might deem the password to be a hidden detail. The modified function might look as follows:
![]()
Although the new-user function and the returned inner function can see password, anyone using the returned inner function can only see the hash of the password. Using a closure, you were able to hide information from callers of a function that’s still visible to the function itself. Having come this far, we’ll take a short break to compare functions such as arjun with objects from other languages.

This is an excerpt from Manning's book The Joy of Clojure, Second Edition.
But in Clojure, a function and a method call look like this:
In a programming language such as Java, there’s no notion of a standalone function.[1] Instead, every problem solvable by Java must be performed with the fundamental philosophy that everything is an object. This view on writing programs is therefore rooted in the idea that behaviors in a program must be either modeled as class instances or attached to them—wise or not. Clojure, on the other hand, is a functional programming language and views the problem of software development as the application of functions to data. Likewise, functions in Clojure enjoy equal standing with data—functions are first-class citizens. Before we start, we should define what makes something first class:
The function fnth builds a list of the function rest of the appropriate length with a final first consed onto the front. This list is then fed into the comp function via apply, which takes a function and a sequence of things and effectively calls the function with the list elements as its arguments. At this point, there’s no longer any doubt that the function fnth builds new functions on the fly based on its arguments. Creating new functions this way is a powerful technique, but it takes some practice to think in a compositional way. It’s relatively rare to see more than one open parenthesis in a row like this in Clojure, but when you see it, it’s almost always because a function (such as fnth) is creating and returning a function that’s called immediately. A general rule of thumb is that if you need a function that applies a number of functions serially to the return of the former, then composition is a good fit:
(map (comp #1 Keyword #2 #(.toLowerCase %) #3 name) #4 '(a B C)) #5 ;;=> (:a :b :c) #1 - Compose fn #2 - … make a keyword #3 - … from a lowercase #4 - … string name #5 - Mapped over a list of symbols !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[]"} !@%STYLE%@!Splitting functions into smaller, well-defined pieces fosters composability and, as a result, reuse.