Scala : Check Sort using anonymous function

object issorted extends App{ def issorted[A](as:Array[A],ordered:(A,A)=>Boolean):Boolean= { def loop(n:Int): Boolean = { if (n<=0) true // if recursion goes to first element then array is sorted else ordered(as(n),as(n-1)) && loop(n-1) // else check the and && conditions for oredred function and loop(n-1) .. bith true then true else false } loop(as.length-1) } print(issorted(Array(1,2,3,4,5),(x:Int,y:Int)=>x>y)) // declare… Read More Scala : Check Sort using anonymous function

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… Read More Scala: Anonymous function or Generic function , search element in array

Scala: searching array element using recursion

HI, Below is an example of searching of elements in an array using scala monomorphic function as well recursion object arraysearch extends App { def searcharray(ss:Array[String],key:String):Int= { def loop(n:Int):Int= { if(ss(n)==key) n // else if (n<=0) -1 else loop(n-1) } loop(ss.length-1)  // pass the arary(length -1) in loop function  recursion } println(searcharray(Array(“hii”,”how”,”are”,”you”),”are”)) }   Thanks please… Read More Scala: searching array element using recursion