docs.scala-lang
docs.scala-lang copied to clipboard
[Tour] Currying section could add more explanations to Partial application
The current Partial application in the multiple parameter lists section is as follows.
val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val numberFunc = numbers.foldLeft(List[Int]()) _
val squares = numberFunc((xs, x) => xs :+ x*x)
println(squares) // List(1, 4, 9, 16, 25, 36, 49, 64, 81, 100)
val cubes = numberFunc((xs, x) => xs :+ x*x*x)
println(cubes) // List(1, 8, 27, 64, 125, 216, 343, 512, 729, 1000)
It'd be better to remind the reader of the syntax of using _ at the end of the numberFunc definition.
Also, it would be helpful to explain the :+ operator and add a hyperlink for more details.
Missing those explanations makes it hard to understand.
@gaopinghuang0 thank you for reporting. I agree with your comments. Would you be interested in contributing a pull request? Also, it is worth noting that in Scala 3 the trailing _ is not anymore needed, so I’m tempted to just leave it out, with a comment below saying that it in Scala 2 we need to add the trailing _.
Regarding “it would be helpful to explain the :+ operator”, I suggest using the appended method instead, what do you think?