Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/main/scala/stdlib/HigherOrderFunctions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ object HigherOrderFunctions
result2 should be(res1)
}

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

/** Function returning another function:
/** Higher Order Function returning another function:
*/
def returningFunctionHigherOrderFunctions(res0: Boolean, res1: Int, res2: Int) {
def addWithoutSyntaxSugar(x: Int): Function1[Int, Int] = {
Expand Down Expand Up @@ -145,7 +148,8 @@ object HigherOrderFunctions
def functionAsParameterHigherOrderFunctions(
res0: List[String],
res1: List[String],
res2: List[Int]) {
res2: List[String],
res3: List[Int]) {
def makeUpper(xs: List[String]) = xs map {
_.toUpperCase
}
Expand All @@ -160,9 +164,10 @@ object HigherOrderFunctions
}) should be(res1)

//using it inline
List("Scala", "Erlang", "Clojure") map {
_.length
} should be(res2)
val myName = (name: String) => s"My name is $name"
makeWhatEverYouLike(List("John", "Mark"), myName) should be(res2)

List("Scala", "Erlang", "Clojure") map (_.length) should be(res3)
}

}
3 changes: 2 additions & 1 deletion src/test/scala/stdlib/HigherOrderFunctionsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ class HigherOrderFunctionsSpec extends Spec with Checkers {
Test
.testSuccess(
HigherOrderFunctions.functionAsParameterHigherOrderFunctions _,
List("ABC", "XYZ", "123") :: List("abc", "xyz", "123") :: List(5, 6, 7) :: HNil
List("ABC", "XYZ", "123") :: List("abc", "xyz", "123") ::
List("My name is John", "My name is Mark") :: List(5, 6, 7) :: HNil
)
)
}
Expand Down