【问题标题】:function working on functions of Array[T] or List[T] or Iterable[T]作用于 Array[T] 或 List[T] 或 Iterable[T] 函数的函数
【发布时间】:2010-02-12 16:53:34
【问题描述】:

我试图为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?

【问题讨论】:

    标签: scala scala-2.8


    【解决方案1】:

    一种方法是定义函数:

    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'))
    

    【讨论】:

    • 我真的是想让test 打电话给func(input, predicate)。这会导致found: input.type, required: I。我可以通过在test 声明中更改为input:I 来解决这个问题。然后我得到inferred type arguments [Array[Char],Char] do not conform to method test's type parameter bounds [I &lt;: Traversable[T],T]Array[Char] 示例。 String 示例也有问题。 ListIterable 工作。
    • 您可以删除 I <: traversable t="">Boolean) => I and expected: I)。
    【解决方案2】:

    我会在 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))
    }
    

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

    【讨论】:

    • 首先,感谢您将我在问题中的建议具体化,并建议 A 和 CC 作为由 func 绑定在一起的不相关类型。我遇到的唯一问题是就地数组解决方案与多次运行该函数不能很好地配合,因为输出成为下一次的输入。我尝试在 shift 函数中创建一个新的数组副本,但随后遇到了 Manifest 的问题:could not find implicit value for evidence parameter of type Manifest[T].
    • @huynhjl 是的,在 Scala 2.8 上使用数组可能很困难。我在想您可能必须使函数柯里化,在第一个参数列表中传递一个显式的Manifest[T],然后在必要时传递该参数。并且,当调用test 时,也要传递明确的Manifest。再提出一个问题,我会调查一下。
    【解决方案3】:

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

    【讨论】:

    • 我在使用这种方法时遇到了麻烦:polymorphic expression cannot be instantiated to expected type; found : =&gt; (Array[Char], (Char) =&gt; Boolean) =&gt; Array[Char] required: (Iterable[Char], (Char) =&gt; Boolean) =&gt; Iterable[Char]。如果我离开[Char],我会收到与Iterable[?] 相同的消息。所以似乎没有应用隐式。
    猜你喜欢
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    • 2012-08-28
    • 2016-06-26
    • 2015-08-21
    相关资源
    最近更新 更多