当前位置: 首页>>代码示例>>Scala>>正文


Scala Sorting类代码示例

本文整理汇总了Scala中scala.util.Sorting的典型用法代码示例。如果您正苦于以下问题:Scala Sorting类的具体用法?Scala Sorting怎么用?Scala Sorting使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Sorting类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: ProblemTwentyTwo

//设置package包名称以及导入依赖的类
package org.nason.euler.twenty

import scala.util.Sorting
object ProblemTwentyTwo {
  
  def main(args: Array[String]) {
    println( solution )
  }
  
  def solution =
  {
    val lines = io.Source.fromFile(dataPath).getLines() toArray
    // split comma-separated values and strip "
    val values = (lines(0) split ",") map ( s => s.substring(1,s.length-1) )
    Sorting.quickSort(values)
    def nameToValue( s:String ) = { s map (_.toInt - 'A' + 1) reduceLeft(_+_) }
    (values map ( nameToValue(_) ) zipWithIndex)
       .map( i => ((i._2+1) * i._1).toLong )
       .reduceLeft( _+_ )
  }
  
  val dataPath = "/home/sorensjm/workspace/euler/data/problem22.txt"
} 
开发者ID:drkeoni,项目名称:euler-jms-scala,代码行数:24,代码来源:ProblemTwentyTwo.scala

示例2: STArraySyntax

//设置package包名称以及导入依赖的类
package sio.core.syntax

import cats.kernel.Order
import sio.core.MutSyntax

import scala.annotation.tailrec
import scala.reflect.ClassTag
import scala.util.Sorting

import sio.core._

trait STArraySyntax {
  implicit class STArraySyntax[S, E](handle: STArray[S, E]) extends MutSyntax[S, Array[E]](handle) {
    def length: Int = unsafePure { _.length }

    def get(i: Int): ST[S, E] = lift { _.apply(i) }
    def set(i: Int, x: E): ST[S, Unit] = lift { _.update(i, x) }

    def transform(f: E => E): ST[S, Unit] = lift { a =>
      @tailrec def go(i: Int): Unit =
        if (i < a.length) {
          a.update(i, f(a(i)))
          go(i + 1)
        } else ()
      go(0)
    }

    def stableSort(implicit E: Order[E], CT: ClassTag[E]): ST[S, Unit] =
      lift { a => Sorting.stableSort(a)(CT, E.toOrdering) }
    def quickSort(implicit E: Order[E], CT: ClassTag[E]): ST[S, Unit] =
      lift { a => Sorting.quickSort(a)(E.toOrdering) }
  }
} 
开发者ID:alexknvl,项目名称:sio,代码行数:34,代码来源:STArraySyntax.scala

示例3: IntervalSchedule

//设置package包名称以及导入依赖的类
package com.gaiam.gcsis.util

import org.joda.time.{Interval => JInterval}
import org.joda.time.Instant
import org.joda.time.ReadableInstant
import scala.util.Sorting

class IntervalSchedule(seq: Seq[JInterval]) {
    val intervals: Array[JInterval] = Sorting.stableSort(seq, (a: JInterval, b: JInterval) => a.getStart.compareTo(b.getStart) <= 0)

    private def combine(i1: JInterval, i2: JInterval) = {
        if (i1.contains(i2)) i1
        else if (i2.contains(i1)) i2
        else new JInterval(i1.getStart, i2.getEnd)
    }

    def continuousInterval(from: ReadableInstant) = {
        intervals.foldLeft[Option[JInterval]](None)(
            (result, interval) => {
             (result, interval) match {
                case (None, i: JInterval) if i.contains(from) => Some(i)
                case (None, _) => None
                case (Some(res: JInterval), i: JInterval) if res.overlaps(i) || res.abuts(i) => Some(combine(res, i))
                case _ => result
             }
            })
    }
} 
开发者ID:GaiamTV,项目名称:gcsi-scala-util,代码行数:29,代码来源:IntervalSchedule.scala


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