什么是习语Clojure“去除”从列表中的多个单个实例? [英] What is idiomatic Clojure to "remove" a single instance from many in a list?

查看:132
本文介绍了什么是习语Clojure“去除”从列表中的多个单个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,其中可能包含比较相等的元素。我想要一个类似的列表,但删除一个元素。所以从(:a:b:c:b:d)我想要能够删除只是一个:b获得(:a:c:b:d) p>

上下文是卡牌游戏中的一手牌,其中有两副标准牌在玩,所以可能有重复的牌,但是一次只能玩一张。



我有工作代码,见下文。在Clojure中有更多惯用的方法吗?

 (defn remove-one [c left right] 
if(= right())
left
(if(= c(first right))
(concat(reverse left)(rest right))
(删除一个c()卡)))

(defn remove-card [c cards]
b

这是我前一段时间的Scala答案:什么是习语Scala方式,一个来自不可变列表的元素?

解决方案

如何:

 (let [[nm](split-with(partial not =:b)[:a:b:c:b:d])](concat n )))

它将列表拆分为:b,然后删除:b, 。


I have a list, which may contain elements that will compare as equal. I would like a similar list, but with one element removed. So from (:a :b :c :b :d) I would like to be able to "remove" just one :b to get (:a :c :b :d).

The context is a hand in a card game where two decks of standard cards are in play, so there may be duplicate cards but still played one at a time.

I have working code, see below. Are there more idiomatic ways to do this in Clojure?

(defn remove-one [c left right]
  (if (= right ())
    left
    (if (= c (first right))
      (concat (reverse left) (rest right))
      (remove-one c (cons (first right) left) (rest right)))))

(defn remove-card [c cards]
  (remove-one c () cards))

Here are the Scala answers I got a while ago: What is an idiomatic Scala way to "remove" one element from an immutable List?

解决方案

How about:

(let [[n m] (split-with (partial not= :b) [:a :b :c :b :d])] (concat n (rest m)))

Which splits the list at :b and then removes the :b and concats the two lists.

这篇关于什么是习语Clojure“去除”从列表中的多个单个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆