Let’s start with Clojure (2)Using leiningen

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 Navigation of this blog
Summary

This article describes how to set up a development environment to get started with Clojure.

JVM

First of all, we need to set up the development environment.

Since Clojure runs on JVM, we need to set up JVM.

(1) Do you have JAVA installed on your PC? If so, check the version.

On a mac, enter “java –version” in the terminal, and type

java 11.0.1 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
If you get a message like “Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)”, you have JAVA. In this case, the version is 11.0 (FESS recommends JAVA11).

(2) If JAVA is not installed (or if the version is old), install JAVA.

homebrew and text editor(spacemacs)

Next, prepare an editor. LightTable is recommended for easy use, but emacs is more convenient for serious use. emacs is a highly extensible editor with various settings, but it requires a great deal of skill to configure all of them. You can find more information on how to set up spacemacs on its official page, but I will describe it here. First, install homebrew for mac. Go to the official page and click

Run the following code in a terminal as written.

/bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)”

To check if the installation is complete, type “brew -v” in the terminal and the version will be displayed.

Next, use that brew to install emacs-plus

brew tap d12frosted/emacs-plus
# to install Emacs 26
brew install emacs-plus
# or to install Emacs 27
brew install emacs-plus@27 --with-spacemacs-icon
# or to install Emacs 28
brew install emacs-plus@28 --with-spacemacs-icon
brew link emacs-plus

Next, clone spacemacs from GitHub and place it as ~/.emacs.d. (If the git command doesn’t work here, install it beforehand using “brew install git”)

git clone https://github.com/syl20bnr/spacemacs ~/.emacs.d

If you enter “emacs” on the command line, the following screen will come up and you are done.

At the initial startup, it asks you what you want to do with some basic settings. First of all, it will ask you to select the key operation mode: “What is your preferred editing style? If you’re not used to the special keystrokes of vim, it’s safer to set it to emacs, but for the rest of the explanation, I’ll use vim because I’m using vim mode.

Next, select the spacemacs configuration “What distribution of spacemacs would you like to start with?” You can set this to spacemacs, which is the standard configuration.

Next, select the completion framework “What type of completion framework do you want?

There are many useful key bindings for spacemacs. There are many useful keybindings for spacemacs, which are compactly summarized in @my4’s page and Nijatahandou’s page. (Key bindings for vim)

The first thing you need to do after starting up spacemacs is to set the configuration of the dot file. The first thing you need to do after starting up spacemacs is to set up the configuration of the dot file, which is described in the .spacemacs file, and by pressing “SPC”, “f”, “e”, and “d” in that order (“SPC” is the space bar), you can get a shortcut to the following configuration page.

If you want to use clojure, configure it in “dotspacemacs-configuration-layers”, which can also be seen above (remove the commented-out “;;”). When the configuration is finished, close it once (press “SPC”, “q”, and “q” in that order).

Leiningen

We need to install “Leiningen” as a tool to run Clojure. leiningen is a build tool for Clojure, and there are several other ways to build Clojure, but leiningen is the most widely used one, so it is an appropriate one to install first. mac For macs, you can use the aforementioned homebrew to “brew install leiningen” in one shot, while for windows, it takes a little more time and effort through self-install.

Once you have leiningen installed, you are ready to build Clojure. First, enter “lein new app my-app (name of the new app)” under any folder using a terminal, and the Clojure template will be generated with the following structure.

├── CHANGELOG.md
├── LICENSE
├── README.md
├── doc
│   └── intro.md
├── project.clj
├── resources
├── src
│   └── my_app
│   └── core.clj
└── test
└── my_app
└── core_test.clj

The project.clj file is the configuration file of the application and contains the following description.

(defproject my-app "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
:url "https://www.eclipse.org/legal/epl-2.0/"}
:dependencies [[org.clojure/clojure "1.10.1"]]
:main ^:skip-aot my-app.core
:target-path "target/%s"
:profiles {:uberjar {:aot :all
:jvm-opts ["-Dclojure.compiler.direct-linking=true"]}})

この中で、最初に変更する部分が

:dependencies [[org.clojure/clojure "1.10.1"]]

section. This is where you will add the libraries you want to use.

The source code should be written in the file src/my-app/core.clj. The default description is as follows.

(ns my-app.core
(:gen-class))
(defn -main
"I don't do a whole lot … yet."
[& args]
(println "Hello, World!"))

As shown above, the Clojure code has a syntax (called S-expression) enclosed in parentheses (). The syntax inside the parentheses is that the function comes first, followed by the variables to be assigned to the function (prefix notation (Polish notation)). For example, the function of addition (+), which represents 1+1 (middle notation), is expressed as (+ 1 1) in Clojure (LISP) syntax; (ns ~), which appears in the template code, represents the namespace of the file, and the function (defn-main ~) represents the main function.

To execute this file, run “lein run” at the top of the my-app folder using a terminal to execute the main function and display “Hello, World! Also, the executable file (jar file) in the java environment is generated by “lein uberjar”.

In the next article, we will discuss the actual repl development using spacemacs.

コメント

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