[Scala] Functions, Anonymous functions

Functions within a function

tupla-function
def tuplaa(tuplattava: Int) = 2 * tuplattava

kahdesti-function
def kahdesti(toiminto: Int => Int, kohde: Int) = toiminto(toiminto(kohde))

kahdesti(tuplaa, 1000)
res3: Int = 4000</span>

kahdesti-function2
kahdesti(luku => luku + 1, 1000)  // 1002
kahdesti(n => n * 2, 1000)  // 4000

Anonymous parameter
kahdesti( _ + 1, 1000)  // 1002
kahdesti( (_: Int) + 1, 1000)
kahdesti( 2 * _, 1000)  // 4000

Example: Strings

def vertaaPituuksia(jono1: String, jono2: String) = jono1.length - jono2.length
def vertaaIntArvoja(jono1: String, jono2: String) = jono1.toInt - jono2.toInt
def vertaaMerkkeja(jono1: String, jono2: String) = jono.compareToIgnoreCase(jono2)

def onkoJarjestyksessa(eka: String, toka: String, kolmas: String, vertaa:(String, String) => Int) = vertaa(eka, toka) <= 0 && vertaa(eka, kolmas) <= 0 && 0 && vertaa(toka, kolmas) <= 0
 
onkoJarjestyksessa("Java", "Scala", "Haskell", vertaaPituuksia)
res4: Boolean = true 
onkoJarjestyksessa("Haskell", "Java", "Scala", vertaaPituuksia)
res5: Boolean = false 
onkoJarjestyksessa("Java", "Scala", "Haskell", vertaaMerkkeja)
res6: Boolean = false 
onkoJarjestyksessa("Haskell", "Java", "Scala", vertaaMerkkeja)
res7: Boolean = true 
onkoJarjestyksessa("200", "123", "1000", vertaaIntArvoja)
res8: Boolean = false 
onkoJarjestyksessa("200", "123", "1000", vertaaPituuksia)
res9: Boolean = true

Anonymous functions

(luku: Int) => luku + 1
res0: Int => Int = <function1>

(x: Double, y: Double) => (x / y).round.toInt
res1: (Double, Double) => Int = <function2>

Function as an object

val kokeilufunktio = (x: Double, y: Double) => (x / y).round.toInt
kokeilufunktio: (Double, Double) => Int = <function2>

kokeilufunktio(10, 4)
res0: Int = 3

val pyoristaOsamaara = kokeilufunktio
pyoristaOsamaara: (Double, Double) => Int = <function2>

Function apply() is a syntatic sugar. It can be said as "Apply pyoristaOsamaara 
function with argument 100 and 8.
pyoristaOsamaara.apply(100, 8)  // = pyoristaOsamaara(100, 0)
res1: Int = 13

Anonymous parameters

Array.tabulate(3, 5)( (rivi, sarake) => rivi + sarake )
Array.tabulate(3, 5)( _ + _)

Limits of Anonymous parameters

// CASE 1.
def sin2cos2(luku: Int) = pow(sin(luku), 2) + pow(cos(luku), 2)
luku => pow(sin(luku, 2) + pow(luku), 2)  // True

// However,
pow(sin(_), 2) + pow(cos(_), 2)  // False. Because each "_" points to two 
                                 // different anonymous parameters.
// Above code corresponds to the following:
(luku1, luku2) => pow(sin(luku1), 2) + pow(cos(luku2), 2)

// CASE 2.
kahdesti( x => x + 1, 100)  // Works
kahdesti( _ + 1, 1000)      // Works

// CASE 3.
kahdesti( x => tuplaa(x + 1), 1000)  // Works
kahdesti( tuplaa(_ + 1), 1000)       // Doesn't work. 
                                     // tupla(_ + 1) == tupla(x => x + 1)

kahdesti( x => tuplaa(x) + 1000)  // Works
kahdesti( tuplaa(_) + 1, 1000)    // Works

Screen Shot 2014-10-29 at 9.11.06 PM

Continue…

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

%d bloggers like this: