Questions tagged [scala-placeholder-syntax]

Use for questions about Scala's placeholder syntax for anonymous functions.

Spec 6.23.2: Placeholder Syntax for Anonymous Functions

21 questions
91
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
  • 53,386
  • 69
  • 224
  • 390
14
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,734
  • 4
  • 24
  • 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…
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:…
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
  • 207
  • 1
  • 9
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
  • 187
  • 7
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
  • 7,575
  • 11
  • 49
  • 89
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,359
  • 2
  • 17
  • 24
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
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 +…
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
  • 2,705
  • 32
  • 54
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?
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(_)) }…
1
vote
1 answer

I found inconsistency in Scala's underscore

This works: (1 to 5).reduceLeft( _+_ ) but this doesn't: (x:Int,y:Int)=>_+_ :8: error: missing parameter type for expanded function ((x$1, x$2) => x$1.$plus(x$2)) (x:Int,y:Int)=>_+_ ^ :8:…
0
votes
1 answer

How to add text outlines to text within Powerpoint via Apache POI:

Does anyone have an idea how we can add outlines to text (text outline) within powerpoint templates (ppxt) using Apache POI? What I have gathered so far is that the XSLFTextRun class does not have a method to get/ set the text outline for a given…
1
2