Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Clojure for Java [OOPs] Programmers

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

Clojure for Java [OOPs] Programmers

Explain the differences and make it easier for OOPS programmers to adopt Clojure.

Avatar for Rashmi Mittal

Rashmi Mittal

January 17, 2020

Other Decks in Programming

Transcript

  1. Clojure for Java [OOPs] Programmers Madhuparna Ghosh - Senior Software

    Developer Rashmi Mittal - Head of Engineering Quintype Technologies India Pvt Ltd
  2. Intent ‘legacy’ OOPS programmers vs ‘current’ functional programmers - Completely

    different paradigms - people find it hard to switch from one to the other - make it easier for OOPS programmers to adopt Clojure - Explain the differences - Clojure built on Java - focus on Clojure underlying workings as it relates to Java - Refrain from comparing which one is better.
  3. Topics • Paradigm • Object References / Clojure Persistent Data

    Structure • Clojure hosted on JVM • Seq vs Iterator • Polymorphism • Mutability in Clojure
  4. Paradigm Object Oriented Programming Encapsulation of Data • Model classes

    based on real-world entities • Define a contract for use of entity and hide all other data. public class Adder { private int x = 0; public Adder(int x1) { x = x1; } public int add(int y) { return x + y; } } Functional Programming - Clojure Emphasis on Data Immutability • Functions return the same output for given input • Lisp-based - code-as-data • Hosted on the JVM (ns Adder) (defn add-int [x y] (+ x y))
  5. Mathematical functions y = f(x) Immutable functions are like mathematical

    functions unlike the methods, procedures of other programming languages. No side effects.
  6. Object References • OOPS requires management of references, hence their

    state change. • It’s important to follow best practices to control the state change. • References also lead to null objects. public foo(Adder adder) { adder.add(10); } Adder myAdder = new Adder(5); foo(myAdder); System.out.println(myAdder);
  7. Clojure - No Object References • Immutable functions get rid

    of references. OOPs Programmer: So you make copies of data every time a function returns a value?!! Clojure Programmer: Heck No! • Efficient copies by sharing elements from original data structure while maintaining Persistent Data Structures.
  8. Implemented using Java • Clojure - built on Java, hosted

    on the JVM • All underlying Java functionality is available. (.trim (String. " test ")) -> “test” (type " test ") -> java.lang.String (.trim " test ") -> “test” (clojure.string/trim " test ") -> “test”
  9. Seq vs Iterator • Java Iterators allow for Collection Iteration,

    collections need to implement the Iterator interface. • Seq follows the abstraction pattern, mostly all Clojure collections implement the ISeq interface. • Stateless and persistent compared to Iterator. (every? seq ["1" [1] '(1) {:1 1} #{1}]) => true
  10. • Ability for an object to have multiple forms/behaviors. •

    Key concept in OOP - override or overload. Polymorphism public class Adder implements MyMath { int compute (int x) {} float compute (float f, int p) {} } public class ComplexAdder extends Adder { int compute (int x) {} float compute (float f, int p) {} } public interface MyMath { int compute (int x); float compute (float f, int p); }
  11. • defprotocol - Define an interface of functions, creates a

    Java Interface internally. • Interface can be implemented using defrecord or deftype. • defrecord generates bytecode for a Java class with the given name in a package with given namespace. Polymorphism - Clojure (def myadder1 (Adder. 1)) -> user.Adder (compute myadder1 2) -> 3 (defprotocol MyMath (compute [this y])) (defrecord Adder [x] MyMath (compute [_ y] (+ x y)))
  12. Mutability in Clojure • Data immutability makes Clojure inherently safe

    for concurrent programming • That said, there is controlled support for shared state management using a few data types - Vars, Refs, Agents, Atoms. • Atoms - Based on underlying Java Atomic variables. For example, AtomicInteger. • Leverage machine-level compare-and-swap (CAS) - ◦ Apply a function to the atom with a given current value. ◦ Function will CAS new value only if current value = expected old value. ◦ Else retry.
  13. Conclusion • Like all Programming languages, both Java and Clojure

    codebases can end up being inefficient if best practices not applied. • Important to understand the Paradigm and the underlying workings to master any language.