Clojure / Scala
Diego Pacheco
Clojure
❏ 2007
❏ Rich Hickey
❏ JVM based but also CLR and JavaScript
❏ Functional dialect of Lisp
❏ Immutability & Immutable Data Structures
❏ Great interoperability with Java
❏ But -> Slow and Hard to troubleshoot.
Who is using Clojure?
https://clojure.org/community/companies
“Function as Data, Data as
functions” -- RH.
You don't need classes: Just Functions and Data!
Lisp
Lisp
Getting started with lien (Clojure build system)
❏ Clojure Build System
❏ Compatible with Maven Repos
❏ Super Easy to use https://leiningen.org/
Hello World on lein REPL
From a File (*.clj)
From a File (*.clj)
From a File (*.clj)
Basics
❏ Lists ()
❏ Vectors []
❏ Assign with Let
❏ “Strings”
❏ seq
❏ if
Functions
reduce
reduce
map
map
filter
filter
every?
Pipeline Operator (->>)
Sets
❏ Like Math Sets
❏ Unsorted
❏ NO Duplicates
❏ Efficient:
❏ Checking
❏ Removal
❏ (sorted-set)
SortedSets
Maps
❏ Also known as
❏ Dictionaries
❏ Hash Maps
❏ Assoc k/v pairs
❏ Domain data
❏ (sorted-map)
Working with “Pojos” == Maps.
❏ What can you do with a
Pojo in OOP? Nothing.
❏ In clojure? A lot !!!
❏ Remember “Alan Perlis”
❏ No need to define “schema”
❏ Productivity
❏ Simplicity
defrecord here is our class be happy :-)
https://github.com/clojure/core.match
Clojure also has Pattern Matcher
Web Development ... Compojure & Ring
$ lein new compojure hello-world
$ cd hello-world
$ lein run server-headless
Open your browser on http://localhost:3000/
handler.clj
https://clojuredocs.org/
Exercises
1. Write a function which returns the last element in a sequence without using *last*.
Test data: (A) [10 20 30 40 50] (B) '(10 8 1) (C) ["x" "y" "z"]
2. Write a function which sum of a sequence of numbers without using *reduce*.
Test data: (A) [1 2 3 4 5 6] (B) '(0 0 -1 1 2 3) (C) #{40 50 25 15 10}
3. Write a REST service with clojure, all persistence needs to be done in memory using maps or records.
You will need to create an BANK application with: deposit, withdrawal, check balance, transfer
money into other accounts. You will need do proper validations and also unit tests. Your application
need to use lein and compojure there are no other frameworks/libs allowed.
You cannot use: for, if and let.
Scala
❏ 2004
❏ Functional Programing Language
❏ Based on Haskell
❏ Runs on the JVM also JavaScript
❏ Statically typed
❏ Better Java
❏ Awesome for Big Data / ML
❏ Created by Martin Odersky
SBT
❏ Build System for Scala
❏ IF you are working with scala you also
could do:
❏ Maven
❏ Gradle
❏ In practice SBT is the right choice.
Helloworld Scala + SBT
Helloworld Scala + SBT
SBT has a REPL too ($ sbt console)
Functions
❏ Functions
❏ Lambdas
❏ Are all Objects
❏ Since
implementation in
java (JVM).
Partial Application
❏ Partial
❏ Lazy
❏ Composition
❏ Re-use
Partial Application via Currying
❏ Partial
❏ Lazy
❏ Composition
❏ Re-use
Variable lengths arguments or in java terms varargs.
❏ *
❏ Dynamic
❏ Generic
❏ Similar to
java
OOP in Scala: Class
❏ Scala support OOP
❏ Here is the better
java part starts
❏ Simple
❏ Clean
❏ Less Verbose
OOP in Scala: Class Constructor + String Interpolation
OOP in Scala: Inheritance
❏ Class Hierarchy
❏ Polymorphism
OOP in Scala: Polymorphism Overriding
❏ Class Hierarchy
❏ Polymorphism
❏ Overriding
OOP in Scala: Traits
❏ Similar to Java Interfaces
❏ However you can have code
❏ Fundamental part of type
system (“Algebra”)
❏ One of best things in Scala
❏ Be Careful it can get crazy :D
❏ Stay practical stay clean!
OOP in Scala: Generics + Types
❏ Generics
❏ Types
❏ Abstractions
❏ Leverage
Compiler
❏ Haskell way
Apply
❏ Apply
❏ Code runs
❏ Default in Scala
❏ Like toString
In Java but better
❏ Super useful
OOP in Scala: Objects
❏ Different then Java Objects
❏ Objects are single instance
❏ It's how you do Singletons in Scala
❏ Great for org static functions
❏ Often classes have companion Objects
in Scala.
❏ Functions in Scala are Objects.
Pattern Matcher
❏ One of the best features
In Scala Language.
❏ Support ifs
❏ Support “_”
❏ Support type matching
❏ Better than Switch
Pattern Matcher
Case classes
❏ One of the best features
In Scala Language.
❏ Super charged Classes
❏ Equality & toString
Support
❏ Work on Pattern
Matcher
❏ Super clean syntax
Collections
❏ Like in Java
❏ But much better
❏ Immutable
❏ Mutable
❏ Tupes
❏ Sets, Arrays, Lists,
❏ Maps vs Map
map, filter, foldLeft
❏ Functional
Combinators
❏ Core FP prog
❏ Super useful
❏ Day by Day work
Exercises
1. Write a function which finds the last element in a List without using *last*.
Test data: (A) List(1,2,3,4,5,6,7,8) (B) List(“1”,”2”,”3”) (C) List(1.0,2.0,3.0)
2. Write a function which Flatten a nested list structure.
Test data: (A) flatten(List(List(1, 1), 2, List(3, List(5, 8)))) (B) (List(), 2, List(3,4))
3. Write a REST service with Scala, all persistence needs to be done in memory using maps or records.
You will need to create an BANK application with: deposit, withdrawal, check balance, transfer
money into other accounts. You will need do proper validations and also unit tests. Your application
need to use lein and compojure there are no other frameworks/libs allowed.
You cannot use: for, if and let.
Clojure / Scala
Diego Pacheco

Clojure & Scala

  • 1.
  • 3.
    Clojure ❏ 2007 ❏ RichHickey ❏ JVM based but also CLR and JavaScript ❏ Functional dialect of Lisp ❏ Immutability & Immutable Data Structures ❏ Great interoperability with Java ❏ But -> Slow and Hard to troubleshoot.
  • 4.
    Who is usingClojure? https://clojure.org/community/companies
  • 7.
    “Function as Data,Data as functions” -- RH. You don't need classes: Just Functions and Data!
  • 9.
  • 10.
  • 12.
    Getting started withlien (Clojure build system) ❏ Clojure Build System ❏ Compatible with Maven Repos ❏ Super Easy to use https://leiningen.org/
  • 13.
    Hello World onlein REPL
  • 14.
    From a File(*.clj)
  • 15.
    From a File(*.clj)
  • 16.
    From a File(*.clj)
  • 17.
    Basics ❏ Lists () ❏Vectors [] ❏ Assign with Let ❏ “Strings” ❏ seq ❏ if
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
    Sets ❏ Like MathSets ❏ Unsorted ❏ NO Duplicates ❏ Efficient: ❏ Checking ❏ Removal ❏ (sorted-set)
  • 28.
  • 29.
    Maps ❏ Also knownas ❏ Dictionaries ❏ Hash Maps ❏ Assoc k/v pairs ❏ Domain data ❏ (sorted-map)
  • 30.
    Working with “Pojos”== Maps. ❏ What can you do with a Pojo in OOP? Nothing. ❏ In clojure? A lot !!! ❏ Remember “Alan Perlis” ❏ No need to define “schema” ❏ Productivity ❏ Simplicity
  • 31.
    defrecord here isour class be happy :-)
  • 32.
  • 33.
    Web Development ...Compojure & Ring $ lein new compojure hello-world $ cd hello-world $ lein run server-headless
  • 34.
    Open your browseron http://localhost:3000/
  • 35.
  • 36.
  • 37.
    Exercises 1. Write afunction which returns the last element in a sequence without using *last*. Test data: (A) [10 20 30 40 50] (B) '(10 8 1) (C) ["x" "y" "z"] 2. Write a function which sum of a sequence of numbers without using *reduce*. Test data: (A) [1 2 3 4 5 6] (B) '(0 0 -1 1 2 3) (C) #{40 50 25 15 10} 3. Write a REST service with clojure, all persistence needs to be done in memory using maps or records. You will need to create an BANK application with: deposit, withdrawal, check balance, transfer money into other accounts. You will need do proper validations and also unit tests. Your application need to use lein and compojure there are no other frameworks/libs allowed. You cannot use: for, if and let.
  • 38.
    Scala ❏ 2004 ❏ FunctionalPrograming Language ❏ Based on Haskell ❏ Runs on the JVM also JavaScript ❏ Statically typed ❏ Better Java ❏ Awesome for Big Data / ML ❏ Created by Martin Odersky
  • 39.
    SBT ❏ Build Systemfor Scala ❏ IF you are working with scala you also could do: ❏ Maven ❏ Gradle ❏ In practice SBT is the right choice.
  • 40.
  • 41.
  • 42.
    SBT has aREPL too ($ sbt console)
  • 43.
    Functions ❏ Functions ❏ Lambdas ❏Are all Objects ❏ Since implementation in java (JVM).
  • 44.
    Partial Application ❏ Partial ❏Lazy ❏ Composition ❏ Re-use
  • 45.
    Partial Application viaCurrying ❏ Partial ❏ Lazy ❏ Composition ❏ Re-use
  • 46.
    Variable lengths argumentsor in java terms varargs. ❏ * ❏ Dynamic ❏ Generic ❏ Similar to java
  • 47.
    OOP in Scala:Class ❏ Scala support OOP ❏ Here is the better java part starts ❏ Simple ❏ Clean ❏ Less Verbose
  • 48.
    OOP in Scala:Class Constructor + String Interpolation
  • 49.
    OOP in Scala:Inheritance ❏ Class Hierarchy ❏ Polymorphism
  • 50.
    OOP in Scala:Polymorphism Overriding ❏ Class Hierarchy ❏ Polymorphism ❏ Overriding
  • 51.
    OOP in Scala:Traits ❏ Similar to Java Interfaces ❏ However you can have code ❏ Fundamental part of type system (“Algebra”) ❏ One of best things in Scala ❏ Be Careful it can get crazy :D ❏ Stay practical stay clean!
  • 52.
    OOP in Scala:Generics + Types ❏ Generics ❏ Types ❏ Abstractions ❏ Leverage Compiler ❏ Haskell way
  • 53.
    Apply ❏ Apply ❏ Coderuns ❏ Default in Scala ❏ Like toString In Java but better ❏ Super useful
  • 54.
    OOP in Scala:Objects ❏ Different then Java Objects ❏ Objects are single instance ❏ It's how you do Singletons in Scala ❏ Great for org static functions ❏ Often classes have companion Objects in Scala. ❏ Functions in Scala are Objects.
  • 55.
    Pattern Matcher ❏ Oneof the best features In Scala Language. ❏ Support ifs ❏ Support “_” ❏ Support type matching ❏ Better than Switch
  • 56.
  • 57.
    Case classes ❏ Oneof the best features In Scala Language. ❏ Super charged Classes ❏ Equality & toString Support ❏ Work on Pattern Matcher ❏ Super clean syntax
  • 58.
    Collections ❏ Like inJava ❏ But much better ❏ Immutable ❏ Mutable ❏ Tupes ❏ Sets, Arrays, Lists, ❏ Maps vs Map
  • 59.
    map, filter, foldLeft ❏Functional Combinators ❏ Core FP prog ❏ Super useful ❏ Day by Day work
  • 60.
    Exercises 1. Write afunction which finds the last element in a List without using *last*. Test data: (A) List(1,2,3,4,5,6,7,8) (B) List(“1”,”2”,”3”) (C) List(1.0,2.0,3.0) 2. Write a function which Flatten a nested list structure. Test data: (A) flatten(List(List(1, 1), 2, List(3, List(5, 8)))) (B) (List(), 2, List(3,4)) 3. Write a REST service with Scala, all persistence needs to be done in memory using maps or records. You will need to create an BANK application with: deposit, withdrawal, check balance, transfer money into other accounts. You will need do proper validations and also unit tests. Your application need to use lein and compojure there are no other frameworks/libs allowed. You cannot use: for, if and let.
  • 61.