The WGJD - Polyglot &Functional Programming Ben Evans and Martijn Verburg (@kittylyst @karianna) http://ww...
This is still not an Oracle legal slide 2
Polyglot and Functional - Why is it important?• The WGJD wants to code rapidly• The WGJD wants to code concisely• The WGJD...
Why Java is not a Golden Hammer• Recompilation is laborious• Static typing can be inflexible – Can lead to long refactor...
Java is a conservative language• New language features take a while to arrive in Java• This is deliberate• Languages which...
Language Zoology• Interpreted vs. Compiled – Interpreted source code is executed as-is – Compiled code is converted to...
Languages on the JVM• There are now over 200, falling into several broad groupings• Language re-implementations – JRuby,...
Polyglot Programming Pyramid• Courtesy of Ola Bini 8
Ola - JVM languages expert? Meh, do not talk to him 9
Polyglot Layers• Domain-specific – Tightly coupled to a specific part of the application domain. – e.g. Apache Camel D...
Questions to ask yourself before going Poly• Is the project area low risk?• How easily does the language interoperate with...
Experts: “use < 5 bullet points”We have a fr&%!en movie screen! 12
Case Study: Rapid Web Development• Matt Raible - 20 criteria for a web framework • Developer Productivity • ...
JVM language web framework shoot out• Grails Wins!• http://bit.ly/jvm-frameworks-matrix ...
Functional Programming• Show a Java-centric approach to Functional Programming (FP)• Focus on what’s missing & how we woul...
Example - Reconciliation Service• We have 2 sources of data• Source 1 - The upstream system “sourceData” – e.g Call a we...
Reconciliation - Java Take 1 17
Output of Reconciliation - Java Take 1• 7172329 OK• 7173341 OK• 1R6GT OK• 1R6GV OK• 1R6GW OK• main_ref: 1R6H2 ...
Analysing the data• What’s gone wrong?• The answer is that upstream system is case-insensitive – Whereas the downstream ...
Reconciliation - Fixing Case SensitivityWith this: 20
Introducing Functional Concepts• This are two ideas related to FP in the previous example• First idea is: – Operating on...
What if we could...• Suppose we could tweak the functionality of a method by adding in some new code of our own.• We’d ne...
Reconciliation - With Match FunctionImaginary FP-in-Java syntax: 23
Library Support• There’s no actual 2-parameter contains() method.• But that’s what we would want, if we could start ag...
“What if” is for namby pamby dreamers 25
FP Map Pattern• FP fans would call this a map() expression – extractPrimaryKeys() takes a List and returns a new List ...
FP - More on the Map Pattern• Note that the type contained in the returned List may be different from the incoming List. ...
FP - The Filter Pattern• The use of map() is an absolutely classic FP idiom.• It’s usually paired with this well-known pat...
Functions-as-values• Now we can see the construct that we need for function-as-value. – We need some way to represent th...
There you go Scala folks!We finally gave you some props It’s the last time 30
What We Didn’t Have Time To Talk About• Why functional is good for modern concurrency – That’s in the next hour!• The my...
What? You still here?Go take a break will you! 32
of 32

Polyglot and functional (Devoxx Nov/2011)

Part 2/3 of our Devoxx University session. A discussion of functional programming and polyglot programming on the JVM for the Java developer!
Published on: Mar 4, 2016
Published in: Technology      
Source: www.slideshare.net


Transcripts - Polyglot and functional (Devoxx Nov/2011)

  • 1. The WGJD - Polyglot &Functional Programming Ben Evans and Martijn Verburg (@kittylyst @karianna) http://www.teamsparq.net Slide Design by http://www.kerrykenneally.com
  • 2. This is still not an Oracle legal slide 2
  • 3. Polyglot and Functional - Why is it important?• The WGJD wants to code rapidly• The WGJD wants to code concisely• The WGJD wants to take advantage of: – The JVM – Non Object-Orientated approaches – Dynamic language approaches• Polyglot and functional languages exist on the JVM – The JVM is no longer tied to Java the language 3
  • 4. Why Java is not a Golden Hammer• Recompilation is laborious• Static typing can be inflexible – Can lead to long refactoring times• Deployment is a heavyweight process – JRebel can mitigate this for web apps• Javas syntax is not a natural fit for producing DSLs 4
  • 5. Java is a conservative language• New language features take a while to arrive in Java• This is deliberate• Languages which try to move quickly can “repent at leisure” – There are some features in Scala which everyone now regrets• Non-Java languages are a test-bed for features – A good place to learn and experiment 5
  • 6. Language Zoology• Interpreted vs. Compiled – Interpreted source code is executed as-is – Compiled code is converted to machine code before execution• Dynamic vs. static – Dynamic variables can have different types at different times – Dynamic types are only resolved at execution time – Static types can be resolved much earlier• Imperative vs. functional – OO and Procedural are both imperative styles – Imperative: Code operates on Data – Functional: Code & Data are one and the same 6
  • 7. Languages on the JVM• There are now over 200, falling into several broad groupings• Language re-implementations – JRuby, Jython• Attempted Java killers – Fantom, Ceylon, Xtend, Scala• Dynamic languages – Groovy, Rhino, Clojure• Academic – Ioke, Seph 7
  • 8. Polyglot Programming Pyramid• Courtesy of Ola Bini 8
  • 9. Ola - JVM languages expert? Meh, do not talk to him 9
  • 10. Polyglot Layers• Domain-specific – Tightly coupled to a specific part of the application domain. – e.g. Apache Camel DSL, Drools, Web templating• Dynamic – Rapid, productive, flexible development of functionality – e.g. Groovy, Jython, Clojure• Stable – Core functionality, stable, well-tested, performant. – e.g. Java, Scala 10
  • 11. Questions to ask yourself before going Poly• Is the project area low risk?• How easily does the language interoperate with Java?• What tooling support is there for the language? – e.g. IDE support• How easy is it to build, test & deploy in this language?• How steep is the learning curve for this language?• How easy is it to hire developers for this language? 11
  • 12. Experts: “use < 5 bullet points”We have a fr&%!en movie screen! 12
  • 13. Case Study: Rapid Web Development• Matt Raible - 20 criteria for a web framework • Developer Productivity • Developer Perception • Learning Curve • Project Health • Developer Availability • Job Trends • Templating • Components • Ajax • Plugins or Add-Ons • Scalability • Testing Support • i18n and l10n • Validation • Multi-language Support • Quality of Documentation/Tutorials • Books Published • REST Support (client and server) • Mobile / iPhone Support • Degree of Risk 13
  • 14. JVM language web framework shoot out• Grails Wins!• http://bit.ly/jvm-frameworks-matrix 14
  • 15. Functional Programming• Show a Java-centric approach to Functional Programming (FP)• Focus on what’s missing & how we would want it to work in Java• Example-driven• Make the connection to other languages (especially Scala) 15
  • 16. Example - Reconciliation Service• We have 2 sources of data• Source 1 - The upstream system “sourceData” – e.g Call a web service for transaction records• Source 2 - The downstream database• We need a reconciliation system – Check that data is actually reaching the DB. 16
  • 17. Reconciliation - Java Take 1 17
  • 18. Output of Reconciliation - Java Take 1• 7172329 OK• 7173341 OK• 1R6GT OK• 1R6GV OK• 1R6GW OK• main_ref: 1R6H2 not present in DB• main_ref: 1R6H3 not present in DB• 1R6H6 OK• 623SRC OK 18
  • 19. Analysing the data• What’s gone wrong?• The answer is that upstream system is case-insensitive – Whereas the downstream one is case-sensitive• 1R6H2 is present in the DB – It’s just called 1r6h2 instead• Let’s go back to the code slide – Can anyone see a problem with the code now?• There’s no containsCaseInsensitive() method – This is an irritant 19
  • 20. Reconciliation - Fixing Case SensitivityWith this: 20
  • 21. Introducing Functional Concepts• This are two ideas related to FP in the previous example• First idea is: – Operating on collections / data structures as a whole – Rather than explicitly iterating over their contents• Second idea is: – A lack of capability to add additional logic to existing methods• Both ideas help with writing concise, safer OO code 21
  • 22. What if we could...• Suppose we could tweak the functionality of a method by adding in some new code of our own.• We’d need to pass the code into the method as a parameter.• We need some way to treat a code blob as though it was a value – Want to be able to put it into a variable.• FP requires the ability to represent bits of logic as though they were values. 22
  • 23. Reconciliation - With Match FunctionImaginary FP-in-Java syntax: 23
  • 24. Library Support• There’s no actual 2-parameter contains() method.• But that’s what we would want, if we could start again• Other languages have this functionality• Called: Lambda expressions, closures, function literals• Need to have library support as well as the language primitive 24
  • 25. “What if” is for namby pamby dreamers 25
  • 26. FP Map Pattern• FP fans would call this a map() expression – extractPrimaryKeys() takes a List and returns a new List – Run an operation on each element in turn (and return the new list we built up). 26
  • 27. FP - More on the Map Pattern• Note that the type contained in the returned List may be different from the incoming List. – Incoming type is DBInfo, outgoing is String – The original List hasn’t been affected in any way.• This is where the name “functional programming” comes from – The functions behave like mathematical functions• A function like f(x) = x * x doesn’t alter the value 2 when it’s passed in. Instead, it returns a different value, 4. 27
  • 28. FP - The Filter Pattern• The use of map() is an absolutely classic FP idiom.• It’s usually paired with this well-known pattern, the filter() form. 28
  • 29. Functions-as-values• Now we can see the construct that we need for function-as-value. – We need some way to represent that “predicate function” for filter()• Here’s one way we could write it (in almost-Scala): (msg) -> { !msg.get("status").equalsIgnoreCase("CANCELLED") };• This is a function which takes one argument – msg is a Map<String, String> – The function returns boolean• Actually, this is also how Java 8 is going to write it. – In fact this is a very Java-ish way of writing Scala • but that’s deliberate 29
  • 30. There you go Scala folks!We finally gave you some props It’s the last time 30
  • 31. What We Didn’t Have Time To Talk About• Why functional is good for modern concurrency – That’s in the next hour!• The myriad academic definitions of closures• Groovy in depth• Clojure• What Java 8 JVM changes mean for other languages – Jigsaw could be a major opportunity or a headache 31
  • 32. What? You still here?Go take a break will you! 32

Related Documents