Clojure - Accessing Individual Fields



Individual fields of the structure can be accessed by accessing the keys along with the structure object.

Syntax

Following is the syntax.

:key structure-name

Parameters − ‘key’ is the keyvalue in the structure. ‘structure-name’ is the structure which is the respective key.

Return Value − The value associated with the key will be returned. An example on how this is used is shown in the following program.

Example

(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (defstruct Employee :EmployeeName :Employeeid)
   (def emp (struct-map Employee :EmployeeName "John" :Employeeid 1))
   (println (:Employeeid emp))
   (println (:EmployeeName emp)))
(Example)

Output

The above program produces the following output.

1
John
clojure_structmaps.htm
Advertisements