Professional Documents
Culture Documents
Languages on JVM
JVM
2
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.
Language timelines Resurgence of functional languages with the advent of Multi-Core processors. Need to have easier Domain Specific constructs.
6
val welcome: String = Hello World def msg(name: String) = println(welcome + name)
} val h = new HelloWorld() h.msg(Your Name)
9
Scala Traits
It is an kind of an Interface with fields and concrete methods. Unlike Class inheritance, a class can mix (Mixins) in any number of traits. Several classes may want to have different common traits.
trait PolyglotProgrammer { def polyProg() { println("Polyglot Programmer") } }
class Person extends PolyglotProgrammer with PolyglotSpeaker {} val person = new Person println(person.polyProg) println(person.polySpeaker)
10
Type Erasure
12
Scala Functional
Functions without side effects Functions inside Functions Anonymous Functions or Function Literals Higher Order Functions
13
Scala Functions
Currying
Closures:
The function value thats created at runtime from this function Literal is called a closure. The name arises from the act of closing the function Literal by capturing the bindings of its free variables.
val add(a: Int) => a + xyz
14
Scala Collections
Immutable Collections Filtering and Combinators
15
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
16
Values (Wrapper Classes and primitives) Singleton objects (Scala hierarchy is different from Java) Traits as interfaces
Stateless
17
Stateless
18
19
Clojure Functional
Functional language
(defn name doc-string? attr-map? [params*] body) (defn helloworld [username] (str Hello, username))
Dynamic Language
Resolves types at runtime
20
Clojure Homoiconicity
Representation of its own data structures and atomic values 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
21
Clojure STM
Software Transactional Manager is the way to handle concurrency of mutable data in Clojure.
Updates are consistent. Refs can specify validation functions. If any of these functions fail, the entire transaction will fail. Updates are isolated. Running transactions cannot see partially completed results from other transactions.
22
Clojure STM
Atoms
Atoms provide a way to manage shared, synchronous, independent state. Atoms are an efficient way to represent some state that will never need to be coordinated with any other, and for which you wish to make synchronous changes
Stateless
Agents
Agents provide independent, asynchronous change of individual locations. Agents are bound to a single storage location for their lifetime, and only allow mutation of that location (to a new state) to occur as a result of an action.
23
26
Thank you
gmail mohanjune@gmail.com Stateless twitter @mohanjune
27