Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
63 lines (41 sloc) 1.38 KB
package scalaBasics.functionalProgramming
object WhatsAFunction extends App {
val doubler = new MyFunction[Int, Int] {
override def apply(element: Int): Int = element * 2
}
println(doubler(2))
val stringToIntConverter = new Function1[String, Int] {
override def apply(string: String): Int = string.toInt
}
println(stringToIntConverter("3") + 4)
val adder = new Function2[Int, Int, Int] {
override def apply(a: Int, b: Int): Int = a + b
}
val adder2: Function2[Int, Int, Int] = new Function2[Int, Int, Int] {
override def apply(a: Int, b: Int): Int = a + b
}
val added3: ((Int, Int) => Int) = new Function2[Int, Int, Int] {
override def apply(a: Int, b: Int): Int = a + b
}
def concatenator: (String, String) => String = new Function2[String, String, String] {
override def apply(a: String, b: String): String = a + b
}
println(concatenator("Hello", "Bob"))
val superAdder: Function1[Int, Function1[Int, Int]] = new Function1[Int, Function1[Int, Int]] {
override def apply(x: Int): Function1[Int, Int] = new Function1[Int, Int] {
override def apply(y: Int): Int = x + y
}
}
val adder3 = superAdder(3)
println(adder3(4))
println(superAdder(3)(4))
}
class Action {
def execute(element: Int): String = ???
}
trait Action2[A, B] {
def execute(element: A): B
}
trait MyFunction[A, B] {
def apply(element: A): B
}
You can’t perform that action at this time.