Let’s start with Clojure (2)Repl and basic programming

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 have covered everything from setting up the Clojure environment to generating template files. In this article, we will discuss repl and immutable data, which is one of the features of Clojure.

First of all, open spacemacs, and press the keys in the order of “SPC”, “f”, and “t” to display the file structure on the left side, and open src/my-app/core.clj in the folder of my-app, which is the template we created last time. Next, press the keys “SPC”, “m”, “s”, and “i” in this order, and the repl server will start up. If you wait for a while and you see [nreapl]Direct connection to localhost:xxxx established in the footer bar of spacemacs, the REPL will start.

In the case of Clojure, the server called nrepl starts up, evaluates the code, and returns the result. In the case of Clojure, the server nrepl is launched, where the code is evaluated and the result is returned. In the case of Clojure, a server called nrepl is launched, where the code is evaluated and the result is returned. Since Clojure is a functional language that consists entirely of functions, the target of the evaluation is the function, and the evaluation result is the output result of the input to the function.

The flow of development using REPL in Clojure is as follows

Start nREPL.

Connect to nREPL from the editor.

Move to the namespace you want to develop on the REPL (the file starting with (ns ~) in the previous template).

Select a function from the code in the namespace and send it to REPL.

5. Send the evaluation result (output result) to nREPL and display it next to the function selected in 4.

If there is no mistake in the result, save it and evaluate the next function.

The result is as follows. When developing the code, since the functions are evaluated in order from the beginning, the debugging check will be completed, and when the last main function is reached, all the operations will have been checked. The above mechanism allows for fast development with little backtracking.

Now, nREPL starts up on Spacemacs and returns to the state where the code in src/my-app/core.clj can be edited.

Since you have selected vim as input, you need to set it to input mode. Move the cursor to the position where you want to type, and press the i or a key to change to input mode. Now all you have to do is type in the code. To get out of the input mode, just press the “ESC” key.

By moving the cursor to the right side of the right parenthesis and pressing “CTRL”, “c”, and “e” at the same time, the function wrapped in the left parenthesis will be sent to nREPL, and the evaluation result will be returned and displayed. If the evaluation is OK, the output result “3” will be displayed, and if there is some problem in the code of the function, an error message will be displayed. In that case, look at the error message, correct the code, and evaluate again.

One of the features of Clojure is that “data is immutable”. For example, in the case of a language with mutable data structure such as ruby

irb(main):001:0> foo = [0, 1, 2, 3, 4, 5, 6, 7, 8]=> [0, 1, 2, 3, 4, 5, 6, 7, 8]irb(main):002:0> foo[6] = 9
=> 9
irb(main):003:0> foo
=> [0, 1, 2, 3, 4, 5, 9, 7, 8] # fooが変更されている

In the case of Clojure, the initial variable foo changes with processing, whereas in the case of

user=> (def foo [0 1 2 3 4 5 6 7 8])
#’user/foo
user=> (assoc foo 6 9)
[0 1 2 3 4 5 9 7 8]user=> (println foo)
[0 1 2 3 4 5 6 7 8] ; fooは元のまま
nil

The variable foo itself does not change even if it is processed, as in The fact that the data is mutable means that when the variable is accessed by multiple modules, the data will differ depending on the order of access.

In the case of Clojure, people who are used to coding mutable data will have to change their way of thinking because the code assumes that the data does not change, but if you get used to immutable mode, you can benefit from controlling variables.

So what do we do when we need to change the state? Clojure provides STM (Software Transactional Memory), a mechanism similar to the transactional part of a database, which uses variables represented by functions such as ref and atom.

user=> (def my-atom (atom 0))
#'user/my-atom
user=> @my-atom
0
user=> (swap! my-atom inc)
1
user=> @my-atom
1

The data changes by defining my-atom as a variable that can change and adding processing to it. In the case of the Web UI, this kind of data structure is necessary because the processing needs to change depending on the previous “state” (for example, the subsequent processing changes depending on whether the box is checked or not).

In the next article, I would like to discuss the data structure in more detail.

コメント

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