Parleys.com Search Index |
|||
|
|||
|
Title: Funky Java, Objective Scala
Summary: Polyglot programming is here, and can improve your style. Suggestions on how to inject a little more functional style into your Java, and temper Scala with a mix of Object Oriented and Functional style. Description: Recently, while the Java platform has been going from strength to strength, improving both performance and support for alternative languages, the Java language has started to appear less exciting than many of the alternatives that now run on the JVM. There is a lot to be said for the modern features of many of these new alternatives, but does that mean the fat lady has sung for Java? Not so, while we might still not have closures or properties, there are many new and interesting libraries and techniques that can breathe new life and style into your Java source. A stronger emphasis on immutable value objects, use of third party libraries like the Google collections library, DSL like fluent interfaces and more radical ideas like Project Lombok can all help you improve your style, reduce the boilerplate, reduce errors and be more productive with Java. It will bring fun back to your Java development. The first part of this session will look at libraries and techniques that you can use wi Speaker(s): Dick Wall Keyword(s): Java scala devoxx 2009 conference Slide Content: 1) Funky Java Objec/ve Scala Dick Wall Navigenics & The Java Posse 2) Funky Java • • • 20+ Years of Object Oriented Focus When all you have is a hammer... What if we introduce Functional style? 2 3) Objective Scala • • • Functional and Object Oriented Great functional support Also great OO support (properties, etc.) 3 4) Agenda Object Orientation Where Functional Fits Genetics 101 Immutability Builders Know Your Collections 4 5) Agenda, Ctd. Predicates/Functions/Constraints Multiple Return Arguments More Ambitious Scala Conclusion 5 6) Speaker’s qualifications ~20 Years Software Engineering ~15 Years Java Development Recently re-wrote the Navigenics Genetic Risk Calculator + Supporting Structures Java Posse Co-Host Bay Area Scala Enthusiasts founder Java Champion 6 7) Object Orientation Predominant for years Suits some problem domains perfectly – GUI – Tangibles – eCommerce But other choices may fit some spaces – Science, Mathematics, etc. 7 8) Where Functional Fits Parallel (multi core) ← stock answer Stuff that's not e-commerce Working with scientists/ mathematicians Where you have too many ifs and fors Alongside Object Orientation? – In my experience, yes! 8 9) Genetics 101 DNA organized into chromosomes Chromosomes have Genes Human beings have about 25,000 genes Genes contain SNPs 9 10) Genetics 101 Single Nucleotide Polymorphism – SNP In English: A DNA 'Phrase' where one nucleotide varies between 2 possible states in different people. e.g. ACTCCTAG → ACTGCTAG Change can be statistically significant for increased risk of a health condition. 10 11) Genetics 101 You get 2 copies, one from each parent For SNP, 2 allele types, risk, non-risk x2 copies = 4 combinations: RR,RN,NR,NN RN/NR Collapsed together → 3 genotypes RR = Homozygous Risk (most risk) RN = Heterozygous Risk (some risk) NN = Homozygous Non-Risk (least risk) 11 12) Genetic Composite Index Scientists assign Odds Ratio to RR,RN,NN Odds Ratio of NN normalized to 1.0 Product of all Odds Ratios gives GCI score GCI(person)/GCI(average) * Average Estimated Lifetime Risk = Person's Estimated Lifetime Risk, etc. More: place within reference populations, deal with multi-gene/multi-SNP 12 13) Great Case for Functional However, this Simple example great for demonstrating Functional approach For SNP, AG → RN → 1.8171 For GCI, 1.0 * 1.8171 * 2.3134 = 4.2037 For ELTR, 4.2037 / 2.5123 * 0.35 = 0.4189 → ~42% life time risk Map/Reduce operation → Functional 13 14) SNPs and Calls SNP results for an individual are called Calls Calls Immutable (except genotyping errors) Indexed by an ID (RSID: Reference SNP ID) Calls may be missing/no calls (normalize to 1.0 odds ratio to eliminate + normalize in reference population too!) 14 15) Calls Call states → Missing → No Call → Low Confidence → Fine Should only ever move from weaker to stronger call state when new call All other values immutable Make whole thing immutable, copy to new call with new state and call value 15 16) Immutable with Copy 16 17) Immutable with Copy BUT! Copy of immutable means return type must not be ignored Common bug in Java applications JSR 305/Findbugs @CheckReturnValue 17 18) Builder Pattern Helps provide missing features – default/named parameters Can be increasingly sophisticated – use interfaces to create a 'guiding' DSL You decide if the payoff is worth it... 18 19) Builder Pattern Example (1) 19 20) Builder Pattern Example (2) 20 21) Imperative Calculator 21 22) What's wrong with that? Nothing! More complex algorithm → much more complicated imperative code Symptoms: – Massive, nested loops – Multiple ifs – Monster methods 22 23) What's wrong with that? 23 24) Know Your Collections Java Collections API very rich Java Generics and Collections – Maurice Naftalin/Philip Wadler Nicely augmented by Google Collections Library (has the same 'feel') Google Collections also brings Predicates, Functions, Constraints 24 25) Using Google Collections 25 26) But that's longer! Arguably less complicated As algorithm complexity grows, bigger win Complexity split up, ordered Easier to test Functions, Predicates can live with object Can compose functions, etc. 26 27) Call → Genotype → OR Call → Genotype Function Genotype → OR Function Compose 2 functions into 1, Call → OR 27 28) Call → Genotype → OR Call → Genotype Function Genotype → OR Function Compose 2 functions into 1, Call → OR 27 29) Pairing Calls/SNP Details Condition has list of desired SNP details Sift through calls, which ones are satisfied? Real version – more complex – finding correct call is heavy operation Don't want to repeat same operation Return pair of SNP Details & Call But! Java doesn't allow multiple returns 28 30) SNP/Call Pair Return Options Create a 'structure' object Add equals, hashcode... Test! Rinse and repeat Other languages have tuples Typesafe tuples in Java? Pair<>, Triple<> 29 31) Pair Implementation Example 30 32) Pair? Triple? Really? Number of implementations indicates utility Feed into functions (→ multiple args) Troublesome for OO purists It's a tool, use it or don't But: if you use value objects, be responsible Equals, hashcode, maintenance, testing You don't roll your own collections (usually) 31 33) Pair Implementation Example 30 34) Pair? Triple? Really? Number of implementations indicates utility Feed into functions (→ multiple args) Troublesome for OO purists It's a tool, use it or don't But: if you use value objects, be responsible Equals, hashcode, maintenance, testing You don't roll your own collections (usually) 31 35) Other Return Options Project Lombok? @Data – avoid boilerplate equals/ hashcode Great for immutable value structures More expressive than generic Pair, but not much more work Not available for all tools/IDEs yet 32 36) Reduce List of Odds Ratios Reduce, want to fold left the values, starting with 1.0, multiplying each in turn No Reduce in Google Collections But, JSR 166y extras has a nice one Could use JSR 166y for whole map/ reduce 34 37) JSR166y ParallelArray example 35 38) JSR166y ParallelArray Overkill here? Sure, until scale needed! Pool adjusts to available cores Work stealing, threading best practices More than just readability 36 39) Scala! Mixes OO, Functional Does both better than Java Case Classes Traits More... tuples... pattern matching... And of course: closures! 37 40) Calculator in Scala Yes, that's everything! But! Double vs double @specialized in Scala 2.8 Imagine ParallelArray with this syntax 38 41) But you said Objective! Case classes – value without boilerplate Immutable properties, equals, hashcode 39 42) Builders? 2.8 has default/named params SnpDetail(testRsId=”rs1234”, chromosome=”chr1”, …) val new = snp.copy(call=”TG”) Don't need builders at all 40 43) More object goodness Properties 41 44) More object goodness scala.reflect.BeanInfo,BeanProperty Operator overloading!? 42 45) Plenty more objecty-ness Traits Object composition (with) Implicits Generics (including 'reification') Structural typing Type inference 43 46) Summary Complex vs Complicated Unfamiliar vs Complicated Complexity is value Simplification is value Functional possible in Java OO/functional to ease into Scala 44 47) DEMO • Scala (if time) 45 |
|||
(c) Parleys.com NV, 2006-2010 - Technical Info
|
|||