From 4f0066e79b1bba0f4cf9de2a7250c6f628236b5e Mon Sep 17 00:00:00 2001 From: Jesko Jochum Date: Tue, 15 Nov 2016 15:58:06 +0100 Subject: [PATCH 1/3] Added an example on how to use anonymous functions in the "Anonymous Function Syntax" page (tutorials/tour/anonymous-function-syntax.md). --- tutorials/tour/anonymous-function-syntax.md | 30 +++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tutorials/tour/anonymous-function-syntax.md b/tutorials/tour/anonymous-function-syntax.md index 7d2f12138a..25d2a43460 100644 --- a/tutorials/tour/anonymous-function-syntax.md +++ b/tutorials/tour/anonymous-function-syntax.md @@ -51,3 +51,33 @@ 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 +package tour + +object AnonymousFunction { + + /** + * Method to increment an integer by one. + */ + var anonymousIncrementFunction = (x: Int) => x + 1 + + /** + * Main method + * @param args application arguments + */ + def main(args: Array[String]) { + + // Create an integer to test the anonymous function with + var myInteger: Int = 0; + + println(myInteger) // Prints: 0 + + myInteger = anonymousIncrementFunction(myInteger) + + println(myInteger) // Prints: 1 + } +} +``` From db85835bd5f60d410359ce245a8af00eed99647f Mon Sep 17 00:00:00 2001 From: Jesko Jochum Date: Mon, 5 Dec 2016 09:08:42 +0100 Subject: [PATCH 2/3] Updated example on how to use anonymous functions. --- tutorials/tour/anonymous-function-syntax.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tutorials/tour/anonymous-function-syntax.md b/tutorials/tour/anonymous-function-syntax.md index 25d2a43460..128190104a 100644 --- a/tutorials/tour/anonymous-function-syntax.md +++ b/tutorials/tour/anonymous-function-syntax.md @@ -55,8 +55,6 @@ Function0[String] The following example shows how to use anonymous function of the beginning of this page ```tut -package tour - object AnonymousFunction { /** @@ -71,7 +69,7 @@ object AnonymousFunction { def main(args: Array[String]) { // Create an integer to test the anonymous function with - var myInteger: Int = 0; + var myInteger = 0 println(myInteger) // Prints: 0 From 78180d94d18d05afca272fa858a9775d12357c03 Mon Sep 17 00:00:00 2001 From: Jesko Jochum Date: Wed, 11 Jan 2017 08:47:53 +0100 Subject: [PATCH 3/3] Updated example on how to use anonymous functions. Omitted the, apparently for Scala unconventional, usage of 'var's in the code. Example is now slimmer, i.e. there is only one line in the main method where the anonymous function is called once and the return value is immediately printed. --- tutorials/tour/anonymous-function-syntax.md | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/tutorials/tour/anonymous-function-syntax.md b/tutorials/tour/anonymous-function-syntax.md index 77a9af4421..5f99fba246 100644 --- a/tutorials/tour/anonymous-function-syntax.md +++ b/tutorials/tour/anonymous-function-syntax.md @@ -60,7 +60,7 @@ object AnonymousFunction { /** * Method to increment an integer by one. */ - var anonymousIncrementFunction = (x: Int) => x + 1 + val plusOne = (x: Int) => x + 1 /** * Main method @@ -68,14 +68,8 @@ object AnonymousFunction { */ def main(args: Array[String]) { - // Create an integer to test the anonymous function with - var myInteger = 0 + println(plusOne(0)) // Prints: 1 - println(myInteger) // Prints: 0 - - myInteger = anonymousIncrementFunction(myInteger) - - println(myInteger) // Prints: 1 } } ```