Use for questions about Scala's placeholder syntax for anonymous functions.
Questions tagged [scala-placeholder-syntax]
23 questions
                    
                    92
                    
            votes
                
                1 answer
            
        Scala underscore - ERROR: missing parameter type for expanded function
I know there have been quite a few questions on this, but I've created a simple example that I thought should work,but still does not and I'm not sure I understand why
val myStrings = new Array[String](3)
// do some string initialization
// this…
         
    
    
        Jeff Storey
        
- 56,312
- 72
- 233
- 406
                    15
                    
            votes
                
                2 answers
            
        Underscore in Named Arguments
Can someone explain me what exactly is going on here? I am not fully getting into it right now:
val s = Seq(1D,2D,3D,4D)
case class WithUnit(value: Double, unit: String)
s map { WithUnit(_,"cm") } // works
s map { WithUnit(value = _ , unit = "cm") }…
         
    
    
        Peter Schmitz
        
- 5,824
- 4
- 26
- 48
                    8
                    
            votes
                
                2 answers
            
        What are the rules to govern underscore to define anonymous function?
I am using _ as placeholder for creating anonymous function, and the problem is I cannot predict how Scala is going to transform my code. More precisely, it mistakenly determines how "large" the anonymous function I want.
 List(1,2,3) foreach…
         
    
    
        WeiChing 林煒清
        
- 4,452
- 3
- 30
- 65
                    5
                    
            votes
                
                1 answer
            
        Scala underscore use to simplify syntax of function literals
I have the following code:
var x = Array(1,3,4,4,1,1,3)
var m = Int.MaxValue
x.foreach((x)=>(m = m min x))
I tried to simplify last sentence to:
x.foreach((m = _ min m))
But the interpreter says:
scala>  x.foreach((m = _ min m))     
:8:… 
         
    
    
        Stan
        
- 73
- 1
- 4
                    3
                    
            votes
                
                1 answer
            
        Why does ((_: Int, _: Int) => _ / _) not compile when ((_: Int) / (_: Int)) does?
I am learning Scala and have a very basic question. Consider the following two expressions using the placeholder syntax - 
// Syntax A  
val fnA = (_: Int, _: Int) => _ / _
// Syntax B
val fnB = (_: Int) / (_: Int) 
and their attempted…
         
    
    
        Jai Prabhu
        
- 245
- 3
- 10
                    3
                    
            votes
                
                1 answer
            
        Use of underscore in function call with Try parameters
I'm trying to understand particular use of underscore in Scala. And following piece of code I cannot understand
class Test[T, S] {
  def f1(f: T => S): Unit = f2(_ map f)
  def f2(f: Try[T] => Try[S]): Unit = {}
}
How is the _ treated in this…
         
    
    
        yurybubnov
        
- 357
- 2
- 11
                    3
                    
            votes
                
                1 answer
            
        In Scala, what are the rules for making closures with underscores?
At first I had believed that using underscores to make closures (e.g. println _) were just shorthand for using an arrow (e.g. x => println x), but I just recently learned that you can also do the following:
def f(a: Int, b: Int) = a + 2 * b
List(1,…
         
    
    
        math4tots
        
- 8,540
- 14
- 58
- 95
                    2
                    
            votes
                
                2 answers
            
        Explanation of "unbound placeholder parameter"
Question
Why case 2 causes "unbound placeholder parameter" while case 1 is OK?
Case 1
val h: (Int => Int) = (x:Int) => { scala.util.Random.nextInt(x) }
val _h: (Int => Int) = { scala.util.Random.nextInt(_) }
h(5)     // 3
_h(5)    // 3
Case 2
val…
         
    
    
        mon
        
- 18,789
- 22
- 112
- 205
                    2
                    
            votes
                
                2 answers
            
        Scala underScore strange behavior: error: missing parameter type for expanded function
First, this:
"1 2".split(" ").toSet
and this:
Set("1", "2")
both evaluate to the same thing, namely
res1: scala.collection.immutable.Set[String] = Set(1, 2)
Why then, when I do:
Set("1", "2") map (_.toInt)
I get as expected this:
res2:…
         
    
    
        GA1
        
- 1,568
- 2
- 19
- 30
                    2
                    
            votes
                
                0 answers
            
        Scala underscore: error: missing parameter type for expanded function
I recently started learning scala and I was a bit confused by how underscore works. While it in most case gives you convenient anonymous method, sometimes it just confuses compiler (and me). 
For example,
This works 
val randomList =…
         
    
    
        Darin
        
- 143
- 1
- 8
                    1
                    
            vote
                
                2 answers
            
        trying to replace words in dataset (DataFrame)
I am trying to put some kind of placeholder for certain words in my dataset.
However, my method doesnt seem to do anything. I do not get an error but it also doesn't do what it is supposed to do. What am I doing wrong here?
CODE:
wordlist_urls…
         
    
    
        smartypants97
        
- 11
- 3
                    1
                    
            vote
                
                1 answer
            
        Error for parentheses in higher order function definitions (Scala)
I am facing error with round brackets in high-order definition. The following code works fine:
val foo: Int => (Int => Int) = n => n + _*2
However, after adding parentheses compiler error arises
val foo1: Int => (Int => Int) = n => n +…
         
    
    
        Loom
        
- 9,768
- 22
- 60
- 112
                    1
                    
            vote
                
                1 answer
            
        Specify the method signature of the method to apply the eta expansion
Is there a way to specify the signature of the method to which I want to apply the eta expansion?
For example:
val tupleNum = (1L,2L)
case class CaseClass(a:String, b:String)
object CaseClass {
  def apply(a: Long, b: Long): CaseClass = new…
         
    
    
        angelcervera
        
- 3,699
- 1
- 40
- 68
                    1
                    
            vote
                
                1 answer
            
        what is does it mean println(_)?
I have this piece of code in scala
val wordCounts = logData.flatMap(line => line.split(" ")).map(word => (word, 1)).reduceByKey((a, b) => a + b)
wordCounts.foreach(println(_))
So what does println(_) mean and what should it print?
         
    
    
        Khaled
        
- 345
- 5
- 14
                    1
                    
            vote
                
                1 answer
            
        Underscores and string concatenation in List.map with Scala
Scala lets you use an underscore to do a simple map. So for example instead of writing:
def roleCall(people: String*){
  people.toList.map(x => println(x))
}  
...I can instead write:
def roleCall(people: String*){
  people.toList.map(println(_))
}…
         
    
    
        James Crosswell
        
- 648
- 4
- 13