本文整理汇总了Scala中scala.math.Ordering类的典型用法代码示例。如果您正苦于以下问题:Scala Ordering类的具体用法?Scala Ordering怎么用?Scala Ordering使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Ordering类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: LogAnalyzerWindowed
//设置package包名称以及导入依赖的类
package com.databricks.apps.logs
import scala.math.Ordering
import org.apache.spark.streaming.Seconds
import org.apache.spark.streaming.dstream.DStream
class LogAnalyzerWindowed(val windowLength: Long, val slideInterval: Long) extends AnalyzeFunctions with Serializable {
import LogStatistics.EMPTY_LOG_STATISTICS
var logStatistics = EMPTY_LOG_STATISTICS
def processAccessLogs(accessLogsDStream: DStream[ApacheAccessLog]): Unit = {
val windowDStream: DStream[ApacheAccessLog] = accessLogsDStream
.window(Seconds(windowLength), Seconds(slideInterval))
windowDStream.foreachRDD(accessLogs => {
if (accessLogs.count() == 0) {
logStatistics = EMPTY_LOG_STATISTICS
} else {
logStatistics = LogStatistics(contentSizeStats(accessLogs).get,
responseCodeCount(accessLogs).take(100).toMap,
filterIPAddress(ipAddressCount(accessLogs)).take(100),
endpointCount(accessLogs).top(10)(Ordering.by[(String, Long), Long](_._2)).toMap)
}
})
}
def getLogStatistics: LogStatistics = logStatistics
}
示例2: EdgeUtils
//设置package包名称以及导入依赖的类
package mapdomain.utils
import mapdomain.graph.{ GeoEdge, GeoVertex, GraphContainer }
import mapdomain.math.{ GVector, Line, Point }
import scala.math.Ordering
object EdgeUtils {
def edgeToLine[E <: GeoEdge, V <: GeoVertex[E]](edge: E)(implicit graph: GraphContainer[E, V]): Line = {
val start = edge.retrieveVertexStart.get
val end = edge.retrieveVertexEnd.get
val pointStart = Point(start.coordinate.longitude, start.coordinate.latitude)
val pointEnd = Point(end.coordinate.longitude, end.coordinate.latitude)
Line.ByPairPoints(pointStart, pointEnd)
}
def edgeToVector[E <: GeoEdge, V <: GeoVertex[E]](edge: E)(implicit graph: GraphContainer[E, V]): GVector = {
val start = edge.retrieveVertexStart.get
val end = edge.retrieveVertexEnd.get
val pointStart = Point(start.coordinate.longitude, start.coordinate.latitude)
val pointEnd = Point(end.coordinate.longitude, end.coordinate.latitude)
GVector(pointStart, pointEnd)
}
def pointToEdge[V, E](points: List[V], edgeGenerator: (V, V) ? E): List[E] = {
points.sliding(2).toList.flatMap {
case fst :: snd :: Nil ? List(edgeGenerator(fst, snd))
case _ ? Nil
}
}
// FIXME improve this
def sortEdgesByAngle[E <: GeoEdge, V <: GeoVertex[E], G <: GraphContainer[E, V]](from: V, edges: List[E])(implicit graph: G): List[E] = {
edges.sortBy(edge ? { // FIXME: Ver de mejorar el ordenamiento
from.coordinate.angleTo(graph.findVertex(edge.vertexEndId).get.coordinate)
})(Ordering[Double])
}
}
示例3: User
//设置package包名称以及导入依赖的类
// The MIT License (MIT)
// Copyright (c) 2016 Paul Lavery
//
// See the LICENCE.txt file distributed with this work for additional information regarding copyright ownership.
package models
import java.util.UUID
import scala.math.Ordering
import play.api.libs.json._
import play.api.libs.json.Reads._
import play.api.libs.functional.syntax._
import reactivemongo.play.json._
// Default parameter not needed because of workaround below but left intentionnally for clarity of intention
case class User(_id: UUID = UUID.randomUUID(),
firstName: String,
lastName: String,
email: String,
priority: Int)
object User {
implicit def orderingByPriority: Ordering[User] = Ordering.by(user => user.priority)
implicit val userFormat: OFormat[User] =
((JsPath \ "_id").formatNullable[UUID].inmap[UUID](_.getOrElse(UUID.randomUUID()), Option(_)) ~
(JsPath \ "firstName").format[String] ~
(JsPath \ "lastName").format[String] ~
(JsPath \ "email").format[String] ~
(JsPath \ "priority").format[Int]
)(User.apply, unlift(User.unapply))
}
示例4: OrderingUtils
//设置package包名称以及导入依赖的类
package com.ibeifeng.bigdata.spark.app.core
import scala.math.Ordering
/**
* Created by XuanYu on 2016/7/23.
*/
object OrderingUtils {
object SecondValueOrdering extends Ordering[(String, Int)]{
/**
*
* @param x
* @param y
* @return
*/
override def compare(x: (String, Int), y: (String, Int)): Int = {
x._2.compare(y._2)
// x._2 compare y._2 // 1 to 10 | 1.to(10)
}
}
}
示例5: SelectionSort
//设置package包名称以及导入依赖的类
package algorithms.sorting
import scala.math.Ordering
object SelectionSort {
def apply[T: Ordering](t: Array[T]): Unit = {
val ordering = implicitly[Ordering[T]]
val N = t.length
for (i <- 0 until N) {
var min = i
for (j <- i + 1 until N) {
if (ordering.lt(t(j), t(min))) {
min = j
}
}
swap(t, min, i)
}
}
}
示例6: swap
//设置package包名称以及导入依赖的类
package algorithms
import scala.math.Ordering
package object sorting {
def swap[T](t: Array[T], a: Int, b: Int): Unit = {
val q = t(a)
t(a) = t(b)
t(b) = q
}
def move_element_at_index_to_sorted_position_on_the_left[T](
t: Array[T],
i: Int,
ordering: Ordering[T],
step: Int = 1): Unit = {
val v = t(i)
var j = i
while (j >= step && ordering.gt(t(j - step), v)) {
t(j) = t(j - step)
j -= step
}
t(j) = v
}
}
示例7: ShellSort
//设置package包名称以及导入依赖的类
package algorithms.sorting
import scala.math.Ordering
object ShellSort {
def apply[T: Ordering](t: Array[T]): Unit = {
val ordering = implicitly[Ordering[T]]
// computing h value
var h = 1
while (h <= t.length / 9) {
h = 3 * h + 1
}
while (h > 0) {
for (i <- h until t.length) {
// Sorting each a h-zone
move_element_at_index_to_sorted_position_on_the_left(t,
i,
ordering,
step = h)
}
h /= 3
}
}
}
示例8: zero
//设置package包名称以及导入依赖的类
package frdomain.ch4
package patterns
trait Monoid[T] {
def zero: T
def op(t1: T, t2: T): T
}
object Monoid {
def apply[T](implicit monoid: Monoid[T]) = monoid
implicit val IntAdditionMonoid = new Monoid[Int] {
val zero = 0
def op(i: Int, j: Int) = i + j
}
implicit val BigDecimalAdditionMonoid = new Monoid[BigDecimal] {
val zero = BigDecimal(0)
def op(i: BigDecimal, j: BigDecimal) = i + j
}
implicit def MapMonoid[K, V: Monoid] = new Monoid[Map[K, V]] {
def zero = Map.empty[K, V]
def op(m1: Map[K, V], m2: Map[K, V]) = m2.foldLeft(m1) { (a, e) =>
val (key, value) = e
a.get(key).map(v => a + ((key, implicitly[Monoid[V]].op(v, value)))).getOrElse(a + ((key, value)))
}
}
final val zeroMoney: Money = Money(Monoid[Map[Currency, BigDecimal]].zero)
implicit def MoneyAdditionMonoid = new Monoid[Money] {
val m = implicitly[Monoid[Map[Currency, BigDecimal]]]
def zero = zeroMoney
def op(m1: Money, m2: Money) = Money(m.op(m1.m, m2.m))
}
object MoneyOrdering extends Ordering[Money] {
def compare(a:Money, b:Money) = a.toBaseCurrency compare b.toBaseCurrency
}
import MoneyOrdering._
import scala.math.Ordering
implicit val MoneyCompareMonoid = new Monoid[Money] {
def zero = zeroMoney
def op(m1: Money, m2: Money) = if (gt(m1, m2)) m1 else m2
}
implicit def BalanceAdditionMonoid = new Monoid[Balance] {
val m = implicitly[Monoid[Money]]
def zero = Balance(zeroMoney)
def op(b1: Balance, b2: Balance) = Balance(m.op(b1.amount, b2.amount))
}
}
示例9: zero
//设置package包名称以及导入依赖的类
package frdomain.ch4
package patterns
package monoids.monoid
trait Monoid[T] {
def zero: T
def op(t1: T, t2: T): T
}
object Monoid {
def apply[T](implicit monoid: Monoid[T]) = monoid
implicit val IntAdditionMonoid = new Monoid[Int] {
val zero = 0
def op(i: Int, j: Int) = i + j
}
implicit val BigDecimalAdditionMonoid = new Monoid[BigDecimal] {
val zero = BigDecimal(0)
def op(i: BigDecimal, j: BigDecimal) = i + j
}
implicit def MapMonoid[K, V: Monoid] = new Monoid[Map[K, V]] {
def zero = Map.empty[K, V]
def op(m1: Map[K, V], m2: Map[K, V]) = m2.foldLeft(m1) { (a, e) =>
val (key, value) = e
a.get(key).map(v => a + ((key, implicitly[Monoid[V]].op(v, value)))).getOrElse(a + ((key, value)))
}
}
final val zeroMoney: Money = Money(Monoid[Map[Currency, BigDecimal]].zero)
implicit def MoneyAdditionMonoid = new Monoid[Money] {
val m = implicitly[Monoid[Map[Currency, BigDecimal]]]
def zero = zeroMoney
def op(m1: Money, m2: Money) = Money(m.op(m1.m, m2.m))
}
object MoneyOrdering extends Ordering[Money] {
def compare(a:Money, b:Money) = a.toBaseCurrency compare b.toBaseCurrency
}
import MoneyOrdering._
import scala.math.Ordering
implicit val MoneyCompareMonoid = new Monoid[Money] {
def zero = zeroMoney
def op(m1: Money, m2: Money) = if (gt(m1, m2)) m1 else m2
}
}
示例10: zero
//设置package包名称以及导入依赖的类
package frdomain.ch4
package patterns
package monoids.foldable
trait Monoid[T] {
def zero: T
def op(t1: T, t2: T): T
}
object Monoid {
def apply[T](implicit monoid: Monoid[T]) = monoid
implicit val IntAdditionMonoid = new Monoid[Int] {
val zero = 0
def op(i: Int, j: Int) = i + j
}
implicit val BigDecimalAdditionMonoid = new Monoid[BigDecimal] {
val zero = BigDecimal(0)
def op(i: BigDecimal, j: BigDecimal) = i + j
}
implicit def MapMonoid[K, V: Monoid] = new Monoid[Map[K, V]] {
def zero = Map.empty[K, V]
def op(m1: Map[K, V], m2: Map[K, V]) = m2.foldLeft(m1) { (a, e) =>
val (key, value) = e
a.get(key).map(v => a + ((key, implicitly[Monoid[V]].op(v, value)))).getOrElse(a + ((key, value)))
}
}
final val zeroMoney: Money = Money(Monoid[Map[Currency, BigDecimal]].zero)
implicit def MoneyAdditionMonoid = new Monoid[Money] {
val m = implicitly[Monoid[Map[Currency, BigDecimal]]]
def zero = zeroMoney
def op(m1: Money, m2: Money) = Money(m.op(m1.m, m2.m))
}
object MoneyOrdering extends Ordering[Money] {
def compare(a:Money, b:Money) = a.toBaseCurrency compare b.toBaseCurrency
}
import MoneyOrdering._
import scala.math.Ordering
implicit val MoneyCompareMonoid = new Monoid[Money] {
def zero = zeroMoney
def op(m1: Money, m2: Money) = if (gt(m1, m2)) m1 else m2
}
}