2016년 11월 20일 일요일

[clojure-docs]comp



comp

comp함수는 함수 여러개를 같이 실행시키게 되는 것이다.
((comp str +) 8 8 8)   ;;=> "24"
기억해야 할 것은 오른쪽에서 왼쪽으로 실행한다는 것이다 comp를 써서 문자 속성 값을 얻는 방법
(def character
  {:name "Smooches McCutes"
   :attributes {:intelligence 10 :strength 4 :dexterity 5}})

(def c-int (comp :intelligence :attributes))
(def c-str (comp :strength :attributes))
(def c-dex (comp :dexterity :attributes))

(c-int character)
10
(c-str character)
4
(c-dex character)
5
이게 뭐야? 왜 저런 값이 나오는 거지? 라고 생각할 수 있다. 자 comp가 어디서부터 실행된다고? 오른쪽에서 왼쪽으로
(#(:intelligence (:attributes %)) character)
10
(#(:strength (:attributes %)) character)
4
(#(:dexterity (:attributes %)) character)
5
이렇게 되는 것과 같다. 어떻게 이렇게 되는 건가?
(defn two-comp
  [f g]
  (fn [& args]
    (f (apply g args))))

댓글 없음:

댓글 쓰기