从Scala中的列表中删除第一个对象的最佳方法是什么?
来自Java,我习惯于使用一种List.remove(Object o)
方法从列表中删除第一次出现的元素.既然我在Scala中工作,我希望该方法返回一个新的不可变List
而不是改变一个给定的列表.我也可能期望该remove()
方法采用谓词而不是对象.总之,我希望找到这样的方法:
/**
* Removes the first element of the given list that matches the given
* predicate, if any. To remove a specific object <code>x</code> from
* the list, use <code>(_ == x)</code> as the predicate.
*
* @param toRemove
* a predicate indicating which element to remove
* @return a new list with the selected object removed, or the same
* list if no objects satisfy the given predicate
*/
def …
Run Code Online (Sandbox Code Playgroud)