Thursday, April 28, 2016

Write Scala functions in multiple ways

There are different ways to define your functions in scala,
If you are coming from pure functional background , you would prefer haskel way of function definition like the one below - sqrt1
Single parameter 
def sqrt1: Int => Int = value => value * value

where as some guys may like using parenthesis to specify the function parameters.
But both have same functionality. - sqrt2

def sqrt2(value:Int) = value * value

Choose your style!!!!!

More than one parameter
def sumOfSqrt1:(Int,Int) => Int = (x,y) => x*x + y*y
If you define more than one parameters , you must specify the parenthesis separated with by colon (:), but it is not the way we saw in sqrt2

Syntax
<functionName> : (<paramater:Type>,<parameter:Type>) => <return_type>

Other way
def sumOfSqrt2(x:Int,y:Int):Int = x*x + y*y
Even you can give function parameters after the equal (=) sign also.

val sumSquare1 = (a: Int,b: Int) => a + b

Merging two sorted arrays - Big O (n+m) time complexity

 Problem :  Merge the two sorted arrays. Edge case :  Array can empty Arrays can be in different size let getMaxLength = ( input1 , input...