二 零 十 五 年 三 月 二 十 四 日 \begin{programma 09.00-09.30 Inleiding Revolutie Functioneel Programmeren Java 8 Johan Blok en Bart Barnard 09.30-10.00 sorteer- en filteropdrachten 10.00-10.30 terugkoppeling en theorie 10.30-11.30 uitgebreidere opdrachten 11.30-12.00 gevolgen vakgebied en onderwijs \end{programma Het probleem Inleiding Intel Xeon processor 5600. (http://cdn.phys.org/newman/gfx/news/hires/westmere-ep-die.jpg)
Imperatief versus Functioneel Java richt zich meer op onderhoudbare en leesbare code dan efficiënt gebruik van clock- cycles Programmacode met locks: complex en foutgevoelig Veel gebruik van java.util.concurrent Performance gaat zelfs omlaag naarmate er meer cores op de processor komen Imperatief: reeks instructies die toestand veranderen sum = 0 for i=1 to count(x) sum = sum + x[i] average = sum / count(x) Functioneel: evaluatie van (wiskundige) functies avg(x): sum(x, count(x)) / count(x) sum(x, i): if (i>0) sum(x, i 1) + x[i] else 0 http://www.cse.wustl.edu/~jain/cse567-11/ftp/multcore/ Theorie: Lambda Calculus G.W. Leibniz (1646-1716) Binair getalstelsel 1671: mechanische rekenmachine Universele denkmachine λ- calculus: Formeel systeem ontwikkeld rond 1930 door Alonzo Church Hilbert s Entscheidungsproblem: algoritme dat bepaalt of een propositie bewijsbaar is Lambda Calculus als model voor berekenbaarheid 1980 s typed lambda calculus: constructive mathematics Voordelen lambda expressies Meer directe vertaling van de bedrijfslogica in programmacode; de nadruk op functionaliteit zorgt voor een betere flow die duidelijker maakt wat er moet gebeuren in plaats van hoe het gebeurt. Niet meer noodzakelijk om om de haverklap objecten te creëeren en heen en weer te sturen. Functies gebruiken als parameters Creëren van functies in andere functies Functies als return- type Veel boilerplate code kan worden verwijderd: duidelijkere er consistentere code Code als data: objecten kunnen worden gezien als data- containers
Java 8 overzicht Java 8 Lambda Expression Syntax Lambda Expression kan anonieme klasse vervangen Collections: filtering Predicates Map and Reduce Type inference button.addactionlistener(new new ActionListener() { public void actionperformed(actionevent e) { System.out.println("button clicked"); ); Anonymous inner class ActionListener heeft één methode: actionperformed() Code as data: we sturen een object mee aan de methode Te veel boilerplate code Is onduidelijk wat de programmeur hiermee bedoelt button.addactionlistener(event -> ); Java 8 Lambda Expression Syntax event is de parameter -> scheidt de parameters van de body van de lambda expressie System.out.println("button clicked ) Practicum 1: sorteren en filteren @Override public List<Student> imperative() { List<Student> list = data.getstudents(); list.sort(new Comparator<Student>() { public int compare(student student1, Student student2) { return student1.getname().compareto(student2.getname()); ); return list; @Override body van de expressie; in plaats van een object geven we het een stuk code mee Type inference javac bepaalt het type van event aan de hand van de signature van addactionlistener() public List<Student> functional() { List<Student> students = data.getstudents(); // TODO functional implementaton of sort by name return students;
External Iteration Application code Collection Code Streams hasnext() hasnext next() next Iteration Naar Warburton, 2014, p.18 External Iteration Internal Iteration int count = 0; Iterator<Artist> iterator = artists.iterator(); while(iterator.hasnext()) { Artist artist = iterator.next(); if (artist.isfrom("london")) { count++; Application code Build operation Result Collection Code Iteration Naar Warburton, 2014, p.19
Internal Iteration Streams in Java 8 long count = allartists.stream().filter(artist -> artist.isfrom("london")).count(); Source Collection / List.stream() Collection / List.parallelstream() Intermediate Operations Filter Map / Flatmap Sorted One terminal operation Foreach Collect Reduce Operations in cursief worden even kort behandeld. Zie voor totaalbeeld Warburton, 2014, pp. 17-41. Streams: Collect Streams: Filter collect(tolist()) is an eager operation that generates a list from the values in a Stream. List<String> foo = Stream.of("a", "b", "c").collect(collectors.tolist()); Any time you re looping over some data and checking each element, you might want to think about using the new filter method on Stream. List<String> startswithnr = Stream.of("a", "1abc", "abc1").filter(value -> isdigit(value.charat(0))).collect(tolist());
Streams: Map If you ve got a function that converts a value of one type into another, map lets you apply this function to a stream of values, producing another stream of the new values. List<String> foo = Stream.of("a", "b", "hello").map(string -> string.touppercase()).collect(tolist()); Streams: Reduce If you ve got a function that converts a value of one type into another, map lets you apply this function to a stream of values, producing another stream of the new values. int count = IntStream.range(1,4).reduce(0, (acc, element) -> acc + element); 1 2 3 4 element 1 3 6 10 acc initial result Predicates Predicates and type inference public public interface Predicate<T>{ boolean test(t t); static Predicate<Employee> isadultmale() { return p -> p.getage() > 21 && p.getgender().equalsignorecase("m");
Type inference old ( java < 1.8) method Practicum 2: uitgebreidere voorbeelden Map<String, Integer> foo = new HashMap<String, Integer>(); Map<String, Integer> bar = new HashMap<>(); ook in dit voorbeeld is javac slim genoeg om de generics van de nieuwe HashMap te achterhalen. diamond notation usehashmap(new HashMap<>());... private void usehashmap(map<string, String> values); M Discussie: Functioneel Programmeren in het Vakgebied van de ICT D H B C F G J K L Q T X N P R S V W Y Z A B-tree whose keys are the consonants of the alphabet. An internal node x containing n[x] keys has n[x]+1 children. Alle leaves are at the same depths in the tree. The purple nodes are examined in a search for the letter R. Krachtiger expressies Verkort ontwikkeltijd Minder code, minder bugs Unit tests Verwerking datastromen (big data) Performance? Hoe snel in bedrijfsleven opgenomen? In het nieuwe curriculum? Cormen et al.2005, p.435
Literatuur Cormen, T.H., C.E. Leiserson, R.L. Riverst, C. Stein, 2005, Introduction to Algorithms. Cambridge, Mass: MIT Press (second edition). Hofstadter, D., 1980, Gödel, Escher, Bach: an Eternal Golden Braid. Pinguin. Michaelson, G, 2011, An Introduction to Functional Programming Through Lambda Calculus, NY: Dover Publications. Nagel, E. and J.R. Newman, 2001, Gödel s Proof. NYU Press Warburton, R, 2014, Java 8 Lambdas. Functional Programming for the Masses. Sebastopol, CA: O Reilly.