Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Practical Scala
HOF
Who am I?
@raulraja
47deg.com
raulraja.com
Scala, Java, Objective-C
1. HOF Higher Order Functions
A function that takes a function as argument
or returns another function
e.g. map & filter
map transforms data
map sadness to happiness
:( :( :(

=> :) :) :)
map sadness to happiness - JAVA
public List<String> mapSadnessToHappiness (List<String> sadness) {
List<String> happiness ...
map sadness to happiness - Scala
List(":(", ":(", ":(").map(face => face.replace('(', ')'))
map sadness to happiness - Scala
List(":(", ":(", ":(").map(_.replace('(', ')'))
filter remove the unwanted
filter sadness keeping happiness
:) :) :(

=> :) :)
filter sadness keeping happiness
List(":)", ":)", ":(").filter(_.contains(":)"))
Idiomatic Transformations
Count char occurrences on a String - JAVA
public static int countCharOccurrences(String haystack...
Idiomatic Transformations
Count char occurrences on a String
"betabeers" groupBy identity mapValues (_.size)
Key differentiator
Higher Order Functions help with most Collection problems
that you usually solve by looping over and ma...
Other Powerful HOF’s examples
Sum
(1 to 1000).reduceLeft( _ + _ )
(1 to 1000).sum

Partition filter
val (passed, failed) = ...
Become a Scala Master
For comprehensions
Case Classes
Futures
Options
Traits
Either
Pattern Matching
Monads
Actors
DSL’s
....
Upcoming SlideShare
Loading in …5
×

Scala Higher Order Functions

1,009 views

Published on

A basic intro to HOF's for Scala newcomers, Part of the Betabeers series

Published in: Technology, Spiritual
  • Be the first to comment

  • Be the first to like this

Scala Higher Order Functions

  1. 1. Practical Scala HOF
  2. 2. Who am I? @raulraja 47deg.com raulraja.com Scala, Java, Objective-C
  3. 3. 1. HOF Higher Order Functions A function that takes a function as argument or returns another function e.g. map & filter
  4. 4. map transforms data
  5. 5. map sadness to happiness :( :( :( => :) :) :)
  6. 6. map sadness to happiness - JAVA public List<String> mapSadnessToHappiness (List<String> sadness) { List<String> happiness = new ArrayList<String>(sadness.size()); for (int i = 0; i < sadness.size(); i++) { String sadFace = sadness.get(i); String happyFace = sadFace.replace('(', ')'); happiness.add(happyFace); } return happiness; } List<String> sadness = Arrays.asList(":(", ":(", ":("); List<String> happiness = mapSadnessToHappiness(sadness);
  7. 7. map sadness to happiness - Scala List(":(", ":(", ":(").map(face => face.replace('(', ')'))
  8. 8. map sadness to happiness - Scala List(":(", ":(", ":(").map(_.replace('(', ')'))
  9. 9. filter remove the unwanted
  10. 10. filter sadness keeping happiness :) :) :( => :) :)
  11. 11. filter sadness keeping happiness List(":)", ":)", ":(").filter(_.contains(":)"))
  12. 12. Idiomatic Transformations Count char occurrences on a String - JAVA public static int countCharOccurrences(String haystack, char needle) { int count = 0; for (int i = 0; i < haystack.length(); i++) { if (haystack.charAt(i) == needle) { count++; } } return count; } public static Map<Character, Integer> toCharOccurrenceMap(String haystack) { Map<Character, Integer> map = new HashMap<Character, Integer>(); if (haystack != null) { for (int i = 0; i < haystack.length(); i++) { char character = haystack.charAt(i); int count = countCharOccurrences(haystack, character); map.put(character, count); } } return map; } toCharOccurrenceMap("betabeers");
  13. 13. Idiomatic Transformations Count char occurrences on a String "betabeers" groupBy identity mapValues (_.size)
  14. 14. Key differentiator Higher Order Functions help with most Collection problems that you usually solve by looping over and manually creating intermediate containers. Transformed results stay immutable and thread safe. No need to reinvent the wheel. Code becomes readable and idiomatic "betabeers" groupBy identity mapValues (_.size)
  15. 15. Other Powerful HOF’s examples Sum (1 to 1000).reduceLeft( _ + _ ) (1 to 1000).sum Partition filter val (passed, failed) = List(49, 58, 88, 90) partition ( _ > 60 ) min List(14, 35, -7, 46, 98).min max List(14, 35, -7, 46, 98).max Imperative iteration (1 to 10) foreach (println) Parallel Collections (1 to 10).par foreach(_ => println(Thread.currentThread.getName))
  16. 16. Become a Scala Master For comprehensions Case Classes Futures Options Traits Either Pattern Matching Monads Actors DSL’s ...

×