Scala: Anonymous function or Generic function , search element in array

//polymorpjic function is a function which conains parameters as of genric types
//also some time known as generic function

object Polymorphicfunc extends App {

def searcharray[A](arr:Array[A],str:A):Int= //polymorphic function or generic function
{
def loop(n:Int):Int= // type [A] can be anything
{
if(arr(n)==str) n // search the str variable at index if find retun index
else if (n<0) -1
else loop(n-1) // recursion of loop function

}
loop(arr.length-1)
}

println(searcharray(Array(“hi”,”how”,”are”,”you”),”hi”))
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s