4

我试图为这个 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):如何使用函数式方法来移动某些数组元素?

4

3 回答 3

3

一种方法是定义函数:

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'))
于 2010-02-12T17:51:01.070 回答
1

您提到的所有类型(甚至是字符串)都是显式(列表)或隐式(数组和字符串)Iterable,因此您所要做的就是在您现在使用 Seq 的方法签名中使用 Iterable。

于 2010-02-12T17:59:51.047 回答
1

我会scala.collection.Seq在 Scala 2.8 上使用,因为这种类型是所有有序集合的父级。除了ArrayString,不幸的是。可以通过视图边界来解决这个问题,如下所示:

def test
  [A, CC <% scala.collection.Seq[A]]
  (input: CC, expected: CC)
  (func: (CC, A => Boolean) => CC, predicate: A => Boolean): Unit = {
  def times(n: Int)(f: => Unit) = 1 to n foreach { count => f }
  def testFunction = assert(func(input, predicate) == expected)
  def warm = times(50) { testFunction }
  def test = times(50) { testFunction }

  warm
  val start = System.currentTimeMillis()
  test
  val end = System.currentTimeMillis()
  println("Total time "+(end - start))
}

我正在对这个函数进行柯里化,以便可以使用输入(和预期的)来推断类型。无论如何,这不适用于您自己Array在 Scala 2.8 上的版本,因为这需要Manifest. 我确信它可以以某种方式在这里提供,但我不太明白如何提供。

但是假设你忽略了所有关于序列、数组等的东西。只需从函数中删除视图绑定,你就会得到:

def test
  [A, CC]
  (input: CC, expected: CC)
  (func: (CC, A => Boolean) => CC, predicate: A => Boolean): Unit = {
  def times(n: Int)(f: => Unit) = 1 to n foreach { count => f }
  def testFunction = assert(func(input, predicate) == expected)
  def warm = times(50) { testFunction }
  def test = times(50) { testFunction }

  warm
  val start = System.currentTimeMillis()
  test
  val end = System.currentTimeMillis()
  println("Total time "+(end - start))
}

这将与查找一样工作。只要类型匹配,这个程序就不需要知道 aCC是什么。

于 2010-02-12T19:07:09.273 回答