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

时间:2010-02-12 16:53:35

标签: scala scala-2.8

我试图为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属性。

处理这个问题的最佳方法是什么?

修改:使用托马斯的建议,这就是我可以获得的距离(适用于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?

3 个答案:

答案 0 :(得分: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'))

答案 1 :(得分:1)

你提到的所有类型(甚至String)都是显式的(List)或隐式的(Array和String)Iterable,所以你所要做的就是在你现在使用Seq的方法签名中使用Iterable。

答案 2 :(得分:1)

我在Scala 2.8上使用scala.collection.Seq,因为此类型是所有有序集合的父级。不幸的是,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))
}

我正在讨论这个函数,以便输入(和期望的)可用于推断类型。无论如何,这不适用于您在Scala 2.8上的Array版本,因为这需要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))
}

哪个会像找到一样工作。只要类型匹配,该程序就无法确定CC是什么。