Last active
December 30, 2015 10:29
-
-
Save yangwansu/7815954 to your computer and use it in GitHub Desktop.
http://www.slipp.net/wiki/pages/viewpage.action?pageId=16711765
에서 계산기에 대해 여러가지로 작성해보았습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(use ['clojure.string]) | |
(defn t [actual expected] | |
(if (= actual expected) | |
(do | |
(println 'sucess) | |
true ) | |
(do | |
(println (str 'fail!! " actual: " actual " expected " expected )) | |
false))) | |
(t 1 1) | |
(t 1 0) | |
(#(reduce %1 %2) + '(1 2 3 4 5)) | |
(#(reduce %1 %2) + (map read-string (split "1,2,3" #","))) | |
(defn add [text] (#(reduce %1 %2) + (map #(if (empty? %) 0 (read-string %)) (split text #",") ))) | |
(map #(if (empty? %) 0 (read-string %)) (split "1,2" #",")) | |
(add "") | |
(add "1,2,4") | |
(add "1,2") | |
(defn add2 | |
([form] | |
(#(reduce %1 %2) + (map #(if (empty? %) 0 (read-string %)) (split form #",") ))) | |
([text sep] | |
(#(reduce %1 %2) + (map #(if (empty? %) 0 (read-string %)) (split text (re-pattern sep)) ))) | |
) | |
(add2 "" ",") | |
(add2 "1,2,4" ",") | |
(add2 "1,2" ",") | |
(defn add3 | |
([form] | |
(cond | |
(= "" form) 0 | |
(.startsWith form "//") | |
(let [matchs (re-find (re-pattern "^//(.)\n(.*)") form)] | |
(add3 (last matchs) (second matchs) ) ) | |
:else (add3 form ",") )) | |
([text sep] | |
(#(reduce %1 %2) + (map #(if (empty? %) 0 (read-string %)) (split text (re-pattern sep)) ))) | |
) | |
(add3 "") | |
(add3 "1,2,4") | |
(add3 "1,2") | |
(add3 "//:\n1") | |
(add3 "//:\n1:2:3") | |
(defn add4 | |
[form] | |
(defn _reduce [text sep] | |
(#(reduce %1 %2) + (map #(if (empty? %) 0 (read-string %)) (split text (re-pattern sep)) ))) | |
(cond | |
(= "" form) 0 | |
(.startsWith form "//") | |
(let [matchs (re-find (re-pattern "^//(.)\n(.*)") form)] | |
(_reduce (last matchs) (second matchs) ) ) | |
:else (_reduce form ",") )) | |
(add4 "") | |
(add4 "1,2,4") | |
(add4 "1,2") | |
(add4 "//:\n1") | |
(add4 "//:\n1:2:3") | |
(defn add5 | |
[form] | |
(defn _apply [text sep] | |
(#(apply %1 %2) + (map #(if (empty? %) 0 (read-string %)) (split text (re-pattern sep)) ))) | |
(cond | |
(= "" form) 0 | |
(.startsWith form "//") | |
(let [matchs (re-find (re-pattern "^//(.)\n(.*)") form)] | |
(_apply (last matchs) (second matchs) ) ) | |
:else (_apply form ",") )) | |
(add5 "") | |
(add5 "1,2,4") | |
(add5 "1,2") | |
(add5 "//:\n1") | |
(add5 "//:\n1:2:3") | |
(_apply "1,2,3" ",") ;;헛 | |
(defn add6 | |
[form] | |
(let [_apply (fn [text sep] (#(apply %1 %2) + (map #(if (empty? %) 0 (read-string %)) (split text (re-pattern sep)) )))] | |
(cond (= "" form) 0 | |
(.startsWith form "//") (let [matchs (re-find (re-pattern "^//(.)\n(.*)") form)] (_apply (last matchs) (second matchs) ) ) | |
:else (_apply form ",") )) | |
) | |
(add6 "") | |
(add6 "1,2,4") | |
(add6 "1,2") | |
(add6 "//:\n1") | |
(add6 "//:\n1:2:3") | |
(re-matches #"^//(.)\n(.*)" "//:\n1:2:3") | |
(re-find (re-pattern #"^//(.)\n(.*)") "//:\n1:2:3") | |
(defn add7 | |
[form] | |
(let [_apply (fn [text sep] (#(apply %1 %2) + (map #(if (empty? %) 0 (read-string %)) (split text (re-pattern sep)) )))] | |
(cond (= "" form) 0 | |
(.startsWith form "//") (let [matchs (re-matches #"^//(.)\n(.*)" form)] (_apply (last matchs) (second matchs) ) ) | |
:else (_apply form ",") )) | |
) | |
(add7 "") | |
(add7 "1,2,4") | |
(add7 "1,2") | |
(add7 "//:\n1") | |
(add7 "//:\n1:2:3") | |
(re-matcher #"^//(.)\n(.*)" "//:\n1:2:3") | |
(class (re-matcher #"^//(.)\n(.*)" "//:\n1:2:3")) | |
(.find (re-matcher #"^//(.)\n(.*)" "//:\n1:2:3")) | |
(-> (re-matcher #"^//(.)\n(.*)" "//:\n1:2:3").find) | |
(def p (re-pattern #"^//(.)\n(.*)")) | |
(-> (re-matcher p "//:\n1:2:3").find) | |
(def validate #(-> (re-matcher p %).find)) | |
(validate "//:\n1:2:3") | |
(def validate #(let [_p (re-pattern #"^//(.)\n(.*)")] (-> (re-matcher _p %).find))) | |
(validate "//:\n1:2:3") | |
(defn parse [form] | |
(if (validate form) | |
(let [matches (re-matches p form) | |
sep (nth matches 1) | |
text (nth matches 2)] | |
(map #(read-string %) (split text (re-pattern sep)))))) | |
(parse "//:\n1:2:3") | |
(apply + (parse "//:\n1:2:3")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment