Convert BigDecimal to BigInt Scala - scala

How to convert BigDecimal to BigInt in Scala?
I am trying to run the following code:
val x:BigDecimal = 2.335656787776;
val y = new BigInt(x);
The Error I get is:
Found: (PlayGround.x : BigDecimal)
Required: java.math.BigInteger
Whereas when I do:
val x:BigDecimal = 2.335656787776;
val y = BigInt(x);
I get:
None of the overloaded alternatives of method apply in object BigInt with types
(x: java.math.BigInteger): BigInt
(x: String, radix: Int): BigInt
(x: String): BigInt
(numbits: Int, rnd: scala.util.Random): BigInt
(bitlength: Int, certainty: Int, rnd: scala.util.Random): BigInt
(signum: Int, magnitude: Array[Byte]): BigInt
(x: Array[Byte]): BigInt
(l: Long): BigInt
(i: Int): BigInt
match arguments ((PlayGround.x : BigDecimal))
How can I cast BigDecimal into BigInt?

toBigInt does what you need. From docs
Converts this BigDecimal to a scala.BigInt.
Which does the following:
new BigInt(this.bigDecimal.toBigInteger)
There is also toBigIntExact which returns an option, if the conversion can be done losslessly.

Use .toBigInt
val x: BigDecimal = 2.335656787776
x.toBigInt // returns 2

A workAround that I found is:
val x:BigDecimal = 2.335656787776;
val y = BigInt("%.0f".format(x));
Is there any better way for the same?

Related

Add null to int column

I have a scala code as below
case class Employee(firstName: String, lastName: String, email: String, salary: Int)
val employee = new Employee("John", null, "john-doe#some.edu", null)
It fails with the below error
error: an expression of type Null is ineligible for implicit conversion
How do i add Null to int salary column?
Int is a primitive type and extends AnyVal which cannot hold null.
null can only be used by AnyRef types.
For Int, null translates to 0.
Refer:
scala> null.asInstanceOf[Int]
res0: Int = 0
scala> null.asInstanceOf[String]
res1: String = null
You could instantiate your class as below:
scala> val employee = new Employee("John", null, "john-doe#some.edu", null.asInstanceOf[Int])
employee: Employee = Employee(John,null,john-doe#some.edu,0)

SCALA: How to Construct a composite Type That can accept any of Int, Long or BigInt implicitly

Suppose I want a Type T such that if I type in
SCALA REPL :
val x:T = 34
x: BigInt = 34
val y:T = 34L
y: BigInt = 34
val z:T = BigInt(34)
z: BigInt = 34
This means I need to construct a Type T using Int, Long and BigInt Types which returns as BigInt in SCALA REPL.
Or Better even if I get:
val x:T = 34
x: T = 34
val y:T = 34L
y: T = 34
val z:T = BigInt(34)
z: T = 34
BigInt is your T type.
Scala has built in implicit conversions for that.
e.g.
val x: BigInt = 34
val y: BigInt = 34L
val z: BigInt = BigInt(34)
will all work.
If you want a different type than BigInt you can use an implicit conversion
import scala.language.implicitConversions
class BigIntWrapper(val value: BigInt) extends AnyVal
implicit def IntegralIsBigInt[T: Integral](num: T): BigIntWrapper = {
val ev = implicitly[Integral[T]]
new BigIntWrapper(BigInt(ev.toLong(num)))
}
val a: BigIntWrapper = 4
val b: BigIntWrapper = 4L

scala string interpolation with a variable length

I use scala f string interpolator as follows:
def format(id: Int) = f"A$id%04d"
format(21) // A0021
However, I would like to be able to define a length once and for all (before fixed to 4), and get a function that it is going to format the string with that length.
So, instead of having
def format(length: Int, id: Int) = ???
f(5, 21) // A00021
I would like to have this:
def format(length: Int)(id: Int) = ???
val f = format(5)
f(21) // A00021
How can I implement this using scala f interpolator or other?
Update
I was not looking for such a solution involving the compiler at runtime, but I appreciate som-snytt's answer. Here there is a working solution based on his answer:
import scala.tools.reflect._,scala.reflect.runtime._,universe._
def defFormat(length: Int): Int => String = {
val code = raw"""(i: Int) => f"A$$i%0${length}d""""
tb.eval(tb.parse(code)).asInstanceOf[Int => String]
}
val format = defFormat(length = 5)
format(21)
scala> def format(n: Int)(i: Int) =
| f"A%%0${n}d" format i
format: (n: Int)(i: Int)String
scala> format(5) _
res0: Int => String = <function1>
scala> .apply(21)
res1: String = A00021
Edit:
scala> import scala.tools.reflect._,scala.reflect.runtime._,universe._
import scala.tools.reflect._
import scala.reflect.runtime._
import universe._
scala> val tb = currentMirror.mkToolBox()
tb: scala.tools.reflect.ToolBox[reflect.runtime.universe.type] = scala.tools.reflect.ToolBoxFactory$ToolBoxImpl#2d10e0b1
scala> def f(n: Int)(i: Int): String = {
| val code = raw"""f"A$${$i}%0${n}d""""
| tb.eval(tb.parse(code)).asInstanceOf[String]
| }
f: (n: Int)(i: Int)String
scala> val g = f(5) _
g: Int => String = <function1>
scala> g(21)
res9: String = A00021
That doesn't actually help much. You really want to
scala> tb.typecheck(tb.parse(code))
scala.tools.reflect.ToolBoxError: reflective typecheck has failed: illegal conversion character 'k'
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$typecheck$1.apply(ToolBoxFactory.scala:178)
which throws if the format is bad.
scala> val code = raw"""(i: Int) => f"A$${i}%k0${10}d""""
code: String = (i: Int) => f"A${i}%k010d"
scala> tb.typecheck(tb.parse(code))
scala.tools.reflect.ToolBoxError: reflective typecheck has failed: illegal conversion character 'k'
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$typecheck$1.apply(ToolBoxFactory.scala:178)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$typecheck$1.apply(ToolBoxFactory.scala:170)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$transformDuringTyper$1$$anonfun$11.apply(ToolBoxFactory.scala:148)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$transformDuringTyper$1$$anonfun$11.apply(ToolBoxFactory.scala:148)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$transformDuringTyper$1$$anonfun$9.apply(ToolBoxFactory.scala:138)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$transformDuringTyper$1$$anonfun$9.apply(ToolBoxFactory.scala:138)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$transformDuringTyper$1$$anonfun$withContext$1$1.apply(ToolBoxFactory.scala:139)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$transformDuringTyper$1$$anonfun$withContext$1$1.apply(ToolBoxFactory.scala:139)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$transformDuringTyper$1$$anonfun$7.apply(ToolBoxFactory.scala:137)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$transformDuringTyper$1$$anonfun$7.apply(ToolBoxFactory.scala:137)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$transformDuringTyper$1.apply(ToolBoxFactory.scala:148)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$transformDuringTyper$1.apply(ToolBoxFactory.scala:121)
at scala.reflect.internal.Trees$class.wrappingIntoTerm(Trees.scala:1716)
at scala.reflect.internal.SymbolTable.wrappingIntoTerm(SymbolTable.scala:16)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal.withWrapping$1(ToolBoxFactory.scala:120)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal.transformDuringTyper(ToolBoxFactory.scala:121)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal.typecheck(ToolBoxFactory.scala:169)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$$anonfun$typecheck$2.apply(ToolBoxFactory.scala:375)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$$anonfun$typecheck$2.apply(ToolBoxFactory.scala:367)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$withCompilerApi$.liftedTree2$1(ToolBoxFactory.scala:355)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$withCompilerApi$.apply(ToolBoxFactory.scala:355)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl.typecheck(ToolBoxFactory.scala:367)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl.typecheck(ToolBoxFactory.scala:27)
... 32 elided
scala> val code = raw"""(i: Int) => f"A$${i}%0${10}d""""
code: String = (i: Int) => f"A${i}%010d"
scala> tb.typecheck(tb.parse(code))
res19: tb.u.Tree =
((i: Int) => ({
val arg$macro$9: Int = i;
new scala.collection.immutable.StringOps("A%010d").format(arg$macro$9)
}: String))
You can't do it using f because its whole point is to make sure it can check the format string for type errors, so the format string has to be static. f could support this scenario explicitly, but it doesn't.
You could make format a macro, but this seems like an overkill. Not to mention that it would have to be defined in a separate module, which looks very inconvenient for this scenario.

Too many elements for Tuple: 27, allowed: 22

I am new to Slick and using Slick 3.1.1. My table looks like
import java.sql.{Blob, Timestamp}
import slick.collection.heterogeneous.HNil
import slick.driver.MySQLDriver.api._
case class AnomalyC(id: Int, serviceName: String, serviceId: String, timeUpdated: Timestamp, timestamp: Timestamp, anomalyCategoryId: Int,
userGroup:Int, riskValue: Float, activityTypeId: Int, destinationHost: String, userName: String, tenantId: Int, information:Blob, timeCreated: Timestamp, userId: Int, anomalyType:Int, anomalyValue:String, measure:Int,
userAction:Int, uniqueIdentifier:Int, similarCount:Int, trainingValue:String, state: Int, riskLevel:Int, userRiskLevel:Int,
userRiskScore: Float, response:Int)
class Anomaly(tag:Tag) extends Table[AnomalyC](tag, "Anomaly") {
def id = column[Int]("id")
def serviceName = column[String]("ServiceName")
def serviceId = column[Int]("ServiceId")
def timeUpdated = column[Timestamp]("TimeUpdated")
def timestamp = column[Timestamp]("Timestamp")
def anomalyCategoryId = column[Int]("AnomalyCategoryId")
def userGroup = column[Int]("UserGroup")
def riskValue = column[Float]("RiskValue")
def activityTypeId = column[Int]("ActivityTypeId")
def destinationHost = column[String]("DestinationHost")
def userName = column[String]("UserName")
def tenantId = column[Int]("TenantId")
def information = column[Blob]("Information")
def timeCreated = column[Timestamp]("TimeCreated")
def userId = column[Int]("UserId")
def anomalyType = column[Int]("AnomalyType")
def anomalyValue = column[String]("AnomalyValue")
def measure = column[Int]("Measure")
def userAction = column[Int]("UserAction")
def uniqueIdentifier = column[String]("UniqueIdentifier")
def similarCount = column[Int]("SimilarCount")
def trainingValue = column[String]("TrainingValue")
def state = column[Int]("State")
def riskLevel = column[Int]("RiskLevel")
def userRiskLevel = column[Int]("UserRiskLevel")
def userRiskScore = column[Float]("UserRiskScore")
def response = column[Int]("Response")
def * = (id, serviceName, serviceId, timeUpdated, timestamp, anomalyCategoryId, userGroup,
riskValue, activityTypeId, destinationHost, userName, tenantId, information, timeCreated, userId, anomalyType, anomalyValue,
measure, userAction, uniqueIdentifier, similarCount, trainingValue, state, riskLevel, userRiskLevel, userRiskScore, response)
}
When I run this, I get error as
Error:(57, 11) too many elements for tuple: 27, allowed: 22
def * = (id, serviceName, serviceId, timeUpdated, timestamp, anomalyCategoryId, userGroup,
^
What do I do?
You could use HLists. Slick has it's own implementation or for probably better interoperability you could use Shapeless. Here is an article that explains it: http://underscore.io/blog/posts/2015/08/08/slickless.html
Alternatively you can use a case class instead of a tuple.
The following worked for me based on the direction provided by #cvogt.
import java.sql.{Blob, Timestamp}
import slick.collection.heterogeneous.HNil
import slick.collection.heterogeneous.syntax._
import slick.driver.MySQLDriver.api._
class Anomaly(tag:Tag) extends Table[Int :: String :: Int :: Timestamp :: Timestamp :: Int :: Int :: Float :: Int :: String
:: String :: Int ::Blob :: Timestamp :: Int ::Int ::String ::Int ::Int ::String ::Int ::String :: Int ::Int ::Int ::
Float :: Int :: HNil ](tag, "Anomaly") {
def id = column[Int]("id")
def serviceName = column[String]("ServiceName")
def serviceId = column[Int]("ServiceId")
def timeUpdated = column[Timestamp]("TimeUpdated")
def timestamp = column[Timestamp]("Timestamp")
def anomalyCategoryId = column[Int]("AnomalyCategoryId")
def userGroup = column[Int]("UserGroup")
def riskValue = column[Float]("RiskValue")
def activityTypeId = column[Int]("ActivityTypeId")
def destinationHost = column[String]("DestinationHost")
def userName = column[String]("UserName")
def tenantId = column[Int]("TenantId")
def information = column[Blob]("Information")
def timeCreated = column[Timestamp]("TimeCreated")
def userId = column[Int]("UserId")
def anomalyType = column[Int]("AnomalyType")
def anomalyValue = column[String]("AnomalyValue")
def measure = column[Int]("Measure")
def userAction = column[Int]("UserAction")
def uniqueIdentifier = column[String]("UniqueIdentifier")
def similarCount = column[Int]("SimilarCount")
def trainingValue = column[String]("TrainingValue")
def state = column[Int]("State")
def riskLevel = column[Int]("RiskLevel")
def userRiskLevel = column[Int]("UserRiskLevel")
def userRiskScore = column[Float]("UserRiskScore")
def response = column[Int]("Response")
def * = id :: serviceName :: serviceId :: timeUpdated :: timestamp :: anomalyCategoryId :: userGroup ::
riskValue :: activityTypeId :: destinationHost :: userName :: tenantId :: information :: timeCreated :: userId :: anomalyType :: anomalyValue ::
measure :: userAction :: uniqueIdentifier :: similarCount :: trainingValue :: state :: riskLevel :: userRiskLevel :: userRiskScore :: response :: HNil
}
The build runs and test pass, however, I still see IntelliJ complains with the following error
As per answer from #insan-e, I re-wrote this and this works as well. I like this approach better however I do not understand the code in it's entirety
import java.sql.{Blob, Timestamp}
import slick.driver.MySQLDriver.api._
case class Anomaly1(id:Int, serviceName:String, serviceId: Int, timeUpdated: Timestamp, timeStamp: Timestamp,
anomalyCategoryId: Int, userGroup: Int, riskValue: Float, activityTypeId: Int, destinationHost: String, userName: String)
case class Anomaly2(tenantId: Int, information:Blob, timeCreated: Timestamp, userId: Int, anomalyType:Int, anomalyValue: String, measure: Int, userAction: Int,
uniqueIdentifier: String, similarCount: Int, trainingValue: String, state: Int, riskLevel: Int,
userRiskLevel: Int, userRiskScore: Float, response: Int)
case class AnomalyRow(anomaly1: Anomaly1, anomaly2: Anomaly2)
class Anomaly(tag:Tag) extends Table[AnomalyRow](tag, "Anomaly") {
def id = column[Int]("id")
def serviceName = column[String]("ServiceName")
def serviceId = column[Int]("ServiceId")
def timeUpdated = column[Timestamp]("TimeUpdated")
def timestamp = column[Timestamp]("Timestamp")
def anomalyCategoryId = column[Int]("AnomalyCategoryId")
def userGroup = column[Int]("UserGroup")
def riskValue = column[Float]("RiskValue")
def activityTypeId = column[Int]("ActivityTypeId")
def destinationHost = column[String]("DestinationHost")
def userName = column[String]("UserName")
def tenantId = column[Int]("TenantId")
def information = column[Blob]("Information")
def timeCreated = column[Timestamp]("TimeCreated")
def userId = column[Int]("UserId")
def anomalyType = column[Int]("AnomalyType")
def anomalyValue = column[String]("AnomalyValue")
def measure = column[Int]("Measure")
def userAction = column[Int]("UserAction")
def uniqueIdentifier = column[String]("UniqueIdentifier")
def similarCount = column[Int]("SimilarCount")
def trainingValue = column[String]("TrainingValue")
def state = column[Int]("State")
def riskLevel = column[Int]("RiskLevel")
def userRiskLevel = column[Int]("UserRiskLevel")
def userRiskScore = column[Float]("UserRiskScore")
def response = column[Int]("Response")
private type Anomaly1TupleType = (Int, String, Int, Timestamp, Timestamp, Int, Int, Float, Int, String, String)
private type Anomaly2TupleType = (Int, Blob, Timestamp, Int, Int, String, Int, Int, String, Int, String, Int, Int, Int, Float, Int)
private type AnomalyRowTupleType = (Anomaly1TupleType, Anomaly2TupleType)
private val anomalyShapedValue = (
(id, serviceName, serviceId, timeUpdated, timestamp, anomalyCategoryId, userGroup, riskValue, activityTypeId, destinationHost, userName),
(tenantId, information, timeCreated, userId, anomalyType, anomalyValue, measure, userAction, uniqueIdentifier, similarCount, trainingValue, state, riskLevel, userRiskLevel, userRiskScore, response)
).shaped[AnomalyRowTupleType]
private val toAnomalyRow: (AnomalyRowTupleType => AnomalyRow) = { anomalyTuple =>
AnomalyRow(anomaly1 = Anomaly1.tupled.apply(anomalyTuple._1), anomaly2 = Anomaly2.tupled.apply(anomalyTuple._2))
}
private val toAnomalyTuple: (AnomalyRow => Option[AnomalyRowTupleType]) = { anomalyRow =>
Some(Anomaly1.unapply(anomalyRow.anomaly1).get, Anomaly2.unapply(anomalyRow.anomaly2).get)
}
def * = anomalyShapedValue <> (toAnomalyRow, toAnomalyTuple)
}
As cvogt already said, you can use nested case classes, they are much easier to work with, see here. I know that HLists are powerful but IMHO people are forcing them too much... What's wrong with case classes? xD
You should compose your AnomalyC class from 2 or more case classes containing <= 22 fields.
As slick does not allow for more than 22 columns and complains that the tupled and unapply methods are not found. The simplest solution is to go for nested case classes if possible. Example :
https://github.com/timgent/spray-slick-template/blob/master/src/main/scala/com/timmeh/openhr/openholidays/model/LargeTableExample1.scala
With HList approach we need to change only the default projection *.
From documentation https://scala-slick.org/doc/3.3.3/cookbook.html

Hex String to Int,Short and Long in Scala

Just can't find a way to transform an Hex String to a number (Int, Long, Short) in Scala.
Is there something like "A".toInt(base)?
You can use the Java libs:
val number = Integer.parseInt("FFFF", 16)
> number: Int = 65535
Or if you are feeling sparky :-):
implicit def hex2int (hex: String): Int = Integer.parseInt(hex, 16)
val number: Int = "CAFE" // <- behold the magic
number: Int = 51966
Also, if you aren't specifically trying to parse a String parameter into hex, note that Scala directly supports hexadecimal Integer literals. In this case:
val x = 0xCAFE
> x: Int = 51966
Isn't Scala wonderful? :-)
7zark7 answer is correct, but I want to make some additions.
Implicit from String to Int can be dangerous. Instead you can use implicit conversion to wrapper and call parsing explicitly:
class HexString(val s: String) {
def hex = Integer.parseInt(s, 16)
}
implicit def str2hex(str: String): HexString = new HexString(str)
val num: Int = "CAFE".hex
What about a one-liner?
def hexToInt(s: String): Int = {
s.toList.map("0123456789abcdef".indexOf(_)).reduceLeft(_ * 16 + _)
}
scala> hexToInt("cafe")
res0: Int = 51966
And to answer your second item:
Is there something like "A".toInt(base)?
Yes, still as a one-liner:
def baseToInt(s: String, base: String): Int = {
s.toList.map(base.indexOf(_)).reduceLeft(_ * base.length + _)
}
scala> baseToInt("1100", "01")
res1: Int = 12
Anyone wanting to convert a UUID from hex to a decimal number can borrow from Benoit's answer and use BigDecimal for the job:
scala> "03cedf84011dd11e38ff0800200c9a66".toList.map(
| "0123456789abcdef".indexOf(_)).map(
| BigInt(_)).reduceLeft( _ * 16 + _)
res0: scala.math.BigInt = 5061830576017519706280227473241971302
Or more generally:
def hex2dec(hex: String): BigInt = {
hex.toLowerCase().toList.map(
"0123456789abcdef".indexOf(_)).map(
BigInt(_)).reduceLeft( _ * 16 + _)
}
def uuid2dec(uuid: UUID): BigInt = {
hex2dec(uuid.toString.replace("-",""))
}
Then:
scala> import java.util.UUID
scala> val id = UUID.fromString("3CEDF84-011D-D11E-38FF-D0800200C9A66")
id: java.util.UUID = 03cedf84-011d-d11e-38ff-0800200c9a66
scala> uuid2dec(id)
res2: BigInt = 5061830576017519706280227473241971302
One practical application for this is encoding the UUID in a barcode, where Code128 produces a shorter barcode for all digits than it does with alphanumeric strings. See notes about subtype "128A" on http://en.wikipedia.org/wiki/Code128#Subtypes.
For Long and Short, it is also possible to use the Java methods directly like
Long2long(java.lang.Long.valueOf(hexString, 16))
where Long2long can be even be omitted in some cases.