【心得】
sort-by
【今日湯底】
Simple, given a string of words, return the length of the shortest word(s).
String will never be empty and you do not need to account for different data types.
(必須通過以下測試)
(ns kata.test
(:require [clojure.test :refer :all]
[kata :refer [find_shortest]]))
(deftest basic-tests
(is (= (find_shortest "bitcoin take over the world maybe who knows perhaps") 3))
(is (= (find_shortest "turns out random test cases are easier than writing out basic ones") 3))
(is (= (find_shortest "lets talk about javascript the best language") 3))
(is (= (find_shortest "i want to travel the world writing code one day") 1))
(is (= (find_shortest "Lets all go on holiday somewhere very cold") 2))
)
【我的答案】
(ns kata)
(defn find_shortest [words]
(->> (clojure.string/split words #" ")
(sort-by count)
first
count)
)
思路:
【其他人的答案】
(ns kata
(:require [clojure.string :as str]))
(defn find_shortest [words]
(->> (str/split words #" ")
(map count)
(apply min))
)