This question already has an answer here:
using variable length argument in scala
(1 answer)
Closed 7 years ago.
I have an interface
object Leaf {
def apply(keys: Int*): Leaf = {
new Leaf(keys)
}
}
where Leaf class is defined as follows:
class Leaf(keys: Seq[Int]) extends Node(true, keys, Seq())
Is this possible to pass a Sequence as a keys parameter? Of course I could create second varargs method, but i wonder if there is a method which converts a Sequence into varargs paremeter.
Yes, and I think you mean varargs not varchar :)
Leaf(sequence: _*)
Related
This question already has answers here:
What is the apply function in Scala?
(7 answers)
Closed 1 year ago.
I have below "apply" function, what does apply function do? can you please explain for me?
def apply[K,V](m1:Map[K,V], m2:Map[K,V], merge: (V,V) => V): Map[K,V] =
combine(m1,m2,merge)
The apply function in companion objects and classes has a special meaning in that if an object/class is instantiated without specifying the function, it is the one which is run.
E.g.
object DDF { def apply(text :String) = println(s"got $text") }
DDF("text") // object called like function. function actually called is apply(String) here.
In your example, the apply function merely calls combine. so X(a,b,c) is the same as X.combine(a,b,c).
The technique is how we have achieve multiple constructors on classes too.
This question already has answers here:
How to pass Scala array into Scala vararg method?
(3 answers)
Closed 4 years ago.
Is there a way to declare a variable of type String* in scala? As in a variable number of arguments? The issue is that when I want to test a series of methods that takes in a String* as a parameter and don't want to just replicate the values I pass in every test. I know that I can change the functions to take in a collection of String like Array or Seq, but I wanted to know if there was a way to do it without changing the parameter types
Varargs notation:
def foo(ss :String*) = {
//ss is Seq[String], you can ss.map(), ss.length, etc.
}
usage:
foo()
foo("this", "that")
foo("abc", "abd", "abx")
val someList = List("another" , "collection", "of", "strings")
foo(someList :_*) // turn a collection into individual varargs parameters
This question already has answers here:
In Scala, is there an easy way to convert a case class into a tuple?
(5 answers)
Closed 5 years ago.
I have a case class with two fields and a function which accepts the same arguments. Is where a way to pass contents of case class to this function without explicitly specifying all fields or ugly unapplying?
case class User(name: String, age: Int)
val user = User("Peter", 23)
def func(name: String, age: Int) = {...}
func(user.name, user.age) //works
Function.tupled(func(_,_))(User.unapply(user).get) //works, but ugly
// func(_,_) is explicit because it can be a method of a class
The simplest vanilla Scala solution is the one Tom suggested in the comments:
(func _).tupled(User.unapply(user).get)
If happen to have Shapeless in scope (maybe it's already a dependency), you could also use it to convert user to a tuple:
import shapeless._
val gen = Generic[User]
(func _).tupled(gen.to(user).tupled)
This question already has answers here:
Scala foreach strange behaviour
(5 answers)
Closed 7 years ago.
Given these case classes:
case class FeatureDistance(id: Long, distance: Double)
case class SearchResult(score: Float, id: Long)
Why does this not compile?
val distances = List[FeatureDistance](FeatureDistance(1L, 10f))
val results = distances.map(SearchResult(0f, _.id))
But this does:
val results = distances.map(fd => SearchResult(0f, fd.id))
The compilation error says: missing parameter type for expanded function ((x$3) => x$3.id)
Is it because _ is only scoped to the map function so it's not visible in the SearchResult.apply call?
After doing a bit of research, I found a post on the old scala forums that contains this quote:
When you use "_" as a place holder for an anonymous parameter of a function, the scope of that function is the innermost parenthesis containing it.
So, it's just a question of scope. I suspect this has to do with problems that could otherwise result from having nested function calls that use more than one underscore. For instance:
//suppose we have some x:List[List[Int]]
x.map(_.map(_ + 1))
This question already has answers here:
Why can't the first parameter list of a class be implicit?
(2 answers)
Closed 8 years ago.
The class definition with compiling error:
class Foo(implicit x : Int) {
def this(s : String) = this(s.length)
}
and the class definition can pass compiling
class Foo(implicit x : Int) {
def this(s : String) = this()(s.length)
}
from my point view, the first definition is just correct.
Since the auxiliary constructor explicitly called the primary constructor which expected a Integer as parameter, seems nothing wrong.
And for the second class definition which pass compiling, actually I don't quite understand why it works.
In Scala every auxiliary constructor must invoke another constructor of the same class as its first action.
Also, you do not have a constructor with an Int parameter.
You have constructor with an implicit (Int) parameter.
You can either provide the implicit parameter in the scope or pass it explicitly writing something like this()(9)