Ruby to Scala code translation - Sorting in Scala

怎甘沉沦 提交于 2019-12-13 03:24:53

问题


I'm converting some code from Ruby to Scala. Problem is that I never programmed Ruby in my life. It's going well, but now I reached a line that I don't know because I'm new in Scala and I don't understand the sorting mechanism. So I want to translate this ruby line to scala:

fronts[last_front].sort! {|x,y| crowded_comparison_operator(x,y)}

fronts is Vector[Vector[Map[String, Any]]]

last_front is an Int

crowded_comparison_operator(x,y) returns -1, 0 or 1, x and y are Map[String, Any]


回答1:


You have two possibilities with standard Scala collections:

  • Convert the -1, 0, 1 output of crowded_comparison_operator into a boolean that tells you whether the first element is less than the second element, then use sortWith.
  • define a new Ordering, pass it explicitly to the sorted method.

The sortWith method

The first element is less than the second element if and only if crowded_comparison_operator returns -1, so you could do this:

fronts(last_front).sortWith{ (x, y) => crowded_comparison_operator(x, y) < 0 }

Defining an Ordering for sorted

The sorted method takes an implicit Ordering parameter. You can define your own custom ordering, and pass it explicitly:

import scala.math.Ordering

fronts(last_front).sorted(new Ordering[Vector[Map[String, Any]]] {
  def compare(
    x: Vector[Map[String, Any]], 
    y: Vector[Map[String, Any]]
  ): Int = crowded_comparison_operator(x, y)
})

or shorter, with scala versions supporting SAM (since 2.11.5, if I remember correctly):

fronts(last_front).sorted(
  (x: Vector[Map[String, Any], y: Vector[Map[String, Any]]) => 
    crowded_comparison_operator(x, y)
)

Note that, as @mikej has pointed out, Ruby's sort! sorts the array in-place. This cannot work for an immutable vector, so you have to adjust your code accordingly.



来源:https://stackoverflow.com/questions/50317903/ruby-to-scala-code-translation-sorting-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!