SlideShare une entreprise Scribd logo
1  sur  51
Télécharger pour lire hors ligne
(Clojure Intro)
     @basav
Agenda

• Background
• What is Clojure?
• Why Clojure?
• Some code..
Clojure Vocabulary
Clojure Vocabulary
 Functional             LISP           STM
 Programming
                           lambda calculus
   recursion
                           (λ)
       prefix notation
                                        JVM
Lazy Sequences          macros

  homoiconicity     REPL          S-expressions
Who are these guys?
Jedi Masters of
Programming Taxonomy
     http://en.wikipedia.org/wiki/
Comparison_of_programming_paradigms
What is Clojure?

Clojure is a dynamic, strongly
typed, functional, high
performance implementation
of Lisp on the JVM (and CLR
and yes JavaScript)
What is Lisp?
LISP stands for LISt Processing.
Designed by John McCarthy.
Implementation of lambda
calculus.
Oldest Language - Circa 1958
Uses Lots of ( )s - Polish Prefix
notation
Known for AI, DSLs etc
Example LISP Code




http://kazimirmajorinc.blogspot.in/2012/03/few-examples-of-lisp-code-typography.html
What is lambda calculus?
• Formal system computation by
  for expressing
                 in Computer Science

    functions
• Consists of λ expressions
    •   variables v1, v2, ..., vn, ...
    •   the abstraction symbols λ and .
    •   parentheses ( )
•   Examples
•   λx.x — is a function taking an argument x,
    and returning x                                  Alonzo Church



•   f x — is a function f applied to an argument x
Clojure History
• Project led by Rich Hickey
    •   1.0 in 2007

    •   1.1 in 2009

    •   1.2 in 2010

    •   1.3 in 2011

    •   1.4 in 2012

    •   1.5 released in March 2013


•   Maintained on Github

    •   contains one jar file clojure.jar

•   You can get it from Maven repo
Why Clojure?
http://java.dzone.com/articles/results-2012-state-clojure
Q: How long have you been using Clojure?
Q: What language did you use just prior to adopting Clojure ?
Q: How would you characterize your use of
Clojure today?
Q: In which domain(s) are you using Clojure?
Q: What have been the biggest wins for you in
using Clojure?
Q: Which development environment(s) do you
use to work with Clojure?
Why Clojure?
•   Good at working with Data

    •   Rich literal types like arrays, sets, data types

•   Good at expressing algorithms

    •   Functional

•   Great for concurrency

    •   Immutable data structure

    •   Innovative approach to state - STM
Some Clojure Syntax
println("Hello world!") ;
(println "Hello world!") ;
Data literals
(1 2 3 "this" "is" :a :list)
[1 2 3 "this is a vector"]
{:key "value" :two 2 }
#{"I" "am" "set" }
(println "this is a function call")
fn call


(println "this is a function call")
fn call         arguments


(println "this is a function call")
fn call          arguments


(println "this is a function call")


             list
syntax

(fn-name arg1 arg2 arg3 ...)
Functional Programming
Functional Programming
     A pure Function




    A impure Function
Functional Programming
             A pure Function
                                 don’t
                                 ...look outside their box
 Arguments        (Perform       ...modify anything, anywhere
                                  ...print messages to the user
             calculations, may   ...write to disk


Return Value call other pure     i.e. No Side Effects

                 functions)      E.g. f(x) = x * x + 1




            A impure Function
Functional Programming
             A pure Function
                                 don’t
                                 ...look outside their box
 Arguments        (Perform       ...modify anything, anywhere
                                  ...print messages to the user
             calculations, may   ...write to disk


Return Value call other pure     i.e. No Side Effects

                 functions)      E.g. f(x) = x * x + 1




           A impure Function
 Arguments                 Read External State
               (perform
Return Value calculations) Write External State
                             (Side Effect)
Functional
Programming
Functional
           Programming
• Project Euler (http://projecteuler.net)
Functional
          Programming
• Project Euler (http://projecteuler.net)
• Problem 1: If we list all the natural numbers
  below 10 that are multiples of 3 and 5, we
  get 3,5,6,9. The sum of these mutiples is 23.
Functional
          Programming
• Project Euler (http://projecteuler.net)
• Problem 1: If we list all the natural numbers
  below 10 that are multiples of 3 and 5, we
  get 3,5,6,9. The sum of these mutiples is 23.
  Find the sum of all the multiples of 3 and 5
  below 1000.
Project Euler: range
Project Euler: range

• I need a function to get a list of numbers
Project Euler: range

• I need a function to get a list of numbers
  (range 5) => (0 1 2 3 4)
Project Euler: range

• I need a function to get a list of numbers
  (range 5) => (0 1 2 3 4)
• So to get a range of (0-999) we can do
Project Euler: range

• I need a function to get a list of numbers
  (range 5) => (0 1 2 3 4)
• So to get a range of (0-999) we can do
  (range 1000)
Project Euler:numbers
 that are divisible by
       3 and 5

• zero?
• rem
Project Euler: filter

• Next something that returns a list of
  selected values from a larger list
  (filter even? (range 5)) => (0 2 4)
• filter function takes a predicate and a
  sequence (aka collection)
Project Euler: reduce
• Finally something to add up these numbers
• reduce takes a function and a sequence.
  Applies the function to the first two
  elements of the sequence, then applies the
  function to the result and third item and so
  on
  (reduce + (range 6)) ==> 15
Project Euler:Completed
          program
(defn div_by_3_or_5? [n]
      (or (zero? (rem n 3))
          (zero? (rem n 5))
         )
 )
(reduce +
         (filter div_by_3_or_5?
            (range 1000)
          )
   )
Books / Resources
http://alexott.net/en/clojure/video.html
Thank you
Appendix
Example of Clojure Code
10 threads manipulating one shared data structure, which consists of 100 vectors each one
containing 10 (initially sequential) unique numbers. Each thread then repeatedly selects two
random positions in two random vectors and swaps them. All changes to the vectors occur
in transactions by making use of clojure's software transactional memory system. That's
why even after 100 000 iterations of each thread no number got lost.
 (defn run [nvecs nitems nthreads niters]
   (let [vec-refs (vec (map (comp ref vec)
                             (partition nitems (range (* nvecs nitems)))))
         swap #(let [v1 (rand-int nvecs)
                      v2 (rand-int nvecs)
                      i1 (rand-int nitems)
                      i2 (rand-int nitems)]
                 (dosync
                  (let [temp (nth @(vec-refs v1) i1)]
                     (alter (vec-refs v1) assoc i1 (nth @(vec-refs v2) i2))
                     (alter (vec-refs v2) assoc i2 temp))))
         report #(do
                  (prn (map deref vec-refs))
                  (println "Distinct:"
                            (count (distinct (apply concat (map deref vec-refs))))))]
     (report)
     (dorun (apply pcalls (repeat nthreads #(dotimes [_ niters] (swap)))))
     (report)))

 (run 100 10 10 100000)

Contenu connexe

Tendances

20170317 functional programming in julia
20170317 functional programming in julia20170317 functional programming in julia
20170317 functional programming in julia岳華 杜
 
Yin Yangs of Software Development
Yin Yangs of Software DevelopmentYin Yangs of Software Development
Yin Yangs of Software DevelopmentNaveenkumar Muguda
 
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...GeeksLab Odessa
 
Clojure Intro
Clojure IntroClojure Intro
Clojure Introthnetos
 
Understanding Javascript Engines
Understanding Javascript Engines Understanding Javascript Engines
Understanding Javascript Engines Parashuram N
 
Kotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime PerformanceKotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime Performanceintelliyole
 
[Codemotion 2015] patrones de diseño con java8
[Codemotion 2015] patrones de diseño con java8[Codemotion 2015] patrones de diseño con java8
[Codemotion 2015] patrones de diseño con java8Alonso Torres
 
C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8Christian Nagel
 
The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202Mahmoud Samir Fayed
 
From Lisp to Clojure/Incanter and RAn Introduction
From Lisp to Clojure/Incanter and RAn IntroductionFrom Lisp to Clojure/Incanter and RAn Introduction
From Lisp to Clojure/Incanter and RAn Introductionelliando dias
 
Orthogonal Functional Architecture
Orthogonal Functional ArchitectureOrthogonal Functional Architecture
Orthogonal Functional ArchitectureJohn De Goes
 
Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional ProgrammingFrancesco Bruni
 
All Aboard The Scala-to-PureScript Express!
All Aboard The Scala-to-PureScript Express!All Aboard The Scala-to-PureScript Express!
All Aboard The Scala-to-PureScript Express!John De Goes
 

Tendances (20)

Kotlin, why?
Kotlin, why?Kotlin, why?
Kotlin, why?
 
20170317 functional programming in julia
20170317 functional programming in julia20170317 functional programming in julia
20170317 functional programming in julia
 
Yin Yangs of Software Development
Yin Yangs of Software DevelopmentYin Yangs of Software Development
Yin Yangs of Software Development
 
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
 
Clojure Intro
Clojure IntroClojure Intro
Clojure Intro
 
Understanding Javascript Engines
Understanding Javascript Engines Understanding Javascript Engines
Understanding Javascript Engines
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
 
MP in Clojure
MP in ClojureMP in Clojure
MP in Clojure
 
Kotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime PerformanceKotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime Performance
 
[Codemotion 2015] patrones de diseño con java8
[Codemotion 2015] patrones de diseño con java8[Codemotion 2015] patrones de diseño con java8
[Codemotion 2015] patrones de diseño con java8
 
C# for Java Developers
C# for Java DevelopersC# for Java Developers
C# for Java Developers
 
C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8
 
The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202
 
From Lisp to Clojure/Incanter and RAn Introduction
From Lisp to Clojure/Incanter and RAn IntroductionFrom Lisp to Clojure/Incanter and RAn Introduction
From Lisp to Clojure/Incanter and RAn Introduction
 
Orthogonal Functional Architecture
Orthogonal Functional ArchitectureOrthogonal Functional Architecture
Orthogonal Functional Architecture
 
Introduction to Julia Language
Introduction to Julia LanguageIntroduction to Julia Language
Introduction to Julia Language
 
Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional Programming
 
All Aboard The Scala-to-PureScript Express!
All Aboard The Scala-to-PureScript Express!All Aboard The Scala-to-PureScript Express!
All Aboard The Scala-to-PureScript Express!
 
Introduction to Kotlin
Introduction to KotlinIntroduction to Kotlin
Introduction to Kotlin
 
Why Haskell
Why HaskellWhy Haskell
Why Haskell
 

Similaire à Clojure intro

Exploring Clojurescript
Exploring ClojurescriptExploring Clojurescript
Exploring ClojurescriptLuke Donnet
 
Functional Programming Past Present Future
Functional Programming Past Present FutureFunctional Programming Past Present Future
Functional Programming Past Present FutureIndicThreads
 
Functional Programming - Past, Present and Future
Functional Programming - Past, Present and FutureFunctional Programming - Past, Present and Future
Functional Programming - Past, Present and FuturePushkar Kulkarni
 
Introduction to Elixir
Introduction to ElixirIntroduction to Elixir
Introduction to ElixirDiacode
 
Getting started with Clojure
Getting started with ClojureGetting started with Clojure
Getting started with ClojureJohn Stevenson
 
A brief introduction to lisp language
A brief introduction to lisp languageA brief introduction to lisp language
A brief introduction to lisp languageDavid Gu
 
Exploiting Concurrency with Dynamic Languages
Exploiting Concurrency with Dynamic LanguagesExploiting Concurrency with Dynamic Languages
Exploiting Concurrency with Dynamic LanguagesTobias Lindaaker
 
Scala clojure techday_2011
Scala clojure techday_2011Scala clojure techday_2011
Scala clojure techday_2011Thadeu Russo
 
Fuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional ProgrammingFuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional ProgrammingShine Xavier
 
Clojure made-simple - John Stevenson
Clojure made-simple - John StevensonClojure made-simple - John Stevenson
Clojure made-simple - John StevensonJAX London
 
Concurrency Constructs Overview
Concurrency Constructs OverviewConcurrency Constructs Overview
Concurrency Constructs Overviewstasimus
 
Booting into functional programming
Booting into functional programmingBooting into functional programming
Booting into functional programmingDhaval Dalal
 
Part 3-functions
Part 3-functionsPart 3-functions
Part 3-functionsankita44
 
Iterarators and generators in python
Iterarators and generators in pythonIterarators and generators in python
Iterarators and generators in pythonSarfaraz Ghanta
 

Similaire à Clojure intro (20)

Exploring Clojurescript
Exploring ClojurescriptExploring Clojurescript
Exploring Clojurescript
 
Functional Programming Past Present Future
Functional Programming Past Present FutureFunctional Programming Past Present Future
Functional Programming Past Present Future
 
Functional Programming - Past, Present and Future
Functional Programming - Past, Present and FutureFunctional Programming - Past, Present and Future
Functional Programming - Past, Present and Future
 
Introduction to Elixir
Introduction to ElixirIntroduction to Elixir
Introduction to Elixir
 
Clojure
ClojureClojure
Clojure
 
Getting started with Clojure
Getting started with ClojureGetting started with Clojure
Getting started with Clojure
 
A brief introduction to lisp language
A brief introduction to lisp languageA brief introduction to lisp language
A brief introduction to lisp language
 
Exploiting Concurrency with Dynamic Languages
Exploiting Concurrency with Dynamic LanguagesExploiting Concurrency with Dynamic Languages
Exploiting Concurrency with Dynamic Languages
 
Scala clojure techday_2011
Scala clojure techday_2011Scala clojure techday_2011
Scala clojure techday_2011
 
Fuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional ProgrammingFuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional Programming
 
Should i Go there
Should i Go thereShould i Go there
Should i Go there
 
Clojure made-simple - John Stevenson
Clojure made-simple - John StevensonClojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
 
Concurrency Constructs Overview
Concurrency Constructs OverviewConcurrency Constructs Overview
Concurrency Constructs Overview
 
Booting into functional programming
Booting into functional programmingBooting into functional programming
Booting into functional programming
 
Part 3-functions
Part 3-functionsPart 3-functions
Part 3-functions
 
Hadoop + Clojure
Hadoop + ClojureHadoop + Clojure
Hadoop + Clojure
 
Gnu octave
Gnu octave Gnu octave
Gnu octave
 
Hw09 Hadoop + Clojure
Hw09   Hadoop + ClojureHw09   Hadoop + Clojure
Hw09 Hadoop + Clojure
 
F#3.0
F#3.0 F#3.0
F#3.0
 
Iterarators and generators in python
Iterarators and generators in pythonIterarators and generators in python
Iterarators and generators in python
 

Dernier

Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxMatsuo Lab
 
20200723_insight_release_plan_v6.pdf20200723_insight_release_plan_v6.pdf
20200723_insight_release_plan_v6.pdf20200723_insight_release_plan_v6.pdf20200723_insight_release_plan_v6.pdf20200723_insight_release_plan_v6.pdf
20200723_insight_release_plan_v6.pdf20200723_insight_release_plan_v6.pdfJamie (Taka) Wang
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...DianaGray10
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Will Schroeder
 
Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.francesco barbera
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024SkyPlanner
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...Aggregage
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Websitedgelyza
 
Cloud Revolution: Exploring the New Wave of Serverless Spatial Data
Cloud Revolution: Exploring the New Wave of Serverless Spatial DataCloud Revolution: Exploring the New Wave of Serverless Spatial Data
Cloud Revolution: Exploring the New Wave of Serverless Spatial DataSafe Software
 
Babel Compiler - Transforming JavaScript for All Browsers.pptx
Babel Compiler - Transforming JavaScript for All Browsers.pptxBabel Compiler - Transforming JavaScript for All Browsers.pptx
Babel Compiler - Transforming JavaScript for All Browsers.pptxYounusS2
 
Introduction to Quantum Computing
Introduction to Quantum ComputingIntroduction to Quantum Computing
Introduction to Quantum ComputingGDSC PJATK
 
Comparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioComparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioChristian Posta
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7DianaGray10
 
PicPay - GenAI Finance Assistant - ChatGPT for Customer Service
PicPay - GenAI Finance Assistant - ChatGPT for Customer ServicePicPay - GenAI Finance Assistant - ChatGPT for Customer Service
PicPay - GenAI Finance Assistant - ChatGPT for Customer ServiceRenan Moreira de Oliveira
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesDavid Newbury
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfAijun Zhang
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdfPedro Manuel
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxGDSC PJATK
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024D Cloud Solutions
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfDaniel Santiago Silva Capera
 

Dernier (20)

Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptx
 
20200723_insight_release_plan_v6.pdf20200723_insight_release_plan_v6.pdf
20200723_insight_release_plan_v6.pdf20200723_insight_release_plan_v6.pdf20200723_insight_release_plan_v6.pdf20200723_insight_release_plan_v6.pdf
20200723_insight_release_plan_v6.pdf20200723_insight_release_plan_v6.pdf
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
 
Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Website
 
Cloud Revolution: Exploring the New Wave of Serverless Spatial Data
Cloud Revolution: Exploring the New Wave of Serverless Spatial DataCloud Revolution: Exploring the New Wave of Serverless Spatial Data
Cloud Revolution: Exploring the New Wave of Serverless Spatial Data
 
Babel Compiler - Transforming JavaScript for All Browsers.pptx
Babel Compiler - Transforming JavaScript for All Browsers.pptxBabel Compiler - Transforming JavaScript for All Browsers.pptx
Babel Compiler - Transforming JavaScript for All Browsers.pptx
 
Introduction to Quantum Computing
Introduction to Quantum ComputingIntroduction to Quantum Computing
Introduction to Quantum Computing
 
Comparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioComparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and Istio
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7
 
PicPay - GenAI Finance Assistant - ChatGPT for Customer Service
PicPay - GenAI Finance Assistant - ChatGPT for Customer ServicePicPay - GenAI Finance Assistant - ChatGPT for Customer Service
PicPay - GenAI Finance Assistant - ChatGPT for Customer Service
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond Ontologies
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdf
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdf
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptx
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
 

Clojure intro

  • 2. Agenda • Background • What is Clojure? • Why Clojure? • Some code..
  • 4. Clojure Vocabulary Functional LISP STM Programming lambda calculus recursion (λ) prefix notation JVM Lazy Sequences macros homoiconicity REPL S-expressions
  • 7. Programming Taxonomy http://en.wikipedia.org/wiki/ Comparison_of_programming_paradigms
  • 8. What is Clojure? Clojure is a dynamic, strongly typed, functional, high performance implementation of Lisp on the JVM (and CLR and yes JavaScript)
  • 9. What is Lisp? LISP stands for LISt Processing. Designed by John McCarthy. Implementation of lambda calculus. Oldest Language - Circa 1958 Uses Lots of ( )s - Polish Prefix notation Known for AI, DSLs etc
  • 11. What is lambda calculus? • Formal system computation by for expressing in Computer Science functions • Consists of λ expressions • variables v1, v2, ..., vn, ... • the abstraction symbols λ and . • parentheses ( ) • Examples • λx.x — is a function taking an argument x, and returning x Alonzo Church • f x — is a function f applied to an argument x
  • 12. Clojure History • Project led by Rich Hickey • 1.0 in 2007 • 1.1 in 2009 • 1.2 in 2010 • 1.3 in 2011 • 1.4 in 2012 • 1.5 released in March 2013 • Maintained on Github • contains one jar file clojure.jar • You can get it from Maven repo
  • 15. Q: How long have you been using Clojure?
  • 16. Q: What language did you use just prior to adopting Clojure ?
  • 17. Q: How would you characterize your use of Clojure today?
  • 18. Q: In which domain(s) are you using Clojure?
  • 19. Q: What have been the biggest wins for you in using Clojure?
  • 20. Q: Which development environment(s) do you use to work with Clojure?
  • 21. Why Clojure? • Good at working with Data • Rich literal types like arrays, sets, data types • Good at expressing algorithms • Functional • Great for concurrency • Immutable data structure • Innovative approach to state - STM
  • 25. Data literals (1 2 3 "this" "is" :a :list) [1 2 3 "this is a vector"] {:key "value" :two 2 } #{"I" "am" "set" }
  • 26. (println "this is a function call")
  • 27. fn call (println "this is a function call")
  • 28. fn call arguments (println "this is a function call")
  • 29. fn call arguments (println "this is a function call") list
  • 32. Functional Programming A pure Function A impure Function
  • 33. Functional Programming A pure Function don’t ...look outside their box Arguments (Perform ...modify anything, anywhere ...print messages to the user calculations, may ...write to disk Return Value call other pure i.e. No Side Effects functions) E.g. f(x) = x * x + 1 A impure Function
  • 34. Functional Programming A pure Function don’t ...look outside their box Arguments (Perform ...modify anything, anywhere ...print messages to the user calculations, may ...write to disk Return Value call other pure i.e. No Side Effects functions) E.g. f(x) = x * x + 1 A impure Function Arguments Read External State (perform Return Value calculations) Write External State (Side Effect)
  • 36. Functional Programming • Project Euler (http://projecteuler.net)
  • 37. Functional Programming • Project Euler (http://projecteuler.net) • Problem 1: If we list all the natural numbers below 10 that are multiples of 3 and 5, we get 3,5,6,9. The sum of these mutiples is 23.
  • 38. Functional Programming • Project Euler (http://projecteuler.net) • Problem 1: If we list all the natural numbers below 10 that are multiples of 3 and 5, we get 3,5,6,9. The sum of these mutiples is 23. Find the sum of all the multiples of 3 and 5 below 1000.
  • 40. Project Euler: range • I need a function to get a list of numbers
  • 41. Project Euler: range • I need a function to get a list of numbers (range 5) => (0 1 2 3 4)
  • 42. Project Euler: range • I need a function to get a list of numbers (range 5) => (0 1 2 3 4) • So to get a range of (0-999) we can do
  • 43. Project Euler: range • I need a function to get a list of numbers (range 5) => (0 1 2 3 4) • So to get a range of (0-999) we can do (range 1000)
  • 44. Project Euler:numbers that are divisible by 3 and 5 • zero? • rem
  • 45. Project Euler: filter • Next something that returns a list of selected values from a larger list (filter even? (range 5)) => (0 2 4) • filter function takes a predicate and a sequence (aka collection)
  • 46. Project Euler: reduce • Finally something to add up these numbers • reduce takes a function and a sequence. Applies the function to the first two elements of the sequence, then applies the function to the result and third item and so on (reduce + (range 6)) ==> 15
  • 47. Project Euler:Completed program (defn div_by_3_or_5? [n] (or (zero? (rem n 3)) (zero? (rem n 5)) ) ) (reduce + (filter div_by_3_or_5? (range 1000) ) )
  • 51. Example of Clojure Code 10 threads manipulating one shared data structure, which consists of 100 vectors each one containing 10 (initially sequential) unique numbers. Each thread then repeatedly selects two random positions in two random vectors and swaps them. All changes to the vectors occur in transactions by making use of clojure's software transactional memory system. That's why even after 100 000 iterations of each thread no number got lost. (defn run [nvecs nitems nthreads niters] (let [vec-refs (vec (map (comp ref vec) (partition nitems (range (* nvecs nitems))))) swap #(let [v1 (rand-int nvecs) v2 (rand-int nvecs) i1 (rand-int nitems) i2 (rand-int nitems)] (dosync (let [temp (nth @(vec-refs v1) i1)] (alter (vec-refs v1) assoc i1 (nth @(vec-refs v2) i2)) (alter (vec-refs v2) assoc i2 temp)))) report #(do (prn (map deref vec-refs)) (println "Distinct:" (count (distinct (apply concat (map deref vec-refs))))))] (report) (dorun (apply pcalls (repeat nthreads #(dotimes [_ niters] (swap))))) (report))) (run 100 10 10 100000)