英和訓練

著作権は原文に属します

CDS - Getting Started with Clojure Clojureを使い始める

This guide covers: このガイドでは以下を扱う

  • prerequisites (such as Leiningen) and installing 必要なもの(Leiningenなど)とインストール
  • running the REPL REPLの実行
  • creating a project プロジェクトの作成
  • interactive development インタラクティブな開発

This work is licensed under a Creative Commons Attribution 3.0 Unported License (includeing images & stylesheets). この作業はCreative Commonsでライセンスされている。The source is available on Github. ソースはGitHubにある。

Overview 概要

Clojure is a wonderfully simple language and you are going to love it. Clojureは素晴らしくシンプルな言語だ。あなたもきっと気に入ることだろう。

To quickly get started, first make sure you've got Java installed. Clojureを使い始めるには、Javaがインストール済みである必要がある。

Then install the Leiningen project management tool. 次に、プロジェクト管理ツールであるLeiningenをインストールする必要がある。

This author (jg) recommends always installing by downloading the script directly (as described in the instructions at leiningen.org), rather than using your OS's package manager. Leiningenをインストールするには、OSのパッケージマネージャを用いるのではなく、(leiningen.orgで説明されているように、)直接スクリプトをダウンロードしてインストールすべきだろう。と、私John Gabrieleは思う。This will ensure that you get the latest lein version 2. こうすれば確実に、最新であるバージョン2のLeiningenをインストールできるからだ。

Clojure programs are typically developed inside their own project directory, and Leiningen manages projects for you. Clojureのプログラムは、典型的には、プロジェクトごとのディレクトリの中に配置される。そして、Leiningenを使って、そのプロジェクトを管理できる。Lein takes care of pulling in dependencies (including Clojure itself), running the REPL, running your program and its tests, packaging your program for destribution, and other administrative tasks. Leiningenが提供する機能は様々であり、(Clojure自体を含む)依存関係の入手や、REPLの実行、あなたのプログラムの実行、テストの実行、配布のためのパッケージング、およびその他の管理作業を担う。Run lein help to see the list of all the tasks in can perform. Leiningen が行える全てのタスクのリストを見るには、lein helpを実行すればよい。

Again, there's no need to "install" Clojure, per se. Lein will take care of fetching it for you. すでに書いたが、Clojure自体をインストールする必要はない。Leiningenが自動的にClojureを取得してくれる。

Trying out the REPL REPLを試す

Although lein facilitates managing your projects, you can also run it on its own (outside of any particular project directory). Leiningenの主機能は、プロジェクトの管理を助けることだが、(どのプロジェクトのディレクトリの中でもない場所で)単にLeiningen自体を実行することもできる。Once you have the lein tool installed, run it from anywhere you like to get a repl: leinツールがインストールできたなら、どのディレクトリにいる時でも、次のようにしてreplを起動できる。

$ lein repl

You should be greeted with a "user=>" prompt. 「user=>」というプロンプトで迎えられるはずだ。Try it out: 次のように色々と試してみよう。

user=> (+ 1 1)
;; ⇒ 2
user=> (distinct [:a :b :a :c :a :d])
;; ⇒ (:a :b :c :d)
user=> (dotimes [i 3]
  #_=>   (println (rand-nth ["Fabulous!" "Marvelous!" "Inconceivable!"])
  #_=>            i))
;; Marvelous! 0
;; Inconceivable! 1
;; Fabulous! 2
;; ⇒ nil

Your first project 最初のプロジェクト

Create your first Clojure program like so: 次のようにしてあなたの最初のプロジェクトを作ってみよう。

lein new app my-proj
cd my-proj
# Have a look at the "-main" function in src/my_proj/core.clj.
# src/my_proj/core.cljにある「-main」関数を見てみてくれ
lein run

and see the output from that println function call in my_proj/core.clj! すると、my_proj/core.cljで呼ばれているprintln関数からの出力が見られる。

Interactive Development インタラクティブな開発

In your project directory, start up a repl (lein repl) and run your -main function to see its output in the repl: プロジェクトのディレクトリの中で、(lein replとして)replを起動し、あなたの-main関数を実行して、replへの出力を見てみよう。

$ lein repl
...
my-proj.core=> (-main)
Hello, World!
nil

(The prompt is now "my-proj.core=>" instead of "user=>" because lein has started the repl in an app project. 今user=>ではなくmy-proj.core=>というプロンプトが表示されているのは、アプリのプロジェクトの中でleinがreplを起動したからだ。More about that ("namespaces") in the topical guides. これ(「名前空間」)についての詳細は、項目別の解説で扱う。)

From elsewhere, open up your my-proj/src/my_proj/core.clj file in your editor. 他のどこかから、使っているエディタでmy-proj/src/my_proj/core.clj ファイルを開こう。Modify the text in that println call. ファイルの内容にあるprintlnの呼び出しのテキストを変更してみてくれ。

Back in the repl, reload your source file and run -main again: replに戻り、次のようにして、ソースファイルをリロードし、再び-mainを実行する。

my-proj.core=> (require 'my-proj.core :reload)
my-proj.core=> (-main)

to see your changes. printlnの引数を変更した結果が反映されているはずだ。

See Also 参考

Other getting started documentation you might find useful: Clojureの始め方については以下のドキュメンテーションがもある。参考になるかもしれない。

  • Clojure Distilled: Introduction to core concepts necessary for woking with Clojure Clojureで作業をするのに必要な中心的概念の紹介
  • A Brief Beginner's Guide to Clojure: contains a bit more overview and background material for learning your way around the landscape. もう少し踏み込んだ概観や背景の論点について、全体像を学べる。
  • Starting Clojure screencast: an extensive getting-started screencast using Eclispse to develop a webapp project. Eclipseを使ってウェブアプリを開発する入門用の充実したスクリーンキャスト

Next Stop 次の項目

Next stop: the basic Clojure language tutorial.

Contributors

John Gabriele jmg3000@gmail.com (original author)

原文: Getting Started with Clojure | Clojure Documentation | Clojure Docs