1

I'm trying to get more familiar with functional programming and I was wondering if there is a more elegant way to group a list into pairs of 2 and apply a function to those pairs.

case class Line(start: Vector, end: Vector) {
  def isLeftOf(v: Vector) = (end - start).cross(v - start) < 0
}

case class Polygon(vertices: List[Vector]) {
  def edges = (vertices.sliding(2).toList :+ List(vertices.last,vertices.head)).map(l => Line(l(0), l(1)))
  def contains(v: Vector) = {
    edges.map(_.isLeftOf(v)).forall(_ == true)
  }
}

I'm talking about this line

def edges = (vertices.sliding(2).toList :+ List(vertices.last,vertices.head)).map(l => Line(l(0), l(1)))

Is there a better way to write this?

4

2 回答 2

2
val edges = (vertices, vertices.tail :+ vertices.head).zipped map Line

另请参阅以下问题:

你如何将 Scala 列表变成对?

最简洁的序列元素组合方式

于 2012-09-01T23:30:12.500 回答
0

那么你可以通过这样做来简化一点:

case class Polygon(vertices: List[Vector]) {
  def edges = Line(vertices.last, vertices.head) :: vertices.sliding(2).map(l => Line(l(0), l(1))).toList
  def contains(v: Vector) = edges.forall(_.isLeftOf(v))
}

我做了三件事:

  1. 拉出最后/标题线,使其不属于map
  2. 移到toList之后map,以便您映射迭代器,从而避免构建两个列表。
  3. 简化contains为简单地forall使用谓词调用。
于 2012-09-01T21:33:50.953 回答