From 56cb32476920f8b374c372022c2d1df611a9473a Mon Sep 17 00:00:00 2001 From: Travis Lee Date: Thu, 16 Mar 2017 14:47:00 +0100 Subject: [PATCH 1/2] Rewrote anonymous function syntax tour --- .../2017-02-13-anonymous-function-syntax.md | 54 ++++--------------- 1 file changed, 11 insertions(+), 43 deletions(-) diff --git a/tutorials/tour/_posts/2017-02-13-anonymous-function-syntax.md b/tutorials/tour/_posts/2017-02-13-anonymous-function-syntax.md index bd0b26bb1b..713c6d21f4 100644 --- a/tutorials/tour/_posts/2017-02-13-anonymous-function-syntax.md +++ b/tutorials/tour/_posts/2017-02-13-anonymous-function-syntax.md @@ -9,68 +9,36 @@ categories: tour num: 7 next-page: higher-order-functions previous-page: mixin-class-composition +prerequisite-knowledge: string-interpolation, expression-blocks --- -Scala provides a relatively lightweight syntax for defining anonymous functions. The following expression creates a successor function for integers: - +Anonymous functions are functions which are not assigned to an identifier. On the left of the arrow `=>` is a parameter list, and on the right is an expression. ```tut (x: Int) => x + 1 ``` -This is a shorthand for the following anonymous class definition: - -```tut -new Function1[Int, Int] { - def apply(x: Int): Int = x + 1 -} -``` +Anonymous functions are not very useful by themselves but they have a value which can be used as an argument of another function or the return value of another function. This will be covered later in the tour. -It is also possible to define functions with multiple parameters: +It is also possible to define functions with multiple parameters and/or an expression block: ```tut -(x: Int, y: Int) => "(" + x + ", " + y + ")" +(x: Int, y: Int) => { + val xSquared = x * x + val ySquared = y * y + s"($xSquared, $ySquared)" +} ``` or with no parameter: ```tut -() => { System.getProperty("user.dir") } +() => System.getProperty("user.dir") ``` -There is also a very lightweight way to write function types. Here are the types of the three functions defined above: +The types of the three functions defined above are written like so: ``` Int => Int (Int, Int) => String () => String ``` - -This syntax is a shorthand for the following types: - -``` -Function1[Int, Int] -Function2[Int, Int, String] -Function0[String] -``` - -The following example shows how to use anonymous function of the beginning of this page - -```tut -object AnonymousFunction { - - /** - * Method to increment an integer by one. - */ - val plusOne = (x: Int) => x + 1 - - /** - * Main method - * @param args application arguments - */ - def main(args: Array[String]) { - - println(plusOne(0)) // Prints: 1 - - } -} -``` From 54ef8ed0625b24583337e033a2ebe6037a8090ad Mon Sep 17 00:00:00 2001 From: Travis Lee Date: Mon, 20 Mar 2017 21:44:31 +0100 Subject: [PATCH 2/2] Deleted redundant anonymous function syntax tour --- .../2017-02-13-anonymous-function-syntax.md | 44 ------------------- 1 file changed, 44 deletions(-) delete mode 100644 tutorials/tour/_posts/2017-02-13-anonymous-function-syntax.md diff --git a/tutorials/tour/_posts/2017-02-13-anonymous-function-syntax.md b/tutorials/tour/_posts/2017-02-13-anonymous-function-syntax.md deleted file mode 100644 index 713c6d21f4..0000000000 --- a/tutorials/tour/_posts/2017-02-13-anonymous-function-syntax.md +++ /dev/null @@ -1,44 +0,0 @@ ---- -layout: tutorial -title: Anonymous Function Syntax - -disqus: true - -tutorial: scala-tour -categories: tour -num: 7 -next-page: higher-order-functions -previous-page: mixin-class-composition -prerequisite-knowledge: string-interpolation, expression-blocks ---- - -Anonymous functions are functions which are not assigned to an identifier. On the left of the arrow `=>` is a parameter list, and on the right is an expression. -```tut -(x: Int) => x + 1 -``` - -Anonymous functions are not very useful by themselves but they have a value which can be used as an argument of another function or the return value of another function. This will be covered later in the tour. - -It is also possible to define functions with multiple parameters and/or an expression block: - -```tut -(x: Int, y: Int) => { - val xSquared = x * x - val ySquared = y * y - s"($xSquared, $ySquared)" -} -``` - -or with no parameter: - -```tut -() => System.getProperty("user.dir") -``` - -The types of the three functions defined above are written like so: - -``` -Int => Int -(Int, Int) => String -() => String -```