Lambda Expressions: Myths and Mistakes - Richard Warburton (jClarity)

1,982 views

Published on

Presented at JAX London 2013

tl;dr - How will the everyday developer cope with Java 8’s Language changes?

Java 8 will ship with a powerful new abstraction - Lambda Expressions (aka Closures) and a completely retooled set of Collections libraries. In addition interfaces have changed through the addition of default and static methods. The ongoing debate as to whether Java should include such language changes has resulted in many vocal opinions being espoused. Sadly few of these opinions have been backed up by practical experimentation and experience. - Are these opinions just myths?

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,982
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
20
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Lambda Expressions: Myths and Mistakes - Richard Warburton (jClarity)

  1. 1. Lambdas: Myths and Mistakes by Richard Warburton
  2. 2. the critical design tool for software development is a mind well educated in design principles. It is not ... technology. Craig Larman
  3. 3. Who am I? Currently work at jClarity Focus on Performance Tuning Adopt a JSR (Date and Time + Lambdas) Impending O'Reilly book on Lambda Expressions in Java 8.
  4. 4. Talk Structure Why am I talking about this? Intro to Lambda Expressions Beyond the Myths Functional Thinking
  5. 5. Why am I talking about this?
  6. 6. Lambda Expressions are coming in Java 8!
  7. 7. lots of discussion/debate
  8. 8. How can we help? Adopt-a-JSR
  9. 9. Adopt-a-JSR? More community driven standards Hackdays Serve on Expert Groups
  10. 10. Some discussion unimportant Concrete Examples focus discussion
  11. 11. Intro to Lambda Expressions
  12. 12. Overview of Lambdas Goal: Better Libraries Example: Collections with Data Parallelism Approach: Allow Code as Data
  13. 13. Action Listener bto.dAtoLsee(e AtoLsee( { utnadcinitnrnw cinitnr) pbi vi atoPromdAtoEeteet { ulc od cinefre(cinvn vn) Sse.u.rnl(bto cikd) ytmotpitn"utn lce"; } }; )
  14. 14. Code as Data bto.dAtoLsee( utnadcinitnr ? ) ;
  15. 15. Need a parameter bto.dAtoLsee(vn utnadcinitnreet ) ;
  16. 16. Lambda Example bto.dAtoLsee(vn utnadcinitnreet > Sse.u.rnl(bto cikd) ytmotpitn"utn lce" ) ;
  17. 17. No parameters Rnal hlool = unbe elWrd ( - Sse.u.rnl(HloWrd) ) > ytmotpitn"el ol";
  18. 18. Variable Capture Srn nm =gtsrae) tig ae eUeNm(; bto.dAtoLsee(vn utnadcinitnreet > Sse.u.rnl(h "+nm) ytmotpitn"i ae ) ;
  19. 19. Functional Interfaces Everything in Java has a type Problem: Need types to represent Functions Solution: Use interfaces
  20. 20. Functional Interfaces pbi itraeAtoLsee etnsEetitnr{ ulc nefc cinitnr xed vnLsee pbi vi atoPromdAtoEeteet; ulc od cinefre(cinvn vn) }
  21. 21. Streams Support automated data parallelism Build computation pipelines Iterator with inversion of control
  22. 22. External Iteration itcut=0 n on ; fr(ritats :atss { o Ats rit rit) i (ritiFo(Lno"){ f ats.srm"odn) cut+ on+; } }
  23. 23. Internal Iteration atsssra( rit.tem) .itrats - ats.srm"odn) fle(rit > ritiFo(Lno") .on(; cut)
  24. 24. map
  25. 25. map Ls<tig cletd=Sra.f"" "" "el" itSrn> olce temo(a, b, hlo) .a(tig- srn.opeCs() mpsrn > tigtUprae) .olc(oit); clettLs() asrEul(sit"" "" "EL",cletd; setqasaLs(A, B, HLO) olce)
  26. 26. reduce
  27. 27. reduce itsm=Sra.f1 2 3 4 n u temo(, , , ) .eue0 (c,x - ac+x; rdc(, ac ) > c ) asrEul(0 sm; setqas1, u)
  28. 28. filter
  29. 29. filter Ls<tig bgnigihubr = itSrn> einnWtNmes Sra.f"" "ac,"b1) temo(a, 1b" ac" .itrvle- iDgtvlecaA()) fle(au > sii(au.hrt0) .olc(oit); clettLs() asrEul(sit"ac) bgnigihubr) setqasaLs(1b", einnWtNmes;
  30. 30. Putting it all together for a given an album, find the nationality of every band playing on that album
  31. 31. Putting it all together (2) 1. transform an album into its artists 2. figure out which artists are bands 3. find the nationalities of each band
  32. 32. Putting it all together (3) Ls<tig oiis= itSrn> rgn abmgtuiin( lu.eMscas) .itrats - ats.eNm(.trsih"h") fle(rit > ritgtae)satWt(Te) .a(rit- ats.eNtoaiy) mpats > ritgtainlt() .olc(oit); clettLs()
  33. 33. Method References sr- srlnt t > t.egh Srn:lnt tig:egh x- fobrx > o.a() fo:a o:br sr- nwNm(t) t > e aesr Nm:nw ae:e
  34. 34. Beyond the Myths
  35. 35. Claim: Syntax is the most important thing about Lambda Expressions
  36. 36. Yeah, I liked the # syntax proposal better, too. One less character to type! :)
  37. 37. Have you considered 'default null'? It will save a keyword
  38. 38. How about a single punctuation mark, currently unused, as syntax sugar for "()>".
  39. 39. (_, _) -> _ + _ This is starting to look like risque ASCII art :)
  40. 40. Its a Myth!
  41. 41. Claim: Syntax is irrelevant
  42. 42. / Oiial ivld / rgnly nai Sra.f1 2 3 temo(, , ) .oEc( - Sse.u.rnl(); frahx > ytmotpitnx) / Rqie eta; / eurd xr Sra.f1 2 3 temo(, , ) .oEc( - Sse.u.rnl()) frahx > ytmotpitnx;;
  43. 43. Difference between expectations Many language features stolen! adapted Missing Features Stronger Type System Tuples List construction syntax
  44. 44. Framing Effect Different reactions depending on whether something is presented as a loss or a gain.
  45. 45. Recall our earlier example Ls<tig oiis= itSrn> rgn abmgtuiin( lu.eMscas) .itrats - ats.eNm(.trsih"h") fle(rit > ritgtae)satWt(Te) .a(rit- ats.eNtoaiy) mpats > ritgtainlt() .olc(oit); clettLs()
  46. 46. Eager vs Lazy (2) abmgtuiin( lu.eMscas) .itrats - ats.eNm(.trsih"h") fle(rit > ritgtae)satWt(Te) .a(rit- ats.eNtoaiy) mpats > ritgtainlt() / Wa' hpee? / hts apnd .olc(oit); clettLs()
  47. 47. Very little Testing Maybe ... a reflection on popularity of TDD spikes are good for learning unfamiliarity with testing lambdas
  48. 48. How do I test this? ls.tem) itsra( .a( - 10/Mt.el1+Mt.o()+Mt.tn(,x) mpx > . ahci( ahpwx ahaa2y )) .olc(oit); clettLs()
  49. 49. Approach 1: Test surrounding method Don't test the lambda Test the method its surrounded by Works well for simple lambdas
  50. 50. Approach 2: Extract Method dul cmlxucindul x { obe opeFnto(obe ) rtr 10/Mt.el1+Mt.o()+Mt.tn(,x) eun . ahci( ahpwx ahaa20 ); } ls.tem) itsra( .a(hs:opeFnto) mpti:cmlxucin .olc(oit); clettLs()
  51. 51. Mistake: debugging / Sras / tem ls.tem) itsra( .itrfleigucin fle(itrnFnto) .a(apnFnto) mpmpigucin .olc(oit); clettLs() / Y od frlo / e le o op Ls<a>br =nwAryit>) itBr as e raLs<(; fr(o eeet:ls){ o Fo lmn it i (itrnFnto(lmn){ f fleigucineeet Brrsl =mpigucineeet; a eut apnFnto(lmn) br.d(eut; asadrsl) } }
  52. 52. peek ls.tem) itsra( .itrfleigucin fle(itrnFnto) .eke- Sse.u.rnl(Flee vle "+e) pe( > ytmotpitn"itrd au: ); .a(apnFnto) mpmpigucin .a( - e; mpe > ) .olc(oit); clettLs()
  53. 53. Compiler Error Messages
  54. 54. Comparators Cmaao<tig cmaao =cmaigSrn:lnt) oprtrSrn> oprtr oprn(tig:egh; Cmaao<tig cmaao =cmaigsr- srlnt) oprtrSrn> oprtr oprn(t > t.egh;
  55. 55. Compiler Error jv:rfrnet cmaigi abgosbt aa eeec o oprn s miuu oh mto ehd <>oprn(aaui.ucinTItucin ?sprT) Tcmaigjv.tlfnto.onFnto< ue > i jv.tlCmaao admto n aaui.oprtr n ehd <,>oprn(aaui.ucinFnto<?sprT?etnsU) TUcmaigjv.tlfnto.ucin ue , xed > i jv.tlCmaao mth n aaui.oprtr ac
  56. 56. What happened? / Gnrcojc vrat / eei bet ain pbi sai <,UetnsCmaal<?sprU> ulc ttc T xed oprbe ue > Cmaao<> oprtrT cmaigFnto<?sprT ?etnsU kyxrco) oprn(ucin ue , xed > eEtatr / Seilsdpiiievrat / pcaie rmtv ain pbi sai <> ulc ttc T Cmaao<> oprtrT cmaigTItucin ?sprT kyxrco) oprn(onFnto< ue > eEtatr
  57. 57. Summary Syntax important, but not in the way people think New approaches for debugging and testing Take care of overloads and compiler error messages
  58. 58. Functional Thinking
  59. 59. Functional Thinking? Thinking in terms of the input to output relationship and not a sequence of steps
  60. 60. First code that people write Ls<nee>nmes=Ary.sit1 2 3; itItgr ubr rasaLs(, , ) nmesfrahx- { ubr.oEc( > Sse.u.rnl() ytmotpitnx; }; )
  61. 61. Non-idiomatic Proposals Eg: capture non-final local variables
  62. 62. Example Problem Count the number of instances of each word in a document.
  63. 63. Ideal Solution rae.ie( edrlns) .lta( - ssltstem"") faMps > .piASra( ) .olc(ruigys- s cletgopnB( > , cutn(); onig))
  64. 64. Ideal Solution (then) rae.ie( edrlns) .lta( - Sra.fsslt"") faMps > temo(.pi( )) .olc(ruigys- s cletgopnB( > , rdcn( - 1 Itgr:u)) euigs > , nee:sm); / Mpetisfr"a" / a nre o dd / ["a" "a" "a"]= [,1 1 = 3 / dd, dd, dd > 1 , ] >
  65. 65. Bad Solution (Part 1) MpSrn,Ls<tig>iiil a<tig itSrn> nta =b.ie( rlns) .lta( - Ary.temsslt"") faMps > rassra(.pi( )) .olc(ruigys- s) cletgopnB( > ); MpMpEtySrn,Itgr,Itgr fe1=iiil a<a.nr<tig nee> nee> rq nta .nrSt)sra( etye(.tem) .a(nr - nwAsrcMpSmlImtbenr<tig mpety > e btata.ipemualEtySrn, Itgr(nr.eKy) etygtau(.ie)) nee>etygte(, nr.eVle)sz() .olc(olcostMpety- etygtau(); cletCletr.oa(nr > nr.eVle))
  66. 66. Bad Solution (Part 2) Sple<aha<tig Itgr>sple =( - nw upirHsMpSrn, nee> upir ) > e HsMpSrn,Itgr(; aha<tig nee>) BCnue<aha<tig Itgr,MpEtySrn,Itgr>acm= iosmrHsMpSrn, nee> a.nr<tig nee> cu (aha<tig Itgr rsl,MpEtySrn,Itgr HsMpSrn, nee> eut a.nr<tig nee> ety - rsl.u(nr.eKy) etygtau() nr) > eutptetygte(, nr.eVle); BCnue<aha<tig Itgr,HsMpSrn,Itgr> iosmrHsMpSrn, nee> aha<tig nee> mre =HsMp:uAl egr aha:ptl; MpSrn,Itgr fe2=iiiletye(.tem) a<tig nee> rq nta.nrSt)sra( .a(nr - nwAsrcMpSmlImtbenr<tig mpety > e btata.ipemualEtySrn, Itgr(nr.eKy) etygtau(.ie)) nee>etygte(, nr.eVle)sz() .olc(upir acm mre) cletsple, cu, egr;
  67. 67. This takes thought
  68. 68. Summary Idioms are vital Not Java specific at all Requires Practise
  69. 69. Conclusions Gone through a bunch of examples of specific issues ‘Functional Thinking’: Not necessary to start learning. Try before you buy
  70. 70. Questions? @RichardWarburto http://jclarity.com http://insightfullogic.com https://github.com/RichardWarburton/

×