Clojure Functions (L17) Flashcards
Where is the optional “doc string” placed when documenting a Clojure function?
Between the function name and the parameter sequence.
How are doc strings typically formatted?
As a multi line string, the first line being a summary and the remaining lines providing details.
What Clojure form displays the documentation of a specified function?
(doc myFunc)
What are Codox, Marginalia, and Cadastre?
These are third party tools that prepare indexed, web-based documentation from documented Clojure source code.
What is a function’s arity?
It is the number of parameters that function accepts.
Arity overloading occurs by defining separate versions of the same function that take a different of arguments.
False, arity overloading occurs within the same function definition.
Different arities of the same function can call one another.
True.
The following is a valid example of arity overloading.
(defn fname
[arg1] (expression1)
[arg1 arg2] (expression2))
False:
(defn fname
([arg1] (expression1))
([arg1 arg2] (expression2)))
Functions in Clojure may be passed as parameters without any special syntax.
True.
What does Clojure’s map function do?
It applies a specified function argument to each element of an associated sequence.
What does Clojure’s reduce function do?
Applies a specified function parameter to all elements of a sequence in order to produce a single value.
What does the following return?
(reduce + [1 2 3])
6
What does the following return?
(reduce + 1 [1 2 3])
7
What does Clojure’s filter function do?
Applies a specified function parameter to all elements of a sequence in order to extract elements that match the filter.
Must the function passed to Clojure’s filter return a Boolean?
No.
How can a function return another function?
By placing the definition of the function to return at the end of the outer function definition.
What is a closure in Clojure?
These are partially instantiated functions that store variables in the scope of the outer function.
What do Clojure’s “first” and “rest” functions do?
These return the first element of a specified sequence, or all but the first element of a specified sequence, respectively.
Clojure’s “cons” function constructs a new list/vector by prepending the item to the existing sequence.
False, it always constructs a new list by prepending the item to the existing sequence.
What does Clojure’s “take” and “drop” functions do?
These return a list of the first n elements of the sequence, and return a list with the first n elements of the sequenced removed, respectively.
“cons”, “take”, and “drop” always return a list, rather than a vector.
True.
Which Clojure function extracts the elements from a sequence until the specified function is no longer true (truthy)?
take-while
Which Clojure function removes the elements from a sequence until the specified function is no longer true (truthy)?
drop-while
What is returned by the following?
(take-while #(< % 5) [3 6 1 4 7])
(3)