2

用户 Régis Jean-Gilles 优雅地回答了我之前在 CanBuildFrom 和丰富函数(又名“pimp my library”或“enrich my library”)中苦苦挣扎的问题:

在 Scala 中创建一个使用正确类型包装 map() 的隐式函数:不适合胆小的人

但这一次我遇到了一个更复杂的问题。

我有一个函数来实现变体intersectWith, 通过它们的键相交集合。我设法使它们像正确的收集功能一样工作:

implicit class IntersectUnionWithPimp[K, A, Repr](a: GenTraversableLike[(K, A), Repr]) {

  /**
   * Intersect two collections by their keys. This is identical to
   * `intersectWith` except that the combiner function is passed the
   * key as well as the two items to combine.
   *
   * @param b Other collection to intersect with.
   * @param combine Function to combine values from the two collections.
   */
  def intersectWithKey[B, R, That](b: GenTraversable[(K, B)])(
      combine: (K, A, B) => R)(
      implicit bf: CanBuildFrom[Repr, (K, R), That]): That = {
    ...
  }

  /**
   * Intersect two collections by their keys. Keep the ordering of
   * objects in the first collection. Use a combiner function to
   * combine items in common. If either item is a multi-map, then
   * for a key seen `n` times in the first collection and `m`
   * times in the second collection, it will occur `n * m` times
   * in the resulting collection, including all the possible
   * combinations of pairs of identical keys in the two collections.
   *
   * @param b Other collection to intersect with.
   * @param combine Function to combine values from the two collections.
   */
  def intersectWith[B, R, That](b: GenTraversable[(K, B)])(
      combine: (A, B) => R)(
      implicit bf: CanBuildFrom[Repr, (K, R), That]): That =
    a.intersectWithKey(b){ case (_, x, y) => combine(x, y) }(bf)
}

现在,我目前也有非CanBuildFrom版本intersectBy和朋友,这些我无法作为CanBuildFrom版本工作。

implicit class IntersectUnionByPimp[A](a: Traversable[A]) {
  /**
   * Intersect two collections by their keys, with separate key-selection
   * functions for the two collections. This is identical to
   * `intersectBy` except that each collection has its own key-selection
   * function. This allows the types of the two collections to be
   * distinct, with no common parent.
   *
   * @param b Other collection to intersect with.
   * @param key1fn Function to select the comparison key for the first
   *   collection.
   * @param key1fn Function to select the comparison key for the first
   *   collection.
   * @param combine Function to combine values from the two collections.
   */
  def intersectBy2[K, B, R](b: Traversable[B])(key1fn: A => K
      )(key2fn: B => K)(combine: (A, B) => R): Traversable[R] = {
    val keyed_a = a.map { x => (key1fn(x), x) }
    val keyed_b = b.map { x => (key2fn(x), x) }
    keyed_a.intersectWith(keyed_b)(combine).map(_._2)
  }

  /**
   * Intersect two collections by their keys. Keep the ordering of
   * objects in the first collection. Use a combiner function to
   * combine items in common. If either item is a multi-map, then
   * for a key seen `n` times in the first collection and `m`
   * times in the second collection, it will occur `n * m` times
   * in the resulting collection, including all the possible
   * combinations of pairs of identical keys in the two collections.
   *
   * @param b Other collection to intersect with.
   * @param keyfn Function to select the comparison key.
   * @param combine Function to combine values from the two collections.
   */
  def intersectBy[K, B >: A](b: Traversable[B])(keyfn: B => K)(
      combine: (A, B) => B): Traversable[B] = {
    val keyed_a = a.map { x => (keyfn(x), x) }
    val keyed_b = b.map { x => (keyfn(x), x) }
    keyed_a.intersectWith(keyed_b)(combine).map(_._2)
  }
}

到目前为止我能想到的最好的版本是这样的:

implicit class IntersectUnionByPimp[A, Repr](a: GenTraversableLike[A, Repr]) {
  def intersectBy2[K, B, R, That](b: Traversable[B])(key1fn: A => K)(
      key2fn: B => K)(combine: (A, B) => R)(
      implicit bf: CanBuildFrom[Repr, R, That]): That = {
    // FIXME! How to make this work while calling `map`?
    // val keyed_a = a.map { x => (key1fn(x), x) }
    val keyed_a = mutable.Buffer[(K, A)]()
    a.foreach { x => keyed_a += ((key1fn(x), x)) }
    val keyed_b = b.map { x => (key2fn(x), x) }
    keyed_a.intersectWith(keyed_b)(combine).map(_._2)
  }

  def intersectBy[K, B >: A, That](b: Traversable[B])(keyfn: B => K)(
      combine: (A, B) => B)(
      implicit bf: CanBuildFrom[Repr, B, That]): That = {
    // FIXME! How to make this work while calling `map`?
    // val keyed_a = a.map { x => (keyfn(x), x) }
    val keyed_a = mutable.Buffer[(K, A)]()
    a.foreach { x => keyed_a += ((keyfn(x), x)) }
    val keyed_b = b.map { x => (keyfn(x), x) }
    keyed_a.intersectWith(keyed_b)(combine).map(_._2)
}

首先,我不明白为什么需要用可变缓冲区重写对map产生的调用;keyed_a似乎必须有更好的方法。但我仍然在底线得到同样的错误:

[error] /Users/benwing/devel/textgrounder/src/main/scala/opennlp/textgrounder/util/collection.scala:1018: type mismatch;
[error]  found   : scala.collection.mutable.Buffer[R]
[error]  required: That
[error]  Note: implicit method bufferToIndexedSeq is not applicable here because it comes after the application point and it lacks an explicit result type
[error]       keyed_a.intersectWith(keyed_b)(combine).map(_._2)
[error]                                                  ^

所以我的问题是:

  1. 如何调用mapGenTraversableLike?
  2. 如何使呼叫intersectWith正常工作?我知道我必须以某种方式传递CanBuildFrom基于我收到的那个,并且我知道关于mapResultBuilders,但我不确定在这里做什么,或者这是否可能。

的一个例子intersectBy,它与浮点数列表相交,如果它们的整数部分相同,则将两个数字视为相同,并计算绝对差:

scala> List(4.5,2.3,4.2).intersectBy(List(4.6,4.8))(_.toInt){ case (a,b) => (a - b).abs }
res2: Traversable[Double] = List(0.09999999999999964, 0.2999999999999998, 0.39999999999999947, 0.5999999999999996)

(除了返回的类型应该是List[Double]

谢谢你的帮助。

4

1 回答 1

1

好的,事实证明我需要创建一个构建器来返回项目,而不是尝试直接返回它们。以下作品:

implicit class IntersectUnionByPimp[A, Repr](a: GenTraversableLike[A, Repr]) {
  /**
   * Intersect two collections by their keys, with separate key-selection
   * functions for the two collections. This is identical to
   * `intersectBy` except that each collection has its own key-selection
   * function. This allows the types of the two collections to be
   * distinct, with no common parent.
   *
   * @param b Other collection to intersect with.
   * @param key1fn Function to select the comparison key for the first
   *   collection.
   * @param key2fn Function to select the comparison key for the first
   *   collection.
   * @param combine Function to combine values from the two collections.
   */
  def intersectBy2[K, B, R, That](b: GenTraversable[B])(key1fn: A => K)(
      key2fn: B => K)(combine: (A, B) => R)(
      implicit bf: CanBuildFrom[Repr, R, That]): That = {
    // It appears we can't call map() on `a`.
    val keyed_a = mutable.Buffer[(K, A)]()
    a.foreach { x => keyed_a += ((key1fn(x), x)) }
    val keyed_b = b.map { x => (key2fn(x), x) }
    // Nor can we return the value of map() here. Need to use a builder
    // instead.
    val bu = bf()
    for ((_, r) <- keyed_a.intersectWith(keyed_b)(combine))
      bu += r
    bu.result
  }

  /**
   * Intersect two collections by their keys. Keep the ordering of
   * objects in the first collection. Use a combiner function to
   * combine items in common. If either item is a multi-map, then
   * for a key seen `n` times in the first collection and `m`
   * times in the second collection, it will occur `n * m` times
   * in the resulting collection, including all the possible
   * combinations of pairs of identical keys in the two collections.
   *
   * @param b Other collection to intersect with.
   * @param keyfn Function to select the comparison key.
   * @param combine Function to combine values from the two collections.
   */
  def intersectBy[K, B >: A, That](b: GenTraversable[B])(keyfn: B => K)(
      combine: (A, B) => B)(
      implicit bf: CanBuildFrom[Repr, B, That]): That = {
    val keyed_a = mutable.Buffer[(K, A)]()
    a.foreach { x => keyed_a += ((keyfn(x), x)) }
    val keyed_b = b.map { x => (keyfn(x), x) }
    val bu = bf()
    for ((_, r) <- keyed_a.intersectWith(keyed_b)(combine))
      bu += r
    bu.result
  }
}

我不完全确定为什么只调用mapaGenTraversableLike似乎不起作用,但就这样吧。

于 2013-12-24T02:44:29.823 回答