Clojure Functions (L17) Flashcards

1
Q

Where is the optional “doc string” placed when documenting a Clojure function?

A

Between the function name and the parameter sequence.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How are doc strings typically formatted?

A

As a multi line string, the first line being a summary and the remaining lines providing details.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What Clojure form displays the documentation of a specified function?

A

(doc myFunc)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are Codox, Marginalia, and Cadastre?

A

These are third party tools that prepare indexed, web-based documentation from documented Clojure source code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a function’s arity?

A

It is the number of parameters that function accepts.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Arity overloading occurs by defining separate versions of the same function that take a different of arguments.

A

False, arity overloading occurs within the same function definition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Different arities of the same function can call one another.

A

True.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The following is a valid example of arity overloading.
(defn fname
[arg1] (expression1)
[arg1 arg2] (expression2))

A

False:
(defn fname
([arg1] (expression1))
([arg1 arg2] (expression2)))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Functions in Clojure may be passed as parameters without any special syntax.

A

True.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does Clojure’s map function do?

A

It applies a specified function argument to each element of an associated sequence.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does Clojure’s reduce function do?

A

Applies a specified function parameter to all elements of a sequence in order to produce a single value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the following return?
(reduce + [1 2 3])

A

6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the following return?
(reduce + 1 [1 2 3])

A

7

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does Clojure’s filter function do?

A

Applies a specified function parameter to all elements of a sequence in order to extract elements that match the filter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Must the function passed to Clojure’s filter return a Boolean?

A

No.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How can a function return another function?

A

By placing the definition of the function to return at the end of the outer function definition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is a closure in Clojure?

A

These are partially instantiated functions that store variables in the scope of the outer function.

18
Q

What do Clojure’s “first” and “rest” functions do?

A

These return the first element of a specified sequence, or all but the first element of a specified sequence, respectively.

19
Q

Clojure’s “cons” function constructs a new list/vector by prepending the item to the existing sequence.

A

False, it always constructs a new list by prepending the item to the existing sequence.

20
Q

What does Clojure’s “take” and “drop” functions do?

A

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.

21
Q

“cons”, “take”, and “drop” always return a list, rather than a vector.

A

True.

22
Q

Which Clojure function extracts the elements from a sequence until the specified function is no longer true (truthy)?

A

take-while

23
Q

Which Clojure function removes the elements from a sequence until the specified function is no longer true (truthy)?

A

drop-while

24
Q

What is returned by the following?
(take-while #(< % 5) [3 6 1 4 7])

A

(3)

25
Q

What is returned by the following?
(drop-while #(< % 5) [3 6 1 4 7])

A

(6 1 4 7)

26
Q

(some func sequence) returns true or false depending on whether func specifies at least one element in the sequence.

A

False, it returns the first truthy value or nil
otherwise.

27
Q

(concat s1 s2) always returns a list containing s1 and s2’s elements, regardless of the sequence types of s1 and s2.

A

True.

28
Q

The following is valid and returns a sorted vector:
(sort [4 5 2 7])

A

False, sort always returns a list.

29
Q

The following is valid and returns a sorted list:
(sort [“d” “ab” “a”])

A

True.

30
Q

The following is valid and returns a sorted list:
(sort [“d” “ab” 1 “a”])

A

False, you can’t mix types.

31
Q

Does the following return a list sorted in increasing or decreasing order?
(sort #(> %1 %2) [3 1 4 6 7])

A

Decreasing.

32
Q

Does the following return a list sorted in increasing or decreasing order?
(sort #(< %1 %2) [3 1 4 6 7])

A

Increasing.

33
Q

Does the following return a list sorted in increasing or decreasing order?
(sort #(< %2 %1) [3 1 4 6 7])

A

Decreasing.

34
Q

Does the following return a list sorted in increasing or decreasing order?
(sort #(> %2 %1) [3 1 4 6 7])

A

Increasing.

35
Q

What does the following return?
(into [ ] ‘(1 2 3 4))

A

[1 2 3 4]

36
Q

What does the following return?
(into ‘( ) [1 2 3 4])

A

(4 3 2 1)

37
Q

What does the following return?
(into ( ) [1 2 3 4])

A

(4 3 2 1)

38
Q

Are the following lines valid?
(into ‘( ) ‘(1 2 3))
(into ( ) ‘(1 2 3))

A

Yes - both return (3 2 1).

39
Q

Is the following line valid?
(into [ ] [1 2 3])

A

Yes.

40
Q

The following lines take the same speed to execute:
(first (range 1000000))
(last (range 1000000))

A

False - Clojure employs lazy sequences, meaning the first line is nearly instant (much faster).