From 8a9be51675baff48859b6d758806434d0b318939 Mon Sep 17 00:00:00 2001 From: Trevor Date: Thu, 16 Mar 2017 15:39:53 +0100 Subject: [PATCH 1/2] Rewrote nested functions tour --- .../_posts/2017-02-13-nested-functions.md | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/tutorials/tour/_posts/2017-02-13-nested-functions.md b/tutorials/tour/_posts/2017-02-13-nested-functions.md index cb9ac90321..48d4bae8b0 100644 --- a/tutorials/tour/_posts/2017-02-13-nested-functions.md +++ b/tutorials/tour/_posts/2017-02-13-nested-functions.md @@ -1,6 +1,6 @@ --- layout: tutorial -title: Nested Functions +title: Nested Methods disqus: true @@ -11,25 +11,27 @@ next-page: currying previous-page: higher-order-functions --- -In Scala it is possible to nest function definitions. The following object provides a `filter` function for extracting values from a list of integers that are below a threshold value: +In Scala it is possible to nest function definitions. The following object provides a `factorial` function for computing the factorial of a given number: ```tut -object FilterTest extends App { - def filter(xs: List[Int], threshold: Int) = { - def process(ys: List[Int]): List[Int] = - if (ys.isEmpty) ys - else if (ys.head < threshold) ys.head :: process(ys.tail) - else process(ys.tail) - process(xs) +object FactorialTest extends App { + def factorial(x: Int): Int = { + def fact(x: Int, accumulator: Int): Int = { + if (x <= 1) accumulator + else fact(x - 1, x * accumulator) + } + fact(x, 1) } - println(filter(List(1, 9, 2, 8, 3, 7, 4), 5)) + println("Factorial of 2: " + factorial(2)) + println("Factorial of 3: " + factorial(3)) } ``` -_Note: the nested function `process` refers to variable `threshold` defined in the outer scope as a parameter value of `filter`._ +_Note: the nested function `fact` refers to variable `x` defined in the outer scope as a parameter value of `factorial`._ The output of this program is: ``` -List(1,2,3,4) +Factorial of 2: 2 +Factorial of 3: 6 ``` From a191db0464a6858e166d91b9f33a0de61a38e4fc Mon Sep 17 00:00:00 2001 From: Trevor Date: Thu, 16 Mar 2017 17:41:40 +0100 Subject: [PATCH 2/2] rewrote in worksheet mode --- tutorials/tour/_posts/2017-02-13-nested-functions.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tutorials/tour/_posts/2017-02-13-nested-functions.md b/tutorials/tour/_posts/2017-02-13-nested-functions.md index 48d4bae8b0..f995c50186 100644 --- a/tutorials/tour/_posts/2017-02-13-nested-functions.md +++ b/tutorials/tour/_posts/2017-02-13-nested-functions.md @@ -14,17 +14,16 @@ previous-page: higher-order-functions In Scala it is possible to nest function definitions. The following object provides a `factorial` function for computing the factorial of a given number: ```tut -object FactorialTest extends App { - def factorial(x: Int): Int = { + def factorial(x: Int): Int = { def fact(x: Int, accumulator: Int): Int = { if (x <= 1) accumulator else fact(x - 1, x * accumulator) } fact(x, 1) - } - println("Factorial of 2: " + factorial(2)) - println("Factorial of 3: " + factorial(3)) -} + } + + println("Factorial of 2: " + factorial(2)) + println("Factorial of 3: " + factorial(3)) ``` _Note: the nested function `fact` refers to variable `x` defined in the outer scope as a parameter value of `factorial`._