lundi 29 septembre 2014

understanding scala : currying


Vote count:

0




I recently started learning Scala and came across currying. From an answer in this post, this code snippet





def sum(a: Int)(b: Int) = a + b


expands out to this





def sum(a: Int): Int => Int = b => a + b


Then I saw a snippet from scala-lang, which shows it's possible to write something like this to emulate a while loop





def whileLoop (cond : => Boolean) (body : => Unit) : Unit = {
if (cond) {
body
whileLoop (cond) (body)
}
}



Out of curiosity, I tried to expand this out, and got this





def whileLoop2 (cond : => Boolean) : (Unit => Unit) =
(body : => Unit) =>
if (cond) {
body
whileLoop2 (cond) (body)
}



But there seems to be some syntax that I'm missing because I get an error



error: identifier expected but '=>' found.
(body : => Unit) =>
^


What is the proper way to expand out the emulated while loop?



asked 13 secs ago







understanding scala : currying

Aucun commentaire:

Enregistrer un commentaire