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

Scala NumericRange类代码示例

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

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



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

示例1: genericEncoder

//设置package包名称以及导入依赖的类
package io.gustavoamigo.quill.pgsql.encoding.range.numeric

import java.sql.{Types, PreparedStatement}

import io.getquill.source.jdbc.JdbcSource

import scala.collection.immutable.NumericRange

trait Encoders {
  this: JdbcSource[_, _] =>

  private def genericEncoder[T](valueToString: (T => String)): Encoder[T] = {
    new Encoder[T] {
      override def apply(index: Int, value: T, row: PreparedStatement) = {
        val sqlLiteral = valueToString(value)
        row.setObject(index + 1, sqlLiteral, Types.OTHER)
        row
      }
    }
  }

  private def tuple[T](t: (T, T)) = s"[${t._1}, ${t._2}]"

  private def range[T](r: NumericRange[T]) = s"[${r.head}, ${r.last}]"

  implicit val intTupleEncoder: Encoder[(Int, Int)] = genericEncoder(tuple)
  implicit val intRangeEncoder: Encoder[NumericRange[Int]] = genericEncoder(range)
  implicit val bigIntTupleEncoder: Encoder[(BigInt, BigInt)] = genericEncoder(tuple)
  implicit val bigIntRangeEncoder: Encoder[NumericRange[BigInt]] = genericEncoder(range)
  implicit val longTupleEncoder: Encoder[(Long, Long)] = genericEncoder(tuple)
  implicit val longRangeEncoder: Encoder[NumericRange[Long]] = genericEncoder(range)
  implicit val doubleTupleEncoder: Encoder[(Double, Double)] = genericEncoder(tuple)
  implicit val bigDecimalTupleEncoder: Encoder[(BigDecimal, BigDecimal)] = genericEncoder(tuple)
  implicit val bigDecimalRangeEncoder: Encoder[NumericRange[BigDecimal]] = genericEncoder(range)
} 
开发者ID:gustavoamigo,项目名称:quill-pgsql,代码行数:36,代码来源:Encoders.scala


示例2: Polynomial

//设置package包名称以及导入依赖的类
package io.github.typeness.graphiccalculator

import scala.collection.immutable.NumericRange

case class Polynomial(coefficients: Map[Int, Double]) {
  val degree: Int = if (coefficients.keys.nonEmpty) coefficients.keys.max else 0

  def call(x: Double): Double = {
    coefficients.map {
      case (d, coef) => Math.pow(x, d) * coef
    }.sum
  }

  def getPointsInRange(domain: NumericRange[Double]): Map[Double, Double] = domain.map(x => (x, call(x))).toMap
}

object Polynomial {
  def fromMonomials(list: List[Monomial]): Polynomial = {
    val coefficientsMap = list
      .groupBy(_.degree)
      .map {
        case (degree, monomials) => (degree, monomials.map(_.coefficient).sum)
      }
    Polynomial(coefficientsMap)
  }
} 
开发者ID:typeness,项目名称:graphic-calculator,代码行数:27,代码来源:Polynomial.scala


示例3: decoder

//设置package包名称以及导入依赖的类
package io.gustavoamigo.quill.pgsql.encoding.range.numeric

import io.getquill.source.jdbc.JdbcSource
import io.gustavoamigo.quill.pgsql.encoding.GenericDecoder

import scala.collection.immutable.NumericRange

trait Decoders extends GenericDecoder {
  this: JdbcSource[_, _] =>

  private val rangePattern = """\[(\d+\.*\d*),(\d+\.*\d*)[\]|)]""".r

  private def decoder[T](transform: (String, String) => T) = decode(s =>
    rangePattern.findFirstMatchIn(s) match {
      case Some(m) => transform(m.group(1), m.group(2))
    }
  )

  implicit val intTupleDecoder: Decoder[(Int, Int)] = decoder((s1, s2) => (s1.toInt, s2.toInt - 1))
  implicit val intRangeDecoder: Decoder[NumericRange[Int]] = decoder((s1, s2) => Range.Int(s1.toInt, s2.toInt, 1))
  implicit val bigIntTupleDecoder: Decoder[(BigInt, BigInt)] = decoder((s1, s2) => (BigInt(s1), BigInt(s2) - BigInt(1)))
  implicit val bigIntRangeDecoder: Decoder[NumericRange[BigInt]] =
    decoder((s1, s2) => Range.BigInt(BigInt(s1), BigInt(s2), BigInt(1)))
  implicit val longTupleDecoder: Decoder[(Long, Long)] = decoder((s1, s2) => (s1.toLong, s2.toLong - 1))
  implicit val longRangeDecoder: Decoder[NumericRange[Long]] = decoder((s1, s2) => Range.Long(s1.toLong, s2.toLong, 1))
  implicit val doubleTupleDecoder: Decoder[(Double, Double)] = decoder((s1, s2) => (s1.toDouble, s2.toDouble))
  implicit val bigDecimalTupleDecoder: Decoder[(BigDecimal, BigDecimal)] = decoder((s1, s2) => (BigDecimal(s1), BigDecimal(s2)))
  implicit val bigDecimalRangeDecoder: Decoder[NumericRange[BigDecimal]] = decoder((s1, s2) => {
    val (d1, d2) = (BigDecimal(s1), BigDecimal(s2))
    Range.BigDecimal.inclusive(d1, d2, step(d1, d2))
  })

  private def step(d1: BigDecimal, d2: BigDecimal): BigDecimal = {
    val fraction1 = d1.remainder(BigDecimal(1)).toString.length
    val fraction2 = d2.remainder(BigDecimal(1)).toString.length

    val fraction = if (fraction1 > fraction2) d1 else d2
    BigDecimal(1) / BigDecimal(10).pow(fraction.precision - 1)
  }
} 
开发者ID:gustavoamigo,项目名称:quill-pgsql,代码行数:41,代码来源:Decoders.scala



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Scala Matrices类代码示例发布时间:2022-05-23
下一篇:
Scala ThreadFactoryBuilder类代码示例发布时间: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