From db12075258ee301ff1fa6c3dd72957c905a4fb93 Mon Sep 17 00:00:00 2001 From: Joe Conley Date: Thu, 26 Jan 2017 11:29:03 -0500 Subject: [PATCH] Adding closure documentation Also fixing the previous question so that the description is before the actual code. --- src/main/scala/stdlib/HigherOrderFunctions.scala | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/scala/stdlib/HigherOrderFunctions.scala b/src/main/scala/stdlib/HigherOrderFunctions.scala index 3ddfd1bd..8b7bdf71 100644 --- a/src/main/scala/stdlib/HigherOrderFunctions.scala +++ b/src/main/scala/stdlib/HigherOrderFunctions.scala @@ -46,11 +46,13 @@ object HigherOrderFunctions extends FlatSpec with Matchers with org.scalaexercis result should be(res0) } - /** {{{ + /** Here the only variable used in the function body, `i * 10`, is `i`, which is defined as a parameter to the function. + * + *{{{ * val multiplier = (i:Int) => i * 10 * }}} * - * Here the only variable used in the function body, `i * 10`, is `i`, which is defined as a parameter to the function. + * A closure is a function which maintains a reference to one or more variables outside of the function scope (it "closes over" the variables). Scala will detect that you are using variables outside of scope and create an object instance to hold the shared variables. */ def meetClosureHigherOrderFunctions(res0: Int, res1: Int) { var incrementer = 1