프로그래밍 Programming
클로져 시작하기 Getting Started with Clojure
문장전달자
2018. 8. 9. 12:54
728x90
클로져를 빨리 실행해보고자 한다면, 먼저 자바가 설치되어 있는지 확인해야 한다. 그리고 Leiningen 프로젝트 관리 도구를 설치해야 한다. OS 내의 패키지 관리자가 아니라, leiningen.org 에서도 권고하듯이 스크립트를 직접 다운로드하여 설치하기를 권고한다.
Trying out the REPL
lein 툴이 일단 설치되었다면, repl 을 다음과 같이 어디서든 실행할 수 있다.
1 2 3 4 5 6 7 8 9 10 11 12 13 | (dominika) ~$ lein repl nREPL server started on port 33137 on host 127.0.0.1 - nrepl://127.0.0.1:33137 REPL-y 0.3.7, nREPL 0.2.12 Clojure 1.8.0 OpenJDK 64-Bit Server VM 10.0.1+10-Ubuntu-3ubuntu1 Docs: (doc function-name-here) (find-doc "part-of-name-here") Source: (source function-name-here) Javadoc: (javadoc java-object-or-class-here) Exit: Control+D or (exit) or (quit) Results: Stored in vars *1, *2, *3, an exception in *e user=> | cs |
"user=>" 라는 프롬프트가 생성되었을 것이다. 다음과 같이 입력해보자.
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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)) Inconceivable! 0 Marvelous! 1 Inconceivable! 2 nil | cs |
Your first project
다음과 같이 첫번째 클로져 프로그램을 만들어보자.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | (dominika) ~$ lein new app my-proj Generating a project called my-proj based on the 'app' template. (dominika) ~$ ls -l total 24 drwxrwxr-x 23 fukaerii fukaerii 4096 Jun 14 16:56 anaconda3 drwxrwxr-x 8 fukaerii fukaerii 4096 Aug 4 17:01 do-clojure-web drwxrwxr-x 4 fukaerii fukaerii 4096 Jun 19 17:37 dominika -rw-rw-r-- 1 fukaerii fukaerii 825 Jun 14 18:04 environment.yaml drwxrwxr-x 6 fukaerii fukaerii 4096 Aug 9 11:58 my-proj -rw-rw-r-- 1 fukaerii fukaerii 72 Jun 19 16:44 Untitled.ipynb (dominika) ~$ cd my-proj (dominika) ~/my-proj$ ls -l total 40 -rw-rw-r-- 1 fukaerii fukaerii 768 Aug 9 11:58 CHANGELOG.md drwxrwxr-x 2 fukaerii fukaerii 4096 Aug 9 11:58 doc -rw-rw-r-- 1 fukaerii fukaerii 11219 Aug 9 11:58 LICENSE -rw-rw-r-- 1 fukaerii fukaerii 361 Aug 9 11:58 project.clj -rw-rw-r-- 1 fukaerii fukaerii 465 Aug 9 11:58 README.md drwxrwxr-x 2 fukaerii fukaerii 4096 Aug 9 11:58 resources drwxrwxr-x 3 fukaerii fukaerii 4096 Aug 9 11:58 src drwxrwxr-x 3 fukaerii fukaerii 4096 Aug 9 11:58 test | cs |
1 2 3 4 5 6 | (dominika) ~/my-proj/src/my_proj$ ls -al total 12 drwxrwxr-x 2 fukaerii fukaerii 4096 Aug 9 11:58 . drwxrwxr-x 3 fukaerii fukaerii 4096 Aug 9 11:58 .. -rw-rw-r-- 1 fukaerii fukaerii 122 Aug 9 11:58 core.clj | cs |
1 2 3 4 5 6 7 8 | (ns my-proj.core (:gen-class)) (defn -main "I don't do a whole lot ... yet." [& args] (println "Hello, World!")) | cs |
my_proj/core.clj 의 and println 함수 실행 결과를 보자. 프로젝트 디렉토리에서 repl (lein repl) 을 실행하면 다음과 같이 결과를 볼 수 있다.
1 2 | (dominika) ~/my-proj$ lein run Hello, World! | cs |
728x90