Upcoming SlideShare
×

# 1.4 first class-functions

202
-1

Published on

Published in: Technology, Design
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
202
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
1
0
Likes
0
Embeds 0
No embeds

No notes for slide
• Vanskelig å forklare foldleft med flere parameter lister?
• Nevne at dette er et fake eksempel
• ### 1.4 first class-functions

1. 1. First class functions
2. 2. First class functionsval even = Function[Int, Boolean] { def apply(i: Int) = i % 2 == 0}val even: (Int => Boolean) = (i: Int) => i % 2 == 0val even = (i: Int) => i % 2 == 0even.apply(42) // trueeven(13) // false
3. 3. First class functionsval numbers = List(1, 2, 3, 4, 5)numbers.filter(even) // List(2, 4)numbers.filter((i: Int) => i > 2) // List(3, 4, 5)numbers.filter(i => i > 2) // List(3, 4, 5)numbers.filter(_ > 2) // List(3, 4, 5)
4. 4. Collectionsnumbers.filter(i => i > 2) // List(3, 4, 5)numbers.find(i => i > 2) // Some(3)numbers.exists(i => i > 2) // truenumbers.forall(i => i > 2) // falsenumbers.map(i => i * 2) // List(2, 4, 6, 8, 10)numbers.foldLeft(0) { (a, b) => a + b } // 15
5. 5. Deferred execution - constructed example helloButton.addActionListener(e => println(“Hello World!”) )
6. 6. Closuresval people = List(Person(“Alf”), Person(“Fredrik”))val name = “Fredrik”val nameFilter = (p: Person) => p.name == namepeople.filter(nameFilter) // Person(“Fredrik”)
7. 7. Closuresval people = List(Person(“Alf”), Person(“Fredrik”))var name = “Fredrik”val nameFilter = (p: Person) => p.name == namepeople.filter(nameFilter) // Person(“Fredrik”)name = “Alf”people.filter(nameFilter) // Person(“Alf”)
8. 8. Tasks (30 min)Open the first-class-functions projectTests in packagescalaexamples.firstclassfunctionsAdd @Test to one and one methodFollow instructions in the codeMake the tests pass
1. #### A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.