Anonymous functions in Scala are nameless , lightweight functions which can be passed around without any side affects. They are also called as function literals, lambda functions, lambda expressions or simply lambdas. (x,y)=>x+y is an anonymous function which means that the input to the function are two variables and the result will be the sum of them. Another common lambda expression is an underscore form eg _+1 which means the function is an increment function.
Monomorphic functions are those functions which have only one type. Polymorphic functions are functions which are generic and can work for different datatypes. Following example shows the usage of anonymous function, Monomorphic and Polymorphic functions and tail recursion in Scala.
object Demo{
def main(args:Array[Strings]):Unit={
var arrD= new Array[Double](4)
arrD(0) = 2.0
arrD(1) = 4.0
arrD(2) = 6.0
arrD(3) = 7.0
println(binarySearch(arrD,7.0))
println(binSearch[Double](arrD, 7.0, (a:Double, b:Double)=> a>b))
}
// Monomorphic binary search with tail recursion
def binarySearch(ds: Array[Double], key: Double): Int = {
@annotation.tailrec
def go(low: Int, mid: Int, high: Int): Int = {
if (low > high) -mid - 1
else {
val mid2 = (low + high) / 2
val d = ds(mid2)
if (d == key) mid2
else if (d > key) go(low, mid2, mid2 - 1)
else go(mid2 + 1, mid2, high)
}
}
go(0, 0, ds.length - 1)
}
// return the index of the searched data if found else
// a negative number if data doesn't exist
// takes an anonymous function/lambda as a parameter for comparison
//this is a generic method which takes any data type
def binSearch[X]( data: Array[X], key:X , gt: (X,X) => Boolean):Int={
@annotation.tailrec
def go(low:Int, mid:Int, high:Int ):Int={
if( low > high) -mid-1
else{
var mid2 = (low + high) /2
var d= data(mid2)
val greater = gt(d,key)
if ( !greater && !gt(key,d)) mid2
else if (greater) go(low,mid2,mid2-1)
else go(mid2+1,mid2,high )
}
}
go(0,0,data.length-1)
}
}
Let me know if you have any comments on the same.
Monomorphic functions are those functions which have only one type. Polymorphic functions are functions which are generic and can work for different datatypes. Following example shows the usage of anonymous function, Monomorphic and Polymorphic functions and tail recursion in Scala.
object Demo{
def main(args:Array[Strings]):Unit={
var arrD= new Array[Double](4)
arrD(0) = 2.0
arrD(1) = 4.0
arrD(2) = 6.0
arrD(3) = 7.0
println(binarySearch(arrD,7.0))
println(binSearch[Double](arrD, 7.0, (a:Double, b:Double)=> a>b))
}
// Monomorphic binary search with tail recursion
def binarySearch(ds: Array[Double], key: Double): Int = {
@annotation.tailrec
def go(low: Int, mid: Int, high: Int): Int = {
if (low > high) -mid - 1
else {
val mid2 = (low + high) / 2
val d = ds(mid2)
if (d == key) mid2
else if (d > key) go(low, mid2, mid2 - 1)
else go(mid2 + 1, mid2, high)
}
}
go(0, 0, ds.length - 1)
}
// return the index of the searched data if found else
// a negative number if data doesn't exist
// takes an anonymous function/lambda as a parameter for comparison
//this is a generic method which takes any data type
def binSearch[X]( data: Array[X], key:X , gt: (X,X) => Boolean):Int={
@annotation.tailrec
def go(low:Int, mid:Int, high:Int ):Int={
if( low > high) -mid-1
else{
var mid2 = (low + high) /2
var d= data(mid2)
val greater = gt(d,key)
if ( !greater && !gt(key,d)) mid2
else if (greater) go(low,mid2,mid2-1)
else go(mid2+1,mid2,high )
}
}
go(0,0,data.length-1)
}
}
Let me know if you have any comments on the same.