2016년 11월 23일 수요일

[clojure-docs]assoc-in



assoc-in

assoc-in은 정말 어려운 함수이다. 정말 처음엔 뭐 이런게 있나 싶다.
(assoc-in {} [:cookie :monster :vocals] "Finntroll")
{:cookie {:monster {:vocals "Finntroll"}}}
이것은 잘 보자. {}안에 :cookie 안에 :monster 안에 :vocals 안에 "Finntroll"을 넣는 것이다. 그렇다면 이걸보자. 내가 임의로 비슷한 함수를 만들었다.
((fn [new-board [p1 p2] neighbor] (assoc-in new-board [p1 :connections p2] neighbor)) {} [1 3] 4  )
{1 {:connections {3 4}}}
보면 assoc-in은 new-board안에 순서대로 넣는 것이다. {} 안에 1 그리고 그 1안에 :connections 그리고 그 안에 3 그리고 그 값으로 4를 넣은 것이다. 정리하면 assoc의 매개변수는 순서대로 1. 값을 들어갈 곳 2. 키값의 덩어리들 3. 키값 안에 들어가 숨어있을 값 좀 더 이해를 위해 get-in을 사용해보자.
(get-in {1 {:connections {3 4}}} [1])
{:connections {3 4}}

(get-in {1 {:connections {3 4}}} [1 :connections])
{3 4}

댓글 없음:

댓글 쓰기