4clojure 题解 Compress a Sequence

作者: admin 日期: 2017-12-28 09:39:20 人气: - 评论: 0

去除字符串中连续重复的部分

Write a function which removes consecutive duplicates from a sequence.

test not run
(= (apply str (__ "Leeeeeerrroyyy")) "Leroy")
test not run
(= (__ [1 1 2 3 3 2 2 3]) '(1 2 3 2 3))
test not run
(= (__ [[1 2] [1 2] [3 4] [1 2]]) '([1 2] [3 4] [1 2]))















这个最好利用clojure特有的函数来做

(partition-by identity [1 222 33 22 33])

=> ((1) (222) (33) (22) (33))

identity 原样返回

partition-by  f cool) 把cool经过f变换然后把返回的序列中连续相同的部分分割


最后的答案

#(->>
  (partition-by identity %)
(map first)
)



相关内容

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

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

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

闽ICP备15009223号-1