I am new to Scala world, I wanted to use String.format() to create a date format string.
I have three integer value year month and day, I wanted to change it in yyyy-mm-dd. String.format() expect an array of Anyref, when I am creating Array[Anyref] by passing integer value to it, it is throwing below error.
Error:(49, 30) the result type of an implicit conversion must be more specific than AnyRef
dd(2) = inputCalendar.get(5)
My full example is :
val dd = new Array[AnyRef](3);
dd(0) = Integer.valueOf(inputCalendar.get(1))
dd(1) = Integer.valueOf(inputCalendar.get(2) + 1)
dd(2) = inputCalendar.get(5)
println(String.format("%04d-%02d-%02d",dd))
Note: I don't want to use any Date API for this.
Declare dd elements as type Int and this should work.
val dd = new Array[Int](3)
. . . //unchanged
String.format("%04d-%02d-%02d",dd:_*)
Or ...
"%04d-%02d-%02d".format(dd:_*)
Related
Using bs-deriving, I can derive e.g. show instances using [#deriving show]. However, it's not clear how I would use the same derivation but providing a custom show instance for a specific datatype.
Example:
[#deriving show]
type bar = |Bar(int);
[#deriving show]
type foo = |Foo(int, bar);
Using the above example, how would I change Bar to print its integer in e.g. hexadecimal?
You should be able to use #printer to define your own printer function like this:
[#deriving show]
type bar = Bar([#printer fmt => fprintf(fmt, "0x%x")] int);
fprintf is a locally defined function which takes a formatter, a format string and a number of values as specified by the format string. For brevity in this case we partially apply it to avoid having to explicitly pass the int value. It's equivalent to (fmt, n) => fprintf(fmt, "0x%x", n).
The format string specifies that the number should be formatted as hexadecimal with lowercase letters (the %x part) and prefixed with 0x. So 31 would output 0x1f.
object LPrimeFactor {
def main(arg:Array[String]):Unit = {
start(13195)
start(600851475143)
}
def start(until:Long){
var all_prime_fac:Array[Int] = Array()
var i = 2
(compile:compileIncremental) Compilation failed
integer number too large
Even though I changed the arg type to Long, it's still not fixed.
Pass the argument as a Long (notice the L at the end of the number):
start(600851475143L)
// ^
To create a Long literal you must add L to the end of it.
start(600851475143L)
Please remember literals values, if you has not any type direct suffix, the compiler try to get your numeric type values, such as 600851475143 as type Int, which is 32-bit length, two complement representation
MIN_VALUE = -2147483648(- 2 ^ 31)
MAX_VALUE = 2147483647(2 ^ 31 - 1)
So please add right suffix on the literal value, as 600851475143L
object LPrimeFactor {
def main(arg:Array[String]):Unit = {
start(13195)
start(600851475143)
}
def start(until:Long){
var all_prime_fac:Array[Int] = Array()
var i = 2
(compile:compileIncremental) Compilation failed
integer number too large
Even though I changed the arg type to Long, it's still not fixed.
Pass the argument as a Long (notice the L at the end of the number):
start(600851475143L)
// ^
To create a Long literal you must add L to the end of it.
start(600851475143L)
Please remember literals values, if you has not any type direct suffix, the compiler try to get your numeric type values, such as 600851475143 as type Int, which is 32-bit length, two complement representation
MIN_VALUE = -2147483648(- 2 ^ 31)
MAX_VALUE = 2147483647(2 ^ 31 - 1)
So please add right suffix on the literal value, as 600851475143L
I receive in the method printfields a vector[String] which I am printing as follows:
def printFields(fields: Vector[String]): Unit =
{
printf(fields.map(_ => "%s").mkString("",",","\n"),fields: _*)
println(fields)
}
now this give me as output the following:
39,39,35,30
Vector(39, 39, 35,30)
28,28,35,30
Vector(28, 28, 35,30)
Now, Each number correspond to an Id, I need to apply a function to each number that appear here in order to print the element that correspond, in other words, make something like:
printf(fields.map(_ => "%s").mkString("",",","\n"),con.convI2N((fields: _*).toInt))
I try with converting the function to an Iterator, but give me Strings like
39
39
35,30
The last String can not be converted toInt, then this is not an option,
Someone can help me?
Thank you so much
What about converting the Vector[String] to a Vector[Int] as preliminary operation?
fields.map(_.split(',')).flatten.map(_.toInt)
This is just an hint, it is not the safer way, you should check that every String in your Vector is actually an Int or a sequence of comma-separated Ints.
New to Scala and see people are using sign f ahead of a string, here is an example I tried which works. Wondering what is the function of sign f? Does it need to be combined to use with %s? Tried to search some tutorials but failed. Thanks.
object HelloWorld {
def main(args: Array[String]) {
var start = "Monday";
var end = "Friday";
var palindrome = "Dot saw I was Tod";
println(f"date >= $start%s and date <= $end%s" + palindrome);
// output date >= Monday and date <= FridayDot saw I was Tod
}
}
http://docs.scala-lang.org/overviews/core/string-interpolation.html
The f Interpolator
Prepending f to any string literal allows the creation of simple
formatted strings, similar to printf in other languages. When using
the f interpolator, all variable references should be followed by a
printf-style format string, like %d.
PS. another somewhat related feature is http://docs.scala-lang.org/overviews/quasiquotes/expression-details
See the explanation here. For people coming from C the f interpolator is a printf style formatter. % is to denote the type of data and with a $ you may may refer to a previously defined variable.
The % in not mandatory. Its just that you will get a format that is decided by the compiler at compile time. Bit uyou may want to change the output format sometimes.
So if i take an example ,
var start = "Monday";
var end = "Friday";
val age = 33
var palindrome = "Dot saw I was Tod";
println(f"date >= $start and date <= $end and age<= $age%f" + palindrome);
I could omit the %f and i will see a output of 33 as it will inferred as Int. However i could use %f if i wanted to format it as a float. Also if you use a incompatible formatted you will receive a error at compile time.