본문 바로가기

Programming

Clojure Data Structure

Clojure Data Structure

Clojure 의 데이타는 수, 문자열과 같은 개별 데이타 (Atom)와 이들을 묶어 놓은 Collection 으로 구성된다.
Clojure 의 모든 데이타와 Collection immutable 하며 일단 생성되면 다른 메소드에 의해 변경되지 않는다.

Numbers

Clojure 는 정수, 실수, 분수를 지원한다.

          Example Expression                 Return Value

          (+ 5 8)                                 13

          (/ 5 8)                                  5/8
          (/ 5 8.0)                                0.625
          (def a 3) (= a 3)                      true

 

Strings

          Example Expression                 Return Value
          (print "Hello.")                        Hello.nil

 

Symbol

Symbol 일반적으로 값을 binding 하기 위해 사용된다.

          Example Expression                 Return Value
          (def a "My symbol")               

          a                                     "My symbol"  

          (print a)                              My symbol

 

Keyword

Keyword 에는 직접 값이 binding 되지 않으며 콜론과 함께 사용된다.

          Example Expression                 Return Value
          (def a {:key1 11 :key2 12})               

          a                                     {:key1 11 :key2 12}     

          (:key1 a)                              11

 

Nil

Nil java null 같으며 빈 리스트인 () 를 포함한다.

 

 

Collection

Clojure Collection 에는 List, Vector, Map, Set 이 있다.

 

List

List 객체의 순차적 배열을 나타내는 Collection 이다.

          Example Expression                 Return Value
          (first '(35 7 "Mary" (4 5)))          35
     

          (cons 1 '(1 2 3 4))                   (1 1 2 3 4)        

          (conj  '(1 2 3 4) 1)                   (1 1 2 3 4)       

 

Vector

Vector 는 순차적 정수에 의해 인덱싱 되는 데이타의 Collection 이다.

          Example Expression                 Return Value
          (def a [1 2 3 4])         

          (first a)                                 1
          (rest a)                                 (2 3 4)

          (cons 1 a)                              [1 1 2 3 4]
          (conj a 5)                              [1 2 3 4 5]

 

Set

Set 는 서로 다른 원소로 이루어 지는 데이타의 Collection 이다.

          Example Expression                 Return Value
          (def a (set '(a a b c d))         

          (def b (set "aabcd")         

          a                                     {a c b d}
          b                                     {a b c d}

          (def c  (hash-set "aa" "dd" "bb" "cc"))

          (def d  (sorted-set "aa" "dd" "bb" "cc"))

          c                                     #{"aa" "dd" "bb" "cc"}

          d                                     #{"aa" "bb" "cc" "dd"}

          (contains? c "aa")                 true

          (c "aa")                              "aa"

 

         

 

Map

Map key value 쌍을 생성하는데 사용된다.

          Example Expression                 Return Value
          (def a {"MyMap" 1 "b" 2 "c" 3})         

          (MyMap "a")                          1
          (MyMap "b")                          2

          (MyMap "d")                          nil

 

 

Metadata

Metadata 는 다른 데이타를 기술하는 데이타이다.



 

 

'Programming' 카테고리의 다른 글

Maemo 5 개발환경  (0) 2010.01.09
QT 기반으로 새로 태어나는 Maemo 6  (0) 2010.01.03
Closure in C++  (2) 2009.10.03
안드로이드 플랫폼에서 Clojure 실행  (0) 2009.06.14
Clojure - A modern LISP dialect  (0) 2009.05.22