Polyglot and Functional Programming (OSCON 2012)
Upcoming SlideShare
Loading in...5
×
 

Polyglot and Functional Programming (OSCON 2012)

on

  • 1,642 views

OSCON 2012 tutorial session Part II

OSCON 2012 tutorial session Part II

Statistics

Views

Total Views
1,642
Views on SlideShare
1,615
Embed Views
27

Actions

Likes
0
Downloads
19
Comments
0

5 Embeds 27

http://tddv-conf-01 19
http://vestawiki 4
http://tddv-conf-01.ad.trustvesta.com 2
http://coderwall.com 1
http://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

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.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • TODO: If we have time, replace images of code with real text based code\n
  • It’s always worth repeating a bad joke\n
  • Explain what polyglot means\n
  • Do yourself a favour and get a JRebel license if you web hack in Java\n
  • Learning non-Java languages will make you a better programmer\n
  • * Java confuses matters as it’s.... both\n* Static types can be resolved at compile time for example\n
  • \n
  • \n
  • \n
  • \n
  • * Intro the web dev slides. “One major use case for poly is web dev.”\n* Hands up if you do Struts or JSF. Hands up if you like it.\n
  • Upcoming 20 bullet point slide\n
  • \n
  • Actually I can talk to the start-up story here - MV\n\n\n
  • \n
  • \n
  • * We have 2 sources of data, and we want to check whether the same elements appear in each.\n* We need two loops to do this - as there are 3 cases - id is OK, id only appears in source, and only in DB\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • * So, we’ve come at this a different way - we started out by saying that we need to pass in function values to customise functions.\n* This is what’s called “higher-order functions”. This is a different approach.\n
  • * Imaginary FP-in-Java syntax\n* Of course, this method doesn’t really exist. That’s why Eclipse has red-underlined it.\n
  • \n
  • \n
  • Let’s show a bit more context, and show how the reconcile() method gets called. DBInfo is the type which was actually returned from the DB lookup\nAlso, another slightly sneaky trick is in the call to reconcile() – we pass the returned List from extractPrimaryKeys() into the constructor for HashSet to convert it to a Set. This handily de-dups the List for us, making the contains() call more compact in the reconcile() method.\n
  • \n
  • \n* Notice the “defensive copy”. We return a new List. \n* We don’t mutate the existing List (the filter() form behaves like a mathematical function). \n* We build up a new List by testing each element against a function which returns boolean. \n* If the result of testing an element is true, we add it into the output List\n
  • Predicate is the jargon for the returns-boolean testing function that we apply to each element in turn.\n\n
  • \n
  • \n
  • \n

Polyglot and Functional Programming (OSCON 2012) Polyglot and Functional Programming (OSCON 2012) Presentation Transcript

  • The Well-Grounded Java DeveloperPolyglot & Functional Programming
  • This is still not an Oracle legal slide 2
  • Why Polyglot & Functional?• 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
  • 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
  • Java is a conservative language• New language features take a while to arrive in Java• This is deliberate• Languages that move quickly can “repent at leisure” - There are some Scala features which everyone now regrets• Non-Java languages are a test-bed for features - A good place to learn and experiment
  • 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
  • Languages on the JVM• 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
  • Polyglot Programming Pyramid• Courtesy of Ola Bini
  • Ola - JVM languages expert. Do not talk to him Your brain will melt 9
  • 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
  • Ask yourself before going Poly• Is the project area low risk?• How easily does the language interoperate with Java?• What tooling support is there?• Is it easy to build, test & deploy in this language?• How steep is the learning curve? - How easy is it to hire developers?
  • Experts: “use < 5 bullet points” sr&%w that! 12
  • Matt Raible - 20 criteria web frameworks – 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
  • JVM language web framework shoot out • Grails Wins! • http://bit.ly/jvm-frameworks-matrix
  • Functional Programming• Functional Programming is important again - Multi-core CPUs means that your code can truly run parallel - map, reduce, filter idioms would be welcome!• Javas support is mostly missing - Java 7s Fork and Join provides a bit of Map/Reduce - How would we want it to work in Java?• Other languages on the JVM already provide support - Groovy, Scala, Clojure
  • 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.
  • Reconciliation - Java Take 1
  • 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
  • Analysing the data• Q: What’s gone wrong?• A: 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
  • Reconciliation - Fixing Case Sensitivity • With this:
  • Functional Realisation• If you’ve ever found yourself writing collections code• And get frustrated because... - There’s a method which almost provides a way.. - To do what you need.. - But you just need to tweak it slightly..• Then that frustration is an itch - That could be scratched by functional programming!
  • Introducing Functional Concepts • 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
  • What if 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.• Wed need some way to treat the as if it was a value - We want to be able to put it into a variable.• FP requires the ability to represent bits of logic as though they were values.
  • Reconciliation - With Match Function
  • 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 library support as well as the language primitive
  • “What if” is for namby pamby dreamers 26
  • The Map Pattern• FP fans would call this a map expression - extractPrimaryKeys() takes List & returns new List - Run an operation on each element in turn • And return the new list we built up
  • More on the Map Pattern• Note that the type contained in the returned List may be different from the incoming List. - In our example, incoming type is DBInfo, outgoing String - The original List hasn’t been affected in any way.• This is where “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.
  • The Filter Pattern• The use of map is an absolutely classic FP idiom.• It’s usually paired with the filter idiom.
  • Functions-as-values• We can construct our "function-as-a-value" - Need a 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
  • There you go Scala folks!We finally gave you some props It’s the last time 31
  • What We Didn’t Cover• Why functional is good for modern concurrency - That’s in the next hour!• The myriad academic definitions of closures• Groovy, Scala, Clojure, JRuby, Jython, Nashorn - Well actually....• The Groovy folder you copied from the USB stick - Unzip the groovy-2.0.0.zip to a location of your choice - Work through as much of CH08 as you like!
  • What? You still here?Go take a break will you! 33