Scala: high order function

High order function –  are those functions which takes a function as arguments \


example as follows


object fibonaci extends App{
def fibonaci(n:Int)=
def fibgo(n:Int,prev:Int=0,next:Int=1):Int= n match
case 0 => prev
case 1 => next
case _ => fibgo(n-1,next,(next+prev))
def factorial(n:Int):Int= // define factorial function
def go(n:Int,acc:Int):Int= // define a inner function which is common in functional programming
if(n<=0) acc else go(n-1,acc*n) // recursion .. of go function
go(n,1) //calling function go
def formata(s:String,n:Int,f:Int=>Int)=
val msg = “%s %d is %d”
val x = formata(“the factorial of “,5,factorial)  // high order function example — calling a factorial function as an argument



Thanks, Please follow and comment






Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s