Clojureのmapのちょっとした気付き

mapってリストを複数受け取れたんだなって

知らなかった

user=> (doc map)
-------------------------
clojure.core/map
([f coll] [f c1 c2] [f c1 c2 c3] [f c1 c2 c3 & colls])
  Returns a lazy sequence consisting of the result of applying f to the
  set of first items of each coll, followed by applying f to the set
  of second items in each coll, until any one of the colls is
  exhausted.  Any remaining items in other colls are ignored. Function
  f should accept number-of-colls arguments.
nil
user=> (map + [1 2 3] [2 3 4] [3 4 5] [4 5 6])
(10 14 18)
-------------------------

複数のリストの同じ位置の要素に, それぞれ関数を適用した結果のリスト(あやしい日本語)を返していることがわかる

渡したリストの長さが違う場合

user=> (map + [1 3] [2])
(3)
user=> (map + [1 3] [2 3 4])
(3 6)

短い方に合わせられている


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.