Destructuring in Clojure(1)

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

I will summarize the Destrucurting of data, which is important in using Clojure. For sample code, we refer to “Destructuring in Clojure”. The first step is to split the data from the vector data as follows.

(def my-line [[5 10] [10 20]]) 
(let [p1 (first my-line) 
    p2 (second my-line) 
      x1 (first p1) 
      y1 (second p1) 
      x2 (first p2) 
      y2 (second p2)] 
 (println "Line from (" x1 "," y1 ") to (" x2 ", " y2 ")")) 
;= "Line from ( 5 , 10 ) to ( 10 , 20 )"

This can be expressed by Desctructuring using the let statement as follows.

;= Using the same vector as above 
(let [[p1 p2] my-line 
    [x1 y1] p1 
      [x2 y2] p2] 
 (println "Line from (" x1 "," y1 ") to (" x2 ", " y2 ")")) 
;= "Line from ( 5 , 10 ) to ( 10 , 20 )"

Destructuring other than vector data is as follows.

(def my-vector [1 2 3]) 
(def my-list '(1 2 3)) 
(def my-string "abc") 

;= It should come as no surprise that this will print out 1 2 3 
(let [[x y z] my-vector] 
   (println x y z)) 
;= 1 2 3 

;= We can also use a similar technique to destructure a list 
(let [[x y z] my-list] 
   (println x y z)) 
;= 1 2 3 

;= For strings, the elements are destructured by character. 
(let [[x y z] my-string] 
  (println x y z) 
  (map type [x y z])) 
;= a b c 
;= (java.lang.Character java.lang.Character java.lang.Character)

When the object to be destructed is large or small, the following occurs.

(def small-list '(1 2 3)) 
(let [[a b c d e f g] small-list] 
  (println a b c d e f g)) 
;= 1 2 3 nil nil nil nil

(def large-list '(1 2 3 4 5 6 7 8 9 10)) 
(let [[a b c] large-list] 
  (println a b c)) 
;= 1 2 3

If it is small, the remainder will be nil, and if it is large, it will be truncated. You can also specify the value as follows.

(def names ["Michael" "Amber" "Aaron" "Nick" "Earl" "Joe"])

(let [[item1 item2 item3 item4 item5 item6] names] 
  (println item1) 
  (println item2 item3 item4 item5 item6)) 
;= Michael 
;= Amber Aaron Nick Earl Joe

(let [[item1 & remaining] names] 
  (println item1) 
  (apply println remaining)) 
;= Michael 
;= Amber Aaron Nick Earl Joe

(let [[item1 _ item3 _ item5 _] names] 
  (println "Odd names:" item1 item3 item5)) 
;= Odd names: Michael Aaron Earl

(let [[item1 :as all] names] 
  (println "The first name from" all "is" item1)) 
;= The first name from [Michael Amber Aaron Nick Earl Joe] is Michael

(def word "Clojure") 
(let [[x & remaining :as all] word] 
  (apply prn [x remaining all])) 
;= C (l o j u r e) "Clojure"

It can also be applied to arbitrary nested data.

(def my-line [[5 10] [10 20]])

(let [[[x1 y1][x2 y2]] my-line] 
  (println "Line from (" x1 "," y1 ") to (" x2 ", " y2 ")")) 
;= "Line from ( 5 , 10 ) to ( 10 , 20 )"

(let [[[a b :as group1] [c d :as group2]] my-line] 
  (println a b group1) (println c d group2)) 
;= 5 10 [5 10] ;= 10 20 [10 20]

I will continue to discuss Destructuring in the next article.

コメント

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