// Inline add and addR
def sum(ns: Int*): Int = ns.foldLeft(0)((n, m) => n + m) // inlined add
scala> sum(33, 42, 99)
res2: Int = 174 // alright
def sumR(ns: Int*): Int = ns.foldLeft(0)((n, m) => return n + m) // inlined addR
scala> sumR(33, 42, 99)
res3: ???
What will this expression return and why?
Answer
33// um.
A return expression, when evaluated, abandons the current computation and returns to the caller of the method in which return appears.
Tags
#scala
Question
// Inline add and addR
def sum(ns: Int*): Int = ns.foldLeft(0)((n, m) => n + m) // inlined add
scala> sum(33, 42, 99)
res2: Int = 174 // alright
def sumR(ns: Int*): Int = ns.foldLeft(0)((n, m) => return n + m) // inlined addR
scala> sumR(33, 42, 99)
res3: ???
What will this expression return and why?
Answer
?
Tags
#scala
Question
// Inline add and addR
def sum(ns: Int*): Int = ns.foldLeft(0)((n, m) => n + m) // inlined add
scala> sum(33, 42, 99)
res2: Int = 174 // alright
def sumR(ns: Int*): Int = ns.foldLeft(0)((n, m) => return n + m) // inlined addR
scala> sumR(33, 42, 99)
res3: ???
What will this expression return and why?
Answer
33// um.
A return expression, when evaluated, abandons the current computation and returns to the caller of the method in which return appears.
If you want to change selection, open original toplevel document below and click on "Move attachment"
Parent (intermediate) annotation
Open it Int*): Int = ns.foldLeft(0)((n, m) => n + m) // inlined add
scala> sum(33, 42, 99)
res2: Int = 174 // alright
def sumR(ns: Int*): Int = ns.foldLeft(0)((n, m) => return n + m) // inlined addR
scala> sumR(33, 42, 99)
res3: Int = <span>33 // um.
<span><body><html>
Original toplevel document
tpolecat (addR)
scala> sumR(33, 42, 99)
res1: Int = 174
So far so good. There is no apparent difference between sum and sumR which may lead you to think that return is simply an optional keyword. But let's refactor a bit by inlining add and addR.
<span>// Inline add and addR
def sum(ns: Int*): Int = ns.foldLeft(0)((n, m) => n + m) // inlined add
scala> sum(33, 42, 99)
res2: Int = 174 // alright
def sumR(ns: Int*): Int = ns.foldLeft(0)((n, m) => return n + m) // inlined addR
scala> sumR(33, 42, 99)
res3: Int = 33 // um.
What the what?
So, the short version is:
A return expression, when evaluated, abandons the current computation and returns to the caller of the method in which return appears.
So in
Summary
status
not learned
measured difficulty
37% [default]
last interval [days]
repetition number in this series
0
memorised on
scheduled repetition
scheduled repetition interval
last repetition or drill
Details
No repetitions
Discussion
Do you want to join discussion? Click here to log in or create user.