-
Notifications
You must be signed in to change notification settings - Fork 2
/
HigherOrderFuncDemo_5.scala
36 lines (25 loc) · 1000 Bytes
/
HigherOrderFuncDemo_5.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package ScalaBasic
// https://www.scala-exercises.org/std_lib/higher_order_functions
// Higher Order Functions: Higher Order Functions are functions that take functions as arguments and/or return functions.
object HigherOrderFuncDemo_5 extends App{
// Function taking another function as a parameter. Helps in composing functions.
// Hint: a map method applies the function to each element of a list.
def makeUpper(xs: List[String]) =
xs map {
_.toUpperCase
}
def makeLower(xs: List[String]) =
xs map {
_.toLowerCase
}
def makeWhatEverYouLike(xs: List[String], sideEffect: String => String) =
xs map sideEffect
// run
println(makeUpper(List("abc", "xyz", "123")))
println(makeLower(List("abc", "xyz", "123")))
println(makeWhatEverYouLike(List("ABC", "XYZ", "123"), x => x.toLowerCase))
// run inline
val myName = (name: String) => s"My name is $name"
println(makeWhatEverYouLike(List("John", "Mark"), myName))
println(List("Scala", "Erlang", "Clojure") map (_.length))
}