4clojure 题解 Interleave Two Seqs

作者: admin 日期: 2017-12-28 10:13:48 人气: - 评论: 0

交织两个序列,不得使用interleave操作


Write a function which takes two sequences and returns the first item from each, then the second item from each, then the third, etc.

test not run
(= (__ [1 2 3] [:a :b :c]) '(1 :a 2 :b 3 :c))
test not run
(= (__ [1 2] [3 4 5 6]) '(1 3 2 4))
test not run
(= (__ [1 2 3 4] [5]) [1 5])
test not run
(= (__ [30 20] [25 15]) [30 25 20 15])













用loop循环搞定

(loop [s1 a
s2 b
ret (empty a)]
(if (or (empty? s1)
(empty? s2))
ret
(recur (rest s1)
(rest s2)
(conj ret
(first s1)
(first s2)) )))


看到github上有个比较巧妙的方式

mapcat vector

(mapcat vector [1 2 3] [:a :b :c])
=> (1 :a 2 :b 3 :c)


相关内容

发表评论
更多 网友评论0 条评论)
暂无评论

Copyright © 2012-2014 我的代码板 Inc. 保留所有权利。

页面耗时0.0197秒, 内存占用1.83 MB, 访问数据库13次

闽ICP备15009223号-1