gpt4 book ai didi

scala - 函数处理 Array[T] 或 List[T] 或 Iterable[T] 的函数

转载 作者:行者123 更新时间:2023-12-01 00:04:01 24 4
gpt4 key购买 nike

我试图为 this SO question 中提供的答案编写测试/计时函数.一些答案适用于 Array[T] , 一些在 List[T] , 一对一 Iterable[T]和一个在 String !

我想写的是一个函数,它接受 shift*从问题或答案、输入列表、谓词和预期输出中调用函数并运行该函数。有点像:

def test[T](
func:(Seq[T], T=>Boolean) => Seq[T],
input:Seq[T],
predicate:T=>Boolean,
expected:Seq[T]): Unit = {
// may be some warm up
// ... time start, run func, time stop,
// check output against expected
}

除了我可以找出签名,如 Array似乎是可变的 Seq属性,而 List似乎有不可变的 Seq特性。

处理这种情况的最佳方法是什么?

编辑 :使用 Thomas 的建议,这是我可以得到的接近程度(适用于 Array[Char]List[T] 但不适用于 Array[T] ):
val inputArr = Array('a', 'b', 'C', 'D')
val expectArr = Array('a', 'C', 'D', 'b')
val inputList = inputArr.toList
val expectList = expectArr.toList

def test[I, T](
func:(I, T=>Boolean) => Traversable[T],
input: I,
predicate: T=>Boolean,
expected: Traversable[T]): Boolean = {
val result = func(input, predicate)
if (result.size == expected.size) {
result.toIterable.zip(expected.toIterable).forall(x => x._1 == x._2)
} else {
false
}
}

// this method is from Geoff [there][2]
def shiftElements[A](l: List[A], pred: A => Boolean): List[A] = {
def aux(lx: List[A], accum: List[A]): List[A] = {
lx match {
case Nil => accum
case a::b::xs if pred(b) && !pred(a) => aux(a::xs, b::accum)
case x::xs => aux(xs, x::accum)
}
}
aux(l, Nil).reverse
}

def shiftWithFor[T](a: Array[T], p: T => Boolean):Array[T] = {
for (i <- 0 until a.length - 1; if !p(a(i)) && p(a(i+1))) {
val tmp = a(i); a(i) = a(i+1); a(i+1) = tmp
}
a
}

def shiftWithFor2(a: Array[Char], p: Char => Boolean):Array[Char] = {
for (i <- 0 until a.length - 1; if !p(a(i)) && p(a(i+1))) {
val tmp = a(i); a(i) = a(i+1); a(i+1) = tmp
}
a
}

def shiftMe_?(c:Char): Boolean = c.isUpper

println(test(shiftElements[Char], inputList, shiftMe_?, expectList))
println(test(shiftWithFor2, inputArr, shiftMe_?, expectArr))
//following line does not compile
println(test(shiftWithFor, inputArr, shiftMe_?, expectArr))
//found : [T](Array[T], (T) => Boolean) => Array[T]
//required: (?, (?) => Boolean) => Traversable[?]

//following line does not compile
println(test(shiftWithFor[Char], inputArr, shiftMe_?, expectArr))
//found : => (Array[Char], (Char) => Boolean) => Array[Char]
//required: (?, (?) => Boolean) => Traversable[?]

//following line does not compile
println(test[Array[Char], Char](shiftWithFor[Char], inputArr, shiftMe_?, expectArr))
//found : => (Array[Char], (Char) => Boolean) => Array[Char]
//required: (Array[Char], (Char) => Boolean) => Traversable[Char]

我会将 Daniel 的答案标记为已接受,因为它编译并为我提供了一种不同的方式来实现我想要的 - 除非 Array[T] 上的方法创建一个新数组(并带来 Manifest 问题)。

(2): How would be a functional approach to shifting certain array elements?

最佳答案

一种方法是定义函数:

def test[I, T](
func:(I, T=>Boolean) => Traversable[T],
input: I,
predicate: T=>Boolean,
expected: Traversable[T]): Unit = {
println(func(input, predicate))
}

def g(x : Char) = true

test((x : String, y: Char => Boolean) => x, "asdf", g _ , "expected")
test((x : List[Char], y: Char => Boolean) => x, List('s'), g _, List('e'))
test((x : Array[Char], y: Char => Boolean) => x, Array('s'), g _, Array('e'))
test((x : Iterable[Char], y: Char => Boolean) => x, Set('s'), g _, Set('e'))

关于scala - 函数处理 Array[T] 或 List[T] 或 Iterable[T] 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2253521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com