indicthreads-pune12-polyglot & functional programming on jvm-old

Upload: indicthreads

Post on 04-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    1/27

    Polyglot and Functional Programmingon the Java Virtual Machine

    Mohan Kumar MuddanaInfoStretch Pvt. Ltd.

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    2/27

    2

    Languages on JVM

    JVM

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    3/27

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    4/27

    4

    Polyglot Programming on the JVM

    Different languages bring in different features.

    Wider choice of availability.

    Better programming features.

    Imperative and Functional

    Interoperability between the languages.

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    5/27

    5

    Functional Programming

    Derives from mathematics, Lambda Calculus

    Everything is functions First class functions

    Immutability and Concurrency

    Higher Order Functions Dynamic language constructs

    Lesser code with fewer language constructs.

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    6/27

    6

    History of Functional Programming

    Alonzo Church

    Language timelines

    Resurgence of functional languages with theadvent of Multi-Core processors.

    Need to have easier Domain Specific constructs.

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    7/27

    7

    Scala Language aka Scalable

    Why Scala

    How it is scalable

    Getting Started

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    8/27

    8

    Scala Object World

    Scala is Pure Object Oriented Language

    Traits Case Classes and Pattern Matching

    Type safety and Type Erasure

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    9/27

    9

    Scala Classes and Objects

    Everything is objects.

    No primitives wandering around your objects.

    Java like classes definitions.

    Store objects into a variables.

    class HelloWorld() {

    val welcome: String = Hello World

    def msg(name: String) = println(welcome + name)

    }

    val h = new HelloWorld()

    h.msg(Your Name)

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    10/27

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    11/27

    11

    Scala Case Classes and Pattern Match

    Case classes provides pattern matching on objects

    without large amount of boilerplate code. Case classes are normal classes with a case modifier as

    prefix.

    - Case Classes comes with additional conventions.- The compiler adds a factory method with the name of the

    class, so no need to create an instance with new operator.

    - All arguments in the parameter list gets implicit val prefix, so

    they are maintained as fields.- Compiler adds natural implementations of toString,

    hashCode and equals.

    S l S f d

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    12/27

    12

    Scala Type Safety and Erasure

    Scala entails static type safety

    Scala compilor ensures type safety. Type Erasure

    S l F i l

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    13/27

    13

    Scala Functional

    Functions without side effects

    Functions inside Functions

    Anonymous Functions or Function Literals

    Higher Order Functions

    S l F i

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    14/27

    14

    Scala Functions

    Currying

    Closures:

    The function value thats created at runtime from thisfunction

    Literal is called a closure. The name arises from the act ofclosing the function

    Literal by capturing the bindings of its free variables.

    val add(a: Int) => a + xyz

    S l C ll ti

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    15/27

    15

    Scala Collections

    Immutable Collections

    Filtering and Combinators

    S l C ll ti

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    16/27

    16

    Scala Collections

    Recursion gives a powerful way of processing data

    structures without looping syntax

    Tail Recursion Functions which call themselves as their last action in their

    function definition, are called tail recursive

    Compiler optimizes tail calls by calling in single frame

    I t S l t J

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    17/27

    17

    Interop Scala to Java

    Scala code complies into bytecode similar to Javaclasses, methods, exceptions and strings.

    Scala features map directly onto the equivalent Javafeatures.

    Cons:Values (Wrapper Classes and primitives)

    Singleton objects (Scala hierarchy is different from Java)

    Traits as interfaces

    Stateless

    S l C St d

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    18/27

    18

    Scala Case Study

    Stateless

    Clojure a Lisp on JVM

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    19/27

    19

    Clojure a Lisp on JVM

    Why Clojure

    Pure functional language Stateless

    Homoiconicity

    (Un)digestive Syntax Which you might fall in love

    later

    Persistent data structures

    STM

    Atom and Agents

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    20/27

    Clojure Homoiconicity

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    21/27

    21

    Clojure Homoiconicity

    Representation of its own data structures and atomicvalues or more casually code-as-data.

    (defn average [numbers]

    (/ (apply + numbers) (count numbers))) /* List Data Structure

    This definition is a list of data structure containing symbols,values, vectors and another list consists of function body

    Stateless

    Clojure STM

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    22/27

    22

    Clojure STMSoftware Transactional Manager is the way to handle concurrency of

    mutable data in Clojure.

    Using refs, dosync, deref, ref-set, alter and commute

    Like database transactions, STM transactions guarantee some importantproperties:

    Updates are atomic. If you update more than one ref in a transaction, thecumulative effect of all the updates will appear as a single instantaneousevent to anyone not inside your transaction.

    Updates are consistent. Refs can specify validation functions. If any ofthese functions fail, the entire transaction will fail.

    Updates are isolated. Running transactions cannot see partially completedresults from other transactions.

    Stateless

    Clojure STM

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    23/27

    23

    Clojure STMAtoms

    Atoms provide a way to manage shared, synchronous,

    independent state.Atoms are an efficient way to represent some state that willnever need to be coordinated with any other, and for which

    you wish to make synchronous changes

    Agents

    Agents provide independent, asynchronous change ofindividual locations.

    Agents are bound to a single storage location for theirlifetime, and only allow mutation of that location (to a newstate) to occur as a result of an action.

    Stateless

    Interop Clojure to Java

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    24/27

    24

    Interop Clojure to Java

    Clojure is complied and generates Bytecode

    Clojure embraces Java and its libraries. Idiomatic

    Clojure code can call Java libraries directly

    Creating Java instances and accessing its methods.

    In REPL:(def cal (java.util.Calendar/getInstance)

    (. cal getTime)

    Code:

    (import [java.util.Calendar])

    (defn cal (.getInstance java.util.Calendar))

    Stateless

    Interop Java to Clojure

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    25/27

    25

    Interop Java to Clojure

    Clojure is implemented as Java class library.

    Embed Clojure using load code and call functions

    import clojure.lang.RT;

    import clojure.lang.Var;

    public class Foo {

    public static void main(String[] args) throws Exception {

    RT.loadResourceScript("foo.clj"); Var foo = RT.var("user", "foo");

    Object result = foo.invoke("Hi", "there");System.out.println(result);

    }}

    (user being the namespace and foo being the function from Clj)

    Stateless

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    26/27

  • 7/30/2019 IndicThreads-Pune12-Polyglot & Functional Programming on JVM-Old

    27/27

    27

    Thank you

    gmail [email protected]

    twitter @mohanjune Stateless