I believe, what you want to achieve, is to get an index of a String
element(if start counting from 1) in varargs, or return "No one"
. No need to pass indices to the method. You can do it like this:
def hello(name: String, params: String*): Any = {
val idx = params.indexOf(name)
if (idx != -1) idx + 1 else "No One"
}
Unfortunately both this:
def Hello(name: String, args: Any* ) = {
val index = args.indexOf(name)
if(index == -1)
args(args.length - 1)
else
args(index + 1)
}
and this:
def hello(name: String, param: Any*): Any= {
var index = 0
while(index < param.length) {
var n = name
var ar = param(index)
if ( n.equals(ar) ) return param(index + 1)
else index = index + 1
}
param(index -1)
}
are broken, as they throw an exception if you try to find the index of "No one"
, as index + 1
will equal to the size of the array. And it's better to compare things in Scala with ==
for logical equality.
But it's better not to return Any
at all, but return Option[Int]
:
def hello(name: String, params: String*): Option[Int] =
Option(params.indexOf(name)).filter(_ != -1).map(_ + 1)
So then you can use it like this:
val message1 = hello("Jack", "rakesh" ,"Jack").getOrElse("No one")
val message2 = hello("ABC", "rakesh", "Jack", "Vik", "ram").getOrElse("No one")
Answering the comment:
I want to know how can i pass mixed datatypes to "param".
The simplest way is to have them all of type Any
and also get string or integer as return type
The same way, defining return type as Any
The only small issue here, is that there will be no compile time check against other types. E.g. somebody might pass Boolean
or any complex object along with String
's and Int
's to your function. But you can check at runtime against it or play with types to limit them. I don't know your requirement here, maybe it's even advantage for you.
If having Any
is fine, then I would solve it like this:
def Hello(name: Any, params: Any*): Any = Option(params)
.withFilter(_.nonEmpty)
.map(_.indexOf(name))
.filter(i => i != -1 && i < params.length - 1)
.map(i => params(i + 1))
.getOrElse("No one")
Or, if you can assume, params
are never empty and you have to use the last param as the default, instead of just hard coded "No one"
:
def Hello(name: Any, params: Any*): Any = Option(params)
.withFilter(_.nonEmpty)
.map(_.indexOf(name))
.filter(i => i != -1 && i < params.length - 1)
.map(i => params(i + 1))
.getOrElse(params.last)
Notice the check against "No one"
attack: i < params.length - 1
.
Notice that name
now is also of type Any
.
Now, even if you pass "No one"
as a name
, the Option
will evaluate to None
thanking to the filter, and getOrElse
will give you the default "No one"
instead of an exception.
Any
can be helpful although not exact?def func1(a: String, s: Any*): Any
– Sabuj Hassandef Hello(name: String, Param: Either[Array[Int]/Array[String]]): Either[Int/String]
? You could overload it for varargs and have common code in function withEither
. – Mateusz Kubuszok