Upcoming SlideShare
×

# 2.2 higher order-functions

854 views

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
854
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
3
0
Likes
0
Embeds 0
No embeds

No notes for slide
• Denne siden skal vel bort?
• Bruke logging eksempelet
• Logging?
• ### 2.2 higher order-functions

1. 1. Higher order functions
2. 2. Higher order functionsFunctions which take functions as parameters and/or return functions
3. 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. 4. Higher order functionsdef test(numbers: List[Int], f: Int => Boolean) = numbers.map(tall => f(tall))// List[Boolean]
5. 5. Higher order functionsFunctions with several parameters must list them in parenthesis:def test(l: List[String], f: (Int, String) => Boolean)
6. 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. 7. call-by-value vs. call-by-nameExample: Loggingdef thisTakesTime = { println(“Slow computation”) “result”}logger.debug(thisTakesTime())
8. 8. call-by-valuedef debug(s: String) { println(“debug”) if (logLevel <= DEBUG) println(s)}// Slow computation// debug// result
9. 9. call-by-namedef debug(s: => String) { println(“debug”) if (logLevel <= DEBUG) println(s)}// debug// Slow computation// result