In Clojure, the keys
method returns a sequence of the map's keys in the same order as they are defined.
The syntax of this method is as follows:
s = (keys input_map)
This function accepts as a parameter the map to be processed input_map
.
This function returns a sequence s
of the input_map
keys.
Let's look at the following examples to understand this method better:
The example below demonstrates how to collect the keys of a map hosting the information of an employee:
(def employee {:name "Adrian" :age 30 :salary 30})(print "The employee hashmap's info:" employee )(print "\nThe keys of the employee hashmap's info are:" (keys employee))
Let's look at the code widget above:
Line 1: Define a map containing employee information and assign it to a value named employee.
Line 2: Display a message showing the map already defined.
Line 3: Invoke the keys
method to extract the map keys and print them out.
The example below shows how to apply the keys
method on an empty map:
(def employee {})(print "The employee hashmap's info:" employee )(print "\nThe keys of the employee hashmap's info are:" (keys employee))
Let's explain the code widget above:
Line 1: We define an empty map.
Line 2: We display a message showing the map previously defined.
Line 3: We invoke the keys
method to extract the map keys and print them out. We'll notice that a nil
(null or absence of value) is returned since the specified map is empty.
Free Resources