• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala Ordering类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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
} 
开发者ID:krish121,项目名称:Spark-reference-applications,代码行数:32,代码来源:LogAnalyzerWindowed.scala


示例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])
  }
} 
开发者ID:cspinetta,项目名称:footpath-routing,代码行数:44,代码来源:EdgeUtils.scala


示例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))

} 
开发者ID:paulporu,项目名称:Play-User-Queue,代码行数:37,代码来源:User.scala


示例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)
    }
  }



} 
开发者ID:shaoweijob,项目名称:sparktest,代码行数:26,代码来源:OrderingUtils.scala


示例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)
    }
  }
} 
开发者ID:flegall,项目名称:learning-algorithms,代码行数:23,代码来源:SelectionSort.scala


示例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
  }
} 
开发者ID:flegall,项目名称:learning-algorithms,代码行数:26,代码来源:package.scala


示例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
    }
  }
} 
开发者ID:flegall,项目名称:learning-algorithms,代码行数:27,代码来源:ShellSort.scala


示例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))
  }
} 
开发者ID:ricardo8504,项目名称:frdomain-master,代码行数:57,代码来源:Monoid.scala


示例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
  }
} 
开发者ID:ricardo8504,项目名称:frdomain-master,代码行数:52,代码来源:Monoid.scala


示例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
  }
} 
开发者ID:ricardo8504,项目名称:frdomain-master,代码行数:52,代码来源:Monoid.scala



注:本文中的scala.math.Ordering类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala LinearRegression类代码示例发布时间:2022-05-23
下一篇:
Scala DefaultHttpErrorHandler类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap