Clojure - struct



This function is used to define a structure object of the type, which is created by the defstruct operation.

Syntax

Following is the syntax.

(struct structname values)

Parameters − ‘structname’ is the name to be given to the structure. ‘values’ is the values which needs to be assigned to the key values of the structure.

Return Value − Returns a struct object with the values mapped to the keys of the structure.

Example

An example on how this is used is shown in the following program.

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

Output

The above program produces the following output.

{:EmployeeName John, :Employeeid 1}

It can be clearly seen that the values supplied in the struct function was assigned to the keys for the Employee object.

clojure_structmaps.htm
Advertisements