Skip to content

Instantly share code, notes, and snippets.

@harmeetsingh0013
Last active January 4, 2018 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harmeetsingh0013/c560d849e6875174cc663678bcb7b496 to your computer and use it in GitHub Desktop.
Save harmeetsingh0013/c560d849e6875174cc663678bcb7b496 to your computer and use it in GitHub Desktop.
def method1(arg: Int): String = {
s"The method1 passed value is $arg"
}
def method2(arg1: Int, arg2: Int): String = {
s"The method2 passed value sum is ${arg1 + arg2}"
}
def method3(arg1: Int, arg2: Int, arg3: Int): String = {
s"The method3 passed value sum is ${arg1 + arg2 + arg3}"
}
def method4(string: String): String = {
s"String length is ${ string.length }"
}
def hofArg1(f: Int => String) = {
println(f(1))
}
def hofArg2(f: (Int, Int) => String) = {
println(f(1, 1))
}
def hofArg3(f: (Int, Int, Int) => String) = {
println(f(1, 1, 1))
}
// higher order functions
hofArg1(method1)
hofArg2(method2)
hofArg3(method3)
// hofArg1(method4) not compiles because function type nu match
// pass by name
def byNameArg(methodValue: => String) = {
println(methodValue)
}
byNameArg(method1(1))
byNameArg(method2(1, 1))
byNameArg(method3(1, 1, 1))
byNameArg(method4("Hello Name"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment