def stripFrom[A](lst: List[A], x: List[A]): List[A] =
if (lst.containsSlice(x) && x.length > 0)
stripFrom(lst.patch(lst.indexOfSlice(x), List(), x.length), x)
else lst
概念證明:
scala> stripFrom(List(1,2,3,2,6,4,2,3,4,2,1,3,6,3,2), List(2,3))
res3: List[Int] = List(1, 2, 6, 4, 4, 2, 1, 3, 6, 3, 2)
scala> stripFrom(List(1,2,3,2,6,4,2,3,4,2,1,3,6,3,2), List(4,2))
res4: List[Int] = List(1, 2, 3, 2, 6, 3, 1, 3, 6, 3, 2)
scala> stripFrom(List(1,2,3,2,6,4,2,3,4,2,1,3,6,3,2), List(4,2,3,4))
res5: List[Int] = List(1, 2, 3, 2, 6, 2, 1, 3, 6, 3, 2)
scala> stripFrom(List(1,2,3,2,6,4,2,3,4,2,1,3,6,3,2), List(2))
res6: List[Int] = List(1, 3, 6, 4, 3, 4, 1, 3, 6, 3)
我甚至不確定這個問題是否明確。如果我有'List(1,2,1,2,1)'我該怎麼做,我想刪除序列'1,2,1'。我最終得到了「List(1,2)'還是'List(2,1)'? – Alec
@ cricket_007我已更新我的問題 @Alec一般而言,您是正確的。在這個特定的例子中,我需要從左邊開始移除,然後移到右邊,所以在你的例子中,結果將是'List(2,1)' –
感謝您的編輯,但是你要去哪裏從那裏?這不適合你嗎? –