本文整理汇总了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)
}
示例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)
}
}
示例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)
}
}