Write a function which duplicates each element of a sequence.
;; 32. Duplicate a Sequence
;; Write a function which duplicates each element of a sequence.
mapcat #(vector % %)
mapcat #(list % %)
### mapcat
user> (mapcat #(list % %) [1 2 3])
(1 1 2 2 3 3)
user> (doc mapcat)
clojure.core/mapcat
([f & colls])
Returns the result of applying concat to the result of applying map
to f and colls. Thus function f should return a collection.
user=> (source mapcat)
(defn mapcat
"Returns the result of applying concat to the result of applying map
to f and colls. Thus function f should return a collection."
{:added "1.0"}
[f & colls]
(apply concat (apply map f colls)))
nil
user=> (map #(list % %) [1 2 3])
((1 1) (2 2) (3 3))
user=> (concat (list '(1 1) '(2 2) '(3 3)))
((1 1) (2 2) (3 3))
user=> (concat [1])
(1)
Solutions:
maximental's solution:
hypirion's solution:
chouser's solution:
jafingerhut's solution:
'Programming > Clojure' 카테고리의 다른 글
음량을 기준으로 동영상 편집하기 [하스스톤 전장] (0) | 2023.06.28 |
---|
댓글