From 1dd39529f087857b6cea796be4bb02ac9751ca25 Mon Sep 17 00:00:00 2001 From: technical-harsh90 <40689176+technical-harsh90@users.noreply.github.com> Date: Fri, 27 Jul 2018 15:49:13 +0530 Subject: [PATCH 1/2] Updated the docs for Multiple Parameter Listing Here i added the, - Another type of statement used to denote foldLeft, - Another type of statement used to denote foldRight - All generic statements used for fold family --- _tour/multiple-parameter-lists.md | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/_tour/multiple-parameter-lists.md b/_tour/multiple-parameter-lists.md index 113ae59f81..00ca836b74 100644 --- a/_tour/multiple-parameter-lists.md +++ b/_tour/multiple-parameter-lists.md @@ -45,8 +45,22 @@ numbers.foldLeft(0, {(m: Int, n: Int) => m + n}) ``` numbers.foldLeft(0)(_ + _) ``` - - Also, it allows us to fix the parameter `z` and pass around a partial function and reuse it as shown below: + + Finally, `foldLeft` and `foldRight` can be used in any of the following terms, +```tut +val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) + +numbers.foldLeft(0)((sum, item) => sum + item) // Generic Form +numbers.foldRight(0)((sum, item) => sum + item) // Generic Form + +numbers.foldLeft(0)(_+_) // Curried Form +numbers.foldRight(0)(_+_) // Curried Form + +(0 /: numbers)(_+_) // Used in place of foldLeft +(numbers :\ 0)(_+_) // Used in place of foldRight +``` + + Above statement `numbers.foldLeft(0)(_ + _)` allows us to fix the parameter `z` and pass around a partial function and reuse it as shown below: ```tut val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val numberFunc = numbers.foldLeft(List[Int]())_ @@ -64,4 +78,4 @@ print(cubes.toString()) // List(1, 8, 27, 64, 125, 216, 343, 512, 729, 1000) ``` def execute(arg: Int)(implicit ec: ExecutionContext) = ??? ``` - \ No newline at end of file + From 18a1a6ea7efdba7220c3f1ac5506c19697c986b0 Mon Sep 17 00:00:00 2001 From: technical-harsh90 <40689176+technical-harsh90@users.noreply.github.com> Date: Wed, 1 Aug 2018 10:03:57 +0530 Subject: [PATCH 2/2] Updated the order of my changes --- _tour/multiple-parameter-lists.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/_tour/multiple-parameter-lists.md b/_tour/multiple-parameter-lists.md index 00ca836b74..7d4ae731e1 100644 --- a/_tour/multiple-parameter-lists.md +++ b/_tour/multiple-parameter-lists.md @@ -44,6 +44,17 @@ numbers.foldLeft(0, {(m: Int, n: Int) => m + n}) ``` numbers.foldLeft(0)(_ + _) +``` + Above statement `numbers.foldLeft(0)(_ + _)` allows us to fix the parameter `z` and pass around a partial function and reuse it as shown below: +```tut +val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) +val numberFunc = numbers.foldLeft(List[Int]())_ + +val squares = numberFunc((xs, x) => xs:+ x*x) +print(squares.toString()) // List(1, 4, 9, 16, 25, 36, 49, 64, 81, 100) + +val cubes = numberFunc((xs, x) => xs:+ x*x*x) +print(cubes.toString()) // List(1, 8, 27, 64, 125, 216, 343, 512, 729, 1000) ``` Finally, `foldLeft` and `foldRight` can be used in any of the following terms, @@ -60,18 +71,7 @@ numbers.foldRight(0)(_+_) // Curried Form (numbers :\ 0)(_+_) // Used in place of foldRight ``` - Above statement `numbers.foldLeft(0)(_ + _)` allows us to fix the parameter `z` and pass around a partial function and reuse it as shown below: -```tut -val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) -val numberFunc = numbers.foldLeft(List[Int]())_ - -val squares = numberFunc((xs, x) => xs:+ x*x) -print(squares.toString()) // List(1, 4, 9, 16, 25, 36, 49, 64, 81, 100) - -val cubes = numberFunc((xs, x) => xs:+ x*x*x) -print(cubes.toString()) // List(1, 8, 27, 64, 125, 216, 343, 512, 729, 1000) -``` - + #### Implicit parameters To specify certain parameters in a parameter list as `implicit`, multiple parameter lists should be used. An example of this is: