Getting started with Clojure InstallからHello worldくらいまで
Clojure is a robust, practical, and fast programming language with a set of useful features that together form a simple, coherent, and powerful tool.
Installation
https://clojure.org/guides/getting_started にてわかりやすく説明してくれている。Macであれば Homebrewでinstallできる。
brew install clojure
clojure: Java 1.8+ is required to install this formula.
といわれるかもしれません。その場合はエラーメッセージの通り素直にinstallしましょう。
brew cask install adoptopenjdk
Learning
https://clojure.org/guides/learn/syntax にて基本的な文法を抑えることができます。また、このガイドでは章末問題みたいなものがあり、それを解くことで理解を確認することができます。親切ですね、楽しいですね。章末問題を解く過程の中で周辺の基本のキを抑えておきます。
Syntaxを学ぶ
1. Using the REPL, compute the sum of 7654 and 1234.
user=> (+ 7654 1234)
8888
2. Rewrite the following algebraic expression as a Clojure expression: ( 7 + 3 * 4 + 5 ) / 10.
user=> (/ (+ 7 (* 3 4) 5) 10)
12/5
3. Using REPL documentation functions, find the documentation for the rem and mod functions. Compare the results of the provided expressions based on the documentation.
The doc function displays the documentation for any function.
なので、doc functionを実行することで見てみる。まずはrem
。
user=> (doc rem)
-------------------------
clojure.core/rem
([num div])
remainder of dividing numerator by denominator.
nil
次はmod
。
user=> (doc mod)
-------------------------
clojure.core/mod
([num div])
Modulus of num and div. Truncates toward negative infinity.
nil
4. Using find-doc, find the function that prints the stack trace of the most recent REPL exception.
You can also widen your search to include the docstrings themselves with find-doc
ということでfind-doc
functionを実行する
user=> (find-doc "print")
-------------------------
clojure.spec.gen.alpha/any-printable
([& args])
Fn returning clojure.test.check.generators/any-printable
-------------------------
clojure.spec.gen.alpha/simple-type-printable
([& args])
Fn returning clojure.test.check.generators/simple-type-printable
(omit)
Functionsを学ぶ
Clojureは関数型言語(functional language)ということでFunctionsについてもちろんトピックとしてあげられています。
Hello World
Functionを定義してHello Worldします
user=> (defn greet [name] (str "Hello, " name) )
# 'user/greet
user=> (greet "World")
"Hello, World"
この例では、defn
で関数定義の宣言、greet
が関数名、name
が引数、`(str "Hello, " name) が関数内のボディです。
1) Define a function greet that takes no arguments and prints "Hello". Replace the with the implementation: (defn greet [] )
user=> (defn greet [] (str "Hello"))
# 'user/greet
user=> (greet)
"Hello"
これは、Hello Worldでやったとおりですね。no argumentsにするためには、引数部分を[]
にすればok
2) Redefine greet using def, first with the fn special form and then with the #() reader macro.
fn
は無名関数(Anonymous function)を定義するsyntax、def
によってそれをgreet
という名前にbindします
user=> (def greet (fn [] (str "Hello")))
# 'user/greet
user=> (greet)
"Hello"
次に #()
を用いるパターンです。
There is a shorter form for the fn anonymous function syntax implemented in the Clojure reader: #().
https://clojure.org/guides/learn/functions#_anonymous_function_syntax
ということで、fn
をさらに短縮して書ける方式です
user=> (def greet #(str "Hello"))
# 'user/greet
user=> (greet)
"Hello"
その他もこの章では合計13問用意されており、一通りやることで関数定義についても知ることができます。
Atoms
GOTO 2018 • Functional Programming in 40 Minutes • Russ Olsenという関数型プログラミングとは何なのかというトークの中で、Immutableな世界と現実のmutableな世界との橋となる概念がAtomと紹介されていました(とリスニングしました)。Atomsについても次のURLで解説されています。
Atoms provide a way to manage shared, synchronous, independent state.
開発環境
いまどきのClojureのはじめかたという記事を参照させていただいてEmacs + CIDERという組み合わせがメジャーだという話を拝見しました。「なるほど、とはいえVSCodeとかでねぇかな」ということで探したところ、VSCodeもExtensionが提供されてるようです。
VSCode だと Calva もあります!https://t.co/2eYcBLPyhw
— 飯塚将志@にびいろClojure (@uochan) September 4, 2019
開発支援も受けているので、こちらの方がより活発に開発が進むと思われますhttps://t.co/fHPd1sZSro
とのことで、Calvaというものも良さそうだそうです。
Endrole
非常に基本のキになってしまいましたが、「おっ俺も触ってみるか」くらいのきっかけになればいいなくらいのきもちです。