【心得】
read-string
的特殊用法,看了官方文件,像是有個內建的規則會自己解讀餵入的 string,但怎麼解讀還不知道【今日湯底】
You ask a small girl,"How old are you?" She always says, "x years old", where x is a random number between 0 and 9.
Write a program that returns the girl's age (0-9) as an integer.
Assume the test input string is always a valid string. For example, the test input may be "1 year old" or "5 years old". The first character in the string is always a number.
(必須通過以下測試)
(ns clojure.girl-age-test
(:require [clojure.test :refer :all]
[clojure.girl-age :refer :all]))
(deftest
how-old-test
(testing "Testing how old"
(is (= (how-old "1 year old") 1))
(is (= (how-old "6 years old") 6))))
【我的答案】
(ns clojure.girl-age)
(defn how-old
[her-old]
(Integer/parseInt (first (clojure.string/split her-old #" ")))
)
思路:
【其他人的答案】
(ns clojure.girl-age)
(defn how-old
[her-old]
(-> her-old
first
str
Integer/parseInt))
(ns clojure.girl-age)
(def how-old read-string)