Skip to content

Commit a813ccd

Browse files
Merge pull request #84 from Yaskier/patch-2
Higher Order Functions better explanations
2 parents 75b18a4 + 9f17df4 commit a813ccd

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/main/scala/stdlib/HigherOrderFunctions.scala

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ object HigherOrderFunctions
8686
result2 should be(res1)
8787
}
8888

89-
/** We can take that closure and throw it into a method and it will still hold the environment:
89+
/** And then we get to Higher Order Functions:
90+
* Higher Order Functions are functions that take functions as arguments and/or return functions.
91+
*
92+
* We can take that closure and throw it into a Higher Order Function and it will still hold the environment:
9093
*/
9194
def holdEnvironmentHigherOrderFunctions(res0: Int, res1: Int) {
9295
def summation(x: Int, y: Int Int) = y(x)
@@ -102,7 +105,7 @@ object HigherOrderFunctions
102105
result2 should be(res1)
103106
}
104107

105-
/** Function returning another function:
108+
/** Higher Order Function returning another function:
106109
*/
107110
def returningFunctionHigherOrderFunctions(res0: Boolean, res1: Int, res2: Int) {
108111
def addWithoutSyntaxSugar(x: Int): Function1[Int, Int] = {
@@ -145,7 +148,8 @@ object HigherOrderFunctions
145148
def functionAsParameterHigherOrderFunctions(
146149
res0: List[String],
147150
res1: List[String],
148-
res2: List[Int]) {
151+
res2: List[String],
152+
res3: List[Int]) {
149153
def makeUpper(xs: List[String]) = xs map {
150154
_.toUpperCase
151155
}
@@ -160,9 +164,10 @@ object HigherOrderFunctions
160164
}) should be(res1)
161165

162166
//using it inline
163-
List("Scala", "Erlang", "Clojure") map {
164-
_.length
165-
} should be(res2)
167+
val myName = (name: String) => s"My name is $name"
168+
makeWhatEverYouLike(List("John", "Mark"), myName) should be(res2)
169+
170+
List("Scala", "Erlang", "Clojure") map (_.length) should be(res3)
166171
}
167172

168173
}

src/test/scala/stdlib/HigherOrderFunctionsSpec.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ class HigherOrderFunctionsSpec extends Spec with Checkers {
8080
Test
8181
.testSuccess(
8282
HigherOrderFunctions.functionAsParameterHigherOrderFunctions _,
83-
List("ABC", "XYZ", "123") :: List("abc", "xyz", "123") :: List(5, 6, 7) :: HNil
83+
List("ABC", "XYZ", "123") :: List("abc", "xyz", "123") ::
84+
List("My name is John", "My name is Mark") :: List(5, 6, 7) :: HNil
8485
)
8586
)
8687
}

0 commit comments

Comments
 (0)