Let’s start with Clojure (3) data structure

Web Technology   Digital Transformation Technology Artificial Intelligence Technology   Natural Language Processing Technology   Semantic Web Technology   Deep Learning Technology   Online Learning & Reinforcement Learning Technology  Chatbot and Q&A Technology   User Interface Technology   Knowledge Information Processing Technology   Reasoning Technology  Clojure Programming

In the previous article, we discussed the features of Clojure, such as REPL and immutable data. This time, we will discuss data structures.

In JavaScript, there are two types of data: primitive types, which are single pieces of data such as strings, numbers, and Booleans; arrays, which are a combination of multiple primitive types enclosed in []; and object types, which are a set of name-value pairs enclosed in ({}).

In Python, there is the list type, which is data enclosed in [] and separated by a comma (,), the tuple type, which is data enclosed in () and separated by a comma (,), and the dictionary type, which is data enclosed in {} and separated by a comma (,) with the key value and assigned value separated by a colon (:). There are two types of data: list and tuple. (The difference between list type and tuple type is whether it is immutable or not.)

On the other hand, in Clojure, a collection is a complex collection of data (values), and a collection has four types: vector, list, set, and map.

Vectors are data enclosed in []. The order in which they are written is fixed, and the data can be accessed by specifying the number of indices (how many are in a row). “(get [“abc” false 99] 2)→99

A list is data enclosed in parentheses, representing a sequential, concatenated list, where the first element is evaluated as a function. (Code is also a type of data.)

A set represents a set of data in mathematics, where the order of the data is not fixed and there is no duplication of data. Functions that deal with sets, such as clojure.set/difference (to take the difference between sets), clojure.set/intersection (to take the common between sets), and clojure.set/union (to take the or of sets) can be applied.

A map is a set of key-value pairs enclosed in {}, which can be represented as {“Fred” 1400, “Bob” 1240, “Angela” 1024} or {:Fred 1400 :Bob 1240 :Angela 1024}. To access the internal data, for example, you can use the get function (get {“Fred” 1400, “Bob” 1240, “Angela” 1024} “Angela”)→1024.

A data structure unique to Clojure that combines these two is edn (Extensible Data Notation), which is a kind of data notation like JSON or YAML, and is a highly extensible data structure that can represent a variety of data. It is used in leiningen configuration files, integrant configuration files used in web applications, and as a data format in DATOMIC, a proprietary database, and is an inseparable part of Clojure.

For example, the following xml data is an example of concrete data notation

        <binding name='s'>
				<uri>http://learningsparql.com/ns/data#CA</uri>
			</binding>
			<binding name='p'>
				<uri>http://www.w3.org/2000/01/rdf-schema#label</uri>
			</binding>
			<binding name='o'>
				<literal>California</literal>
	</binding>

In JSON, it is represented as follows

"binding": [
{
"@name": "s",
"uri": "http://learningsparql.com/ns/data#CA"
},
{
"@name": "p",
"uri": "http://www.w3.org/2000/01/rdf-schema#label"
},
{
"@name": "o",
"literal": "California"
}
]

In edn, it looks like this

[{:tag :binding,
:attrs {:name "s"},
:content
[{:tag :uri,
:attrs nil,
:content ["http://learningsparql.com/ns/data#CA"]}]}
{:tag :binding,
:attrs {:name "p"},
:content
[{:tag :uri,
:attrs nil,
:content ["http://www.w3.org/2000/01/rdf-schema#label"]}]}
{:tag :binding,
:attrs {:name "o"},
:content
[{:tag :literal, :attrs nil, :content ["California"]}]}]}

The data format is not easy for the human eye to understand, but it can be easily accessed by using the clojure.zip function and the get function used to extract data from map data.

In the next article, I would like to take a detour and discuss an article I read about micro nuclear power generation.

コメント

Exit mobile version
タイトルとURLをコピーしました