• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Lambda Expressions: Myths and Mistakes - Richard Warburton (jClarity)

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



Presented at JAX London 2013 ...

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?



Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

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

    • Lambdas: Myths and Mistakes by Richard Warburton
    • the critical design tool for software development is a mind well educated in design principles. It is not ... technology. Craig Larman
    • 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.
    • Talk Structure Why am I talking about this? Intro to Lambda Expressions Beyond the Myths Functional Thinking
    • Why am I talking about this?
    • Lambda Expressions are coming in Java 8!
    • lots of discussion/debate
    • How can we help? Adopt-a-JSR
    • Adopt-a-JSR? More community driven standards Hackdays Serve on Expert Groups
    • Some discussion unimportant Concrete Examples focus discussion
    • Intro to Lambda Expressions
    • Overview of Lambdas Goal: Better Libraries Example: Collections with Data Parallelism Approach: Allow Code as Data
    • Action Listener bto.dAtoLsee(e AtoLsee( { utnadcinitnrnw cinitnr) pbi vi atoPromdAtoEeteet { ulc od cinefre(cinvn vn) Sse.u.rnl(bto cikd) ytmotpitn"utn lce"; } }; )
    • Code as Data bto.dAtoLsee( utnadcinitnr ? ) ;
    • Need a parameter bto.dAtoLsee(vn utnadcinitnreet ) ;
    • Lambda Example bto.dAtoLsee(vn utnadcinitnreet > Sse.u.rnl(bto cikd) ytmotpitn"utn lce" ) ;
    • No parameters Rnal hlool = unbe elWrd ( - Sse.u.rnl(HloWrd) ) > ytmotpitn"el ol";
    • Variable Capture Srn nm =gtsrae) tig ae eUeNm(; bto.dAtoLsee(vn utnadcinitnreet > Sse.u.rnl(h "+nm) ytmotpitn"i ae ) ;
    • Functional Interfaces Everything in Java has a type Problem: Need types to represent Functions Solution: Use interfaces
    • Functional Interfaces pbi itraeAtoLsee etnsEetitnr{ ulc nefc cinitnr xed vnLsee pbi vi atoPromdAtoEeteet; ulc od cinefre(cinvn vn) }
    • Streams Support automated data parallelism Build computation pipelines Iterator with inversion of control
    • External Iteration itcut=0 n on ; fr(ritats :atss { o Ats rit rit) i (ritiFo(Lno"){ f ats.srm"odn) cut+ on+; } }
    • Internal Iteration atsssra( rit.tem) .itrats - ats.srm"odn) fle(rit > ritiFo(Lno") .on(; cut)
    • map
    • 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)
    • reduce
    • reduce itsm=Sra.f1 2 3 4 n u temo(, , , ) .eue0 (c,x - ac+x; rdc(, ac ) > c ) asrEul(0 sm; setqas1, u)
    • filter
    • 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;
    • Putting it all together for a given an album, find the nationality of every band playing on that album
    • 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
    • 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()
    • 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
    • Beyond the Myths
    • Claim: Syntax is the most important thing about Lambda Expressions
    • Yeah, I liked the # syntax proposal better, too. One less character to type! :)
    • Have you considered 'default null'? It will save a keyword
    • How about a single punctuation mark, currently unused, as syntax sugar for "()>".
    • (_, _) -> _ + _ This is starting to look like risque ASCII art :)
    • Its a Myth!
    • Claim: Syntax is irrelevant
    • / 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;;
    • Difference between expectations Many language features stolen! adapted Missing Features Stronger Type System Tuples List construction syntax
    • Framing Effect Different reactions depending on whether something is presented as a loss or a gain.
    • 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()
    • 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()
    • Very little Testing Maybe ... a reflection on popularity of TDD spikes are good for learning unfamiliarity with testing lambdas
    • How do I test this? ls.tem) itsra( .a( - 10/Mt.el1+Mt.o()+Mt.tn(,x) mpx > . ahci( ahpwx ahaa2y )) .olc(oit); clettLs()
    • Approach 1: Test surrounding method Don't test the lambda Test the method its surrounded by Works well for simple lambdas
    • 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()
    • 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) } }
    • 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()
    • Compiler Error Messages
    • Comparators Cmaao<tig cmaao =cmaigSrn:lnt) oprtrSrn> oprtr oprn(tig:egh; Cmaao<tig cmaao =cmaigsr- srlnt) oprtrSrn> oprtr oprn(t > t.egh;
    • 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
    • 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
    • Summary Syntax important, but not in the way people think New approaches for debugging and testing Take care of overloads and compiler error messages
    • Functional Thinking
    • Functional Thinking? Thinking in terms of the input to output relationship and not a sequence of steps
    • First code that people write Ls<nee>nmes=Ary.sit1 2 3; itItgr ubr rasaLs(, , ) nmesfrahx- { ubr.oEc( > Sse.u.rnl() ytmotpitnx; }; )
    • Non-idiomatic Proposals Eg: capture non-final local variables
    • Example Problem Count the number of instances of each word in a document.
    • Ideal Solution rae.ie( edrlns) .lta( - ssltstem"") faMps > .piASra( ) .olc(ruigys- s cletgopnB( > , cutn(); onig))
    • 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 , ] >
    • 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))
    • 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;
    • This takes thought
    • Summary Idioms are vital Not Java specific at all Requires Practise
    • Conclusions Gone through a bunch of examples of specific issues ‘Functional Thinking’: Not necessary to start learning. Try before you buy
    • Questions? @RichardWarburto http://jclarity.com http://insightfullogic.com https://github.com/RichardWarburton/