Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# 2.2 higher order-functions

689

Published on

Published in: Technology, Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
689
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
2
0
Likes
0
Embeds 0
No embeds

No notes for slide
• Denne siden skal vel bort?
• Bruke logging eksempelet
• Logging?
• ### Transcript

• 1. Higher order functions
• 2. Higher order functionsFunctions which take functions as parameters and/or return functions
• 3. Higher order functionsShort summary of first class functions:val even: (Int => Boolean) = (i: Int) => i % 2 == 0Same type definition:def test(numbers: List[Int], f: Int => Boolean) = ...Call:test(List(1, 2, 3), (i: Int) => i % 2 == 0)
• 4. Higher order functionsdef test(numbers: List[Int], f: Int => Boolean) = numbers.map(tall => f(tall))// List[Boolean]
• 5. Higher order functionsFunctions with several parameters must list them in parenthesis:def test(l: List[String], f: (Int, String) => Boolean)
• 6. call-by-value vs. call-by-nameby-value: expressions are evaluated before being passed to the functionby-name: expressions evaluated inside function nice when computationally expensive possible to create nice APIs
• 7. call-by-value vs. call-by-nameExample: Loggingdef thisTakesTime = { println(“Slow computation”) “result”}logger.debug(thisTakesTime())
• 8. call-by-valuedef debug(s: String) { println(“debug”) if (logLevel <= DEBUG) println(s)}// Slow computation// debug// result
• 9. call-by-namedef debug(s: => String) { println(“debug”) if (logLevel <= DEBUG) println(s)}// debug// Slow computation// result