SlideShare a Scribd company logo
1 of 20
Download to read offline
A gentle introduction to
functional programming through
music and Clojure
Mødegruppe for F#unktionelle Københavnere, 24 November, 2015
Presented by Paul Lam
Agenda
1. What is functional programming
2. Setting up the environment
3. Making some noise
4. Making some music
5. Transforming data to music
6. Why functional programming
Wiki: Functional Programming
“In computer science, functional programming
is a programming paradigm—a style of building
the structure and elements of computer
programs—that treats computation as the
evaluation of mathematical functions and
avoids changing-state and mutable data.”
Hello World
Clojure is a dynamic programming language
that targets the Java Virtual Machine, Common
Language Runtime, and JavaScript
Data Literals
Long 42
BigInteger 1234567891234567
8912
Double 1.234
BigDecimal 1.234M
Ratio 22/7
String “fred”
Character a b c
Keyword :a, :foo
Symbol fred, ethel
Boolean true, false
nil nil
Regex #”a*b”
Collection Types
● Lists - singly linked, grow at front
○ (list 1 2 3), ‘(1 2 3), ‘(:fred "ethel" 27)
● Vectors - indexed access, grow at end
○ [1 2 :a :b], ["fred" :ethel 3/2]
● Maps - key/value associations
○ {:a 1, :b 2, :c 3}, {1 "ethel" 2 "fred"}
● Sets - collection with uniqueness constraint
○ #{:fred :ethel :lucy} ; duplicate key will error
● Heterogeneous
● Everything Nests Arbitrarily!
○ [#{:a :b} "c" [:d] {1 :e 2 [:f :g]}]
Installing Leiningen
Go to http://leiningen.org/ and follow 4 steps
(Note: You must have Java already installed)
1. Download the lein script (or on Windows lein.bat)
2. Place it on your $PATH where your shell can find it (eg. ~/bin)
3. Set it to be executable (chmod a+x ~/bin/lein)
4. Run it (lein) and it will download the self-install package
Then run: $ lein repl // Start a Clojure REPL
Clojure Doc
● http://clojure.org/cheatsheet
● http://clojuredocs.org/
● > (doc …)
Working with lists
> (+ 1 2 3)
6
> (first [1 2 3])
1
> (rest [1 2 3])
(2 3)
> (cons “x” [1 2 3])
(“x” 1 2 3)
> (take 2 [ 1 2 3 4 5])
(1 2)
> (drop 2 [1 2 3 4 5])
(3 4 5)
> (range 10)
(0 1 2 3 4 5 6 7 8 9)
> (filter odd? (range 10))
(1 3 5 7 9)
> (map odd? (range 10))
(false true false true false true
false true false true)
> (reduce + (range 10))
45
What is Overtone?
Overtone: “Collaborative Programmable Music”
http://overtone.github.io/
Interface SuperCollider (an audio synthesis
engine) using Clojure
http://supercollider.sourceforge.net/
Workshop
repo -- https://github.com/Quantisan/functional-music
Working with Lists
> (take 9 (cycle [1 2 3 4]))
(1 2 3 4 1 2 3 4 1)
> (interleave [:a :b :c :d :e] [1 2 3 4 5])
(:a 1 :b 2 :c 3 :d 4 :e 5)
> (partition 3 [1 2 3 4 5 6 7 8 9])
((1 2 3) (4 5 6) (7 8 9))
> (map vector [:a :b :c :d :e] [1 2 3 4 5])
([:a 1] [:b 2] [:c 3] [:d 4] [:e 5])
> (interpose | "asdf")
(a | s | d | f)
> (apply str (interpose | "asdf"))
"a|s|d|f"
Working with Maps and Sets
(def m {:a 1 :b 2 :c 3})
● (m :b) => 2
● (:b m) => 2
● (keys m) => (:a :b :c)
● (assoc m :d 4 :c 42) => {:d 4, :a 1, :b 2, :c 42}
● (merge-with + m {:a 2 :b 3}) => {:a 3, :b 5, :c 3}
● (union #{:a :b :c} #{:c :d :e}) => #{:d :a :b :c :e}
● (join #{{:a 1 :b 2 :c 3} {:a 1 :b 21 :c 42}}
#{{:a 1 :b 2 :e 5} {:a 1 :b 21 :d 4}})
=> #{{:d 4, :a 1, :b 21, :c 42}
{:a 1, :b 2, :c 3, :e 5}}
Java Interop
> (.toUpperCase "fred")
"FRED"
> (.getName String)
"java.lang.String"
> (System/getProperty "user.dir")
"/Users/me/clojure/interop"
> Math/PI
3.141592653589793
Java Interop
> (map #(.getName %) (.getMethods java.util.Date))
("equals" "toString" "hashCode" "clone" "compareTo" "compareTo" "parse" .
. . <and many more>)
> (java.util.Date.)
#inst "2015-01-26T21:49:01.403-00:00"
> (doto (java.util.Date.) (.setSeconds 33) (.setMinutes 22))
#inst "2015-01-26T21:22:33.785-00:00"
https://github.com/Quantisan/functional-
music/blob/solution/src/functional_music/tab.
clj
Solution
Drawbacks of FP
● requires thinking differently
● real-world programs are full of side-effects
● difficulty predicting performance profile
Benefits of Functional Programming
● Easier to reason about
● Composibility
● Separation of concern
● Huges, “Why Functional Programming Matters”, 1990
● http://weblog.raganwald.com/2007/03/why-why-functional-programming-matters.html
Contact
Paul Lam
@Quantisan
paul@quantisan.com

More Related Content

What's hot

Christian Gill ''Functional programming for the people''
Christian Gill ''Functional programming for the people''Christian Gill ''Functional programming for the people''
Christian Gill ''Functional programming for the people''OdessaJS Conf
 
Linked list int_data_fdata
Linked list int_data_fdataLinked list int_data_fdata
Linked list int_data_fdataSamsil Arefin
 
Data Visualization with R.ggplot2 and its extensions examples.
Data Visualization with R.ggplot2 and its extensions examples.Data Visualization with R.ggplot2 and its extensions examples.
Data Visualization with R.ggplot2 and its extensions examples.Dr. Volkan OBAN
 
Array matrix example programs - C language
Array matrix example programs - C languageArray matrix example programs - C language
Array matrix example programs - C languageSk_Group
 
CrystalBall - Compute Relative Frequency in Hadoop
CrystalBall - Compute Relative Frequency in Hadoop CrystalBall - Compute Relative Frequency in Hadoop
CrystalBall - Compute Relative Frequency in Hadoop Suvash Shah
 
ggtimeseries-->ggplot2 extensions
ggtimeseries-->ggplot2 extensions ggtimeseries-->ggplot2 extensions
ggtimeseries-->ggplot2 extensions Dr. Volkan OBAN
 
Coding with Vim
Coding with VimCoding with Vim
Coding with VimEnzo Wang
 
Parallel binary search
Parallel binary searchParallel binary search
Parallel binary search승혁 조
 
R-ggplot2 package Examples
R-ggplot2 package ExamplesR-ggplot2 package Examples
R-ggplot2 package ExamplesDr. Volkan OBAN
 
20160616技術會議
20160616技術會議20160616技術會議
20160616技術會議Jason Kuan
 
Monads from Definition
Monads from DefinitionMonads from Definition
Monads from DefinitionDierk König
 
Зависимые типы в GHC 8. Максим Талдыкин
Зависимые типы в GHC 8. Максим ТалдыкинЗависимые типы в GHC 8. Максим Талдыкин
Зависимые типы в GHC 8. Максим ТалдыкинЮрий Сыровецкий
 

What's hot (20)

Christian Gill ''Functional programming for the people''
Christian Gill ''Functional programming for the people''Christian Gill ''Functional programming for the people''
Christian Gill ''Functional programming for the people''
 
Sol10
Sol10Sol10
Sol10
 
Linked list int_data_fdata
Linked list int_data_fdataLinked list int_data_fdata
Linked list int_data_fdata
 
Ooprc4 b
Ooprc4 bOoprc4 b
Ooprc4 b
 
Data Visualization with R.ggplot2 and its extensions examples.
Data Visualization with R.ggplot2 and its extensions examples.Data Visualization with R.ggplot2 and its extensions examples.
Data Visualization with R.ggplot2 and its extensions examples.
 
R
RR
R
 
Array matrix example programs - C language
Array matrix example programs - C languageArray matrix example programs - C language
Array matrix example programs - C language
 
CrystalBall - Compute Relative Frequency in Hadoop
CrystalBall - Compute Relative Frequency in Hadoop CrystalBall - Compute Relative Frequency in Hadoop
CrystalBall - Compute Relative Frequency in Hadoop
 
Fragmentation
FragmentationFragmentation
Fragmentation
 
ggtimeseries-->ggplot2 extensions
ggtimeseries-->ggplot2 extensions ggtimeseries-->ggplot2 extensions
ggtimeseries-->ggplot2 extensions
 
Py lecture5 python plots
Py lecture5 python plotsPy lecture5 python plots
Py lecture5 python plots
 
Coding with Vim
Coding with VimCoding with Vim
Coding with Vim
 
Module 2 topic 2 notes
Module 2 topic 2 notesModule 2 topic 2 notes
Module 2 topic 2 notes
 
Parallel binary search
Parallel binary searchParallel binary search
Parallel binary search
 
1
11
1
 
R-ggplot2 package Examples
R-ggplot2 package ExamplesR-ggplot2 package Examples
R-ggplot2 package Examples
 
20160616技術會議
20160616技術會議20160616技術會議
20160616技術會議
 
Monads from Definition
Monads from DefinitionMonads from Definition
Monads from Definition
 
FFT
FFTFFT
FFT
 
Зависимые типы в GHC 8. Максим Талдыкин
Зависимые типы в GHC 8. Максим ТалдыкинЗависимые типы в GHC 8. Максим Талдыкин
Зависимые типы в GHC 8. Максим Талдыкин
 

Similar to A gentle introduction to functional programming through music and clojure

The Curious Clojurist - Neal Ford (Thoughtworks)
The Curious Clojurist - Neal Ford (Thoughtworks)The Curious Clojurist - Neal Ford (Thoughtworks)
The Curious Clojurist - Neal Ford (Thoughtworks)jaxLondonConference
 
Do snow.rwn
Do snow.rwnDo snow.rwn
Do snow.rwnARUN DN
 
RDataMining slides-r-programming
RDataMining slides-r-programmingRDataMining slides-r-programming
RDataMining slides-r-programmingYanchang Zhao
 
python beginner talk slide
python beginner talk slidepython beginner talk slide
python beginner talk slidejonycse
 
Clojure for Data Science
Clojure for Data ScienceClojure for Data Science
Clojure for Data ScienceMike Anderson
 
Brief intro to clojure
Brief intro to clojureBrief intro to clojure
Brief intro to clojureRoy Rutto
 
Modern technologies in data science
Modern technologies in data science Modern technologies in data science
Modern technologies in data science Chucheng Hsieh
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with ClojureDmitry Buzdin
 
Clojure Intro
Clojure IntroClojure Intro
Clojure Introthnetos
 
Thinking Functionally In Ruby
Thinking Functionally In RubyThinking Functionally In Ruby
Thinking Functionally In RubyRoss Lawley
 
Introduction to R
Introduction to RIntroduction to R
Introduction to Ragnonchik
 
R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environmentYogendra Chaubey
 
ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015Michiel Borkent
 
Reproducible Computational Research in R
Reproducible Computational Research in RReproducible Computational Research in R
Reproducible Computational Research in RSamuel Bosch
 

Similar to A gentle introduction to functional programming through music and clojure (20)

The Curious Clojurist - Neal Ford (Thoughtworks)
The Curious Clojurist - Neal Ford (Thoughtworks)The Curious Clojurist - Neal Ford (Thoughtworks)
The Curious Clojurist - Neal Ford (Thoughtworks)
 
Do snow.rwn
Do snow.rwnDo snow.rwn
Do snow.rwn
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
 
RDataMining slides-r-programming
RDataMining slides-r-programmingRDataMining slides-r-programming
RDataMining slides-r-programming
 
Clojure basics
Clojure basicsClojure basics
Clojure basics
 
python beginner talk slide
python beginner talk slidepython beginner talk slide
python beginner talk slide
 
Clojure for Data Science
Clojure for Data ScienceClojure for Data Science
Clojure for Data Science
 
Brief intro to clojure
Brief intro to clojureBrief intro to clojure
Brief intro to clojure
 
Modern technologies in data science
Modern technologies in data science Modern technologies in data science
Modern technologies in data science
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
R basics
R basicsR basics
R basics
 
R programming language
R programming languageR programming language
R programming language
 
Clojure Intro
Clojure IntroClojure Intro
Clojure Intro
 
Thinking Functionally In Ruby
Thinking Functionally In RubyThinking Functionally In Ruby
Thinking Functionally In Ruby
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
Full Stack Clojure
Full Stack ClojureFull Stack Clojure
Full Stack Clojure
 
R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environment
 
ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015
 
Reproducible Computational Research in R
Reproducible Computational Research in RReproducible Computational Research in R
Reproducible Computational Research in R
 
Enter The Matrix
Enter The MatrixEnter The Matrix
Enter The Matrix
 

More from Paul Lam

Mozambique, Smallholder Farming, and Technology
Mozambique, Smallholder Farming, and TechnologyMozambique, Smallholder Farming, and Technology
Mozambique, Smallholder Farming, and TechnologyPaul Lam
 
When a machine learning researcher and a software engineer walk into a bar
When a machine learning researcher and a software engineer walk into a barWhen a machine learning researcher and a software engineer walk into a bar
When a machine learning researcher and a software engineer walk into a barPaul Lam
 
Evolution of Our Software Architecture
Evolution of Our Software ArchitectureEvolution of Our Software Architecture
Evolution of Our Software ArchitecturePaul Lam
 
Yet another startup built on Clojure(Script)
Yet another startup built on Clojure(Script)Yet another startup built on Clojure(Script)
Yet another startup built on Clojure(Script)Paul Lam
 
Clojure in US vs Europe
Clojure in US vs EuropeClojure in US vs Europe
Clojure in US vs EuropePaul Lam
 
2014 docker boston fig for developing microservices
2014 docker boston   fig for developing microservices2014 docker boston   fig for developing microservices
2014 docker boston fig for developing microservicesPaul Lam
 
Customer Behaviour Analytics: Billions of Events to one Customer-Product Prop...
Customer Behaviour Analytics: Billions of Events to one Customer-Product Prop...Customer Behaviour Analytics: Billions of Events to one Customer-Product Prop...
Customer Behaviour Analytics: Billions of Events to one Customer-Product Prop...Paul Lam
 
Composing re-useable ETL on Hadoop
Composing re-useable ETL on HadoopComposing re-useable ETL on Hadoop
Composing re-useable ETL on HadoopPaul Lam
 
An agile approach to knowledge discovery on web log data
An agile approach to knowledge discovery on web log dataAn agile approach to knowledge discovery on web log data
An agile approach to knowledge discovery on web log dataPaul Lam
 

More from Paul Lam (9)

Mozambique, Smallholder Farming, and Technology
Mozambique, Smallholder Farming, and TechnologyMozambique, Smallholder Farming, and Technology
Mozambique, Smallholder Farming, and Technology
 
When a machine learning researcher and a software engineer walk into a bar
When a machine learning researcher and a software engineer walk into a barWhen a machine learning researcher and a software engineer walk into a bar
When a machine learning researcher and a software engineer walk into a bar
 
Evolution of Our Software Architecture
Evolution of Our Software ArchitectureEvolution of Our Software Architecture
Evolution of Our Software Architecture
 
Yet another startup built on Clojure(Script)
Yet another startup built on Clojure(Script)Yet another startup built on Clojure(Script)
Yet another startup built on Clojure(Script)
 
Clojure in US vs Europe
Clojure in US vs EuropeClojure in US vs Europe
Clojure in US vs Europe
 
2014 docker boston fig for developing microservices
2014 docker boston   fig for developing microservices2014 docker boston   fig for developing microservices
2014 docker boston fig for developing microservices
 
Customer Behaviour Analytics: Billions of Events to one Customer-Product Prop...
Customer Behaviour Analytics: Billions of Events to one Customer-Product Prop...Customer Behaviour Analytics: Billions of Events to one Customer-Product Prop...
Customer Behaviour Analytics: Billions of Events to one Customer-Product Prop...
 
Composing re-useable ETL on Hadoop
Composing re-useable ETL on HadoopComposing re-useable ETL on Hadoop
Composing re-useable ETL on Hadoop
 
An agile approach to knowledge discovery on web log data
An agile approach to knowledge discovery on web log dataAn agile approach to knowledge discovery on web log data
An agile approach to knowledge discovery on web log data
 

Recently uploaded

Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 

Recently uploaded (20)

Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 

A gentle introduction to functional programming through music and clojure

  • 1. A gentle introduction to functional programming through music and Clojure Mødegruppe for F#unktionelle Københavnere, 24 November, 2015 Presented by Paul Lam
  • 2. Agenda 1. What is functional programming 2. Setting up the environment 3. Making some noise 4. Making some music 5. Transforming data to music 6. Why functional programming
  • 3. Wiki: Functional Programming “In computer science, functional programming is a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data.”
  • 5. Clojure is a dynamic programming language that targets the Java Virtual Machine, Common Language Runtime, and JavaScript
  • 6. Data Literals Long 42 BigInteger 1234567891234567 8912 Double 1.234 BigDecimal 1.234M Ratio 22/7 String “fred” Character a b c Keyword :a, :foo Symbol fred, ethel Boolean true, false nil nil Regex #”a*b”
  • 7. Collection Types ● Lists - singly linked, grow at front ○ (list 1 2 3), ‘(1 2 3), ‘(:fred "ethel" 27) ● Vectors - indexed access, grow at end ○ [1 2 :a :b], ["fred" :ethel 3/2] ● Maps - key/value associations ○ {:a 1, :b 2, :c 3}, {1 "ethel" 2 "fred"} ● Sets - collection with uniqueness constraint ○ #{:fred :ethel :lucy} ; duplicate key will error ● Heterogeneous ● Everything Nests Arbitrarily! ○ [#{:a :b} "c" [:d] {1 :e 2 [:f :g]}]
  • 8. Installing Leiningen Go to http://leiningen.org/ and follow 4 steps (Note: You must have Java already installed) 1. Download the lein script (or on Windows lein.bat) 2. Place it on your $PATH where your shell can find it (eg. ~/bin) 3. Set it to be executable (chmod a+x ~/bin/lein) 4. Run it (lein) and it will download the self-install package Then run: $ lein repl // Start a Clojure REPL
  • 9. Clojure Doc ● http://clojure.org/cheatsheet ● http://clojuredocs.org/ ● > (doc …)
  • 10. Working with lists > (+ 1 2 3) 6 > (first [1 2 3]) 1 > (rest [1 2 3]) (2 3) > (cons “x” [1 2 3]) (“x” 1 2 3) > (take 2 [ 1 2 3 4 5]) (1 2) > (drop 2 [1 2 3 4 5]) (3 4 5) > (range 10) (0 1 2 3 4 5 6 7 8 9) > (filter odd? (range 10)) (1 3 5 7 9) > (map odd? (range 10)) (false true false true false true false true false true) > (reduce + (range 10)) 45
  • 11. What is Overtone? Overtone: “Collaborative Programmable Music” http://overtone.github.io/ Interface SuperCollider (an audio synthesis engine) using Clojure http://supercollider.sourceforge.net/
  • 13. Working with Lists > (take 9 (cycle [1 2 3 4])) (1 2 3 4 1 2 3 4 1) > (interleave [:a :b :c :d :e] [1 2 3 4 5]) (:a 1 :b 2 :c 3 :d 4 :e 5) > (partition 3 [1 2 3 4 5 6 7 8 9]) ((1 2 3) (4 5 6) (7 8 9)) > (map vector [:a :b :c :d :e] [1 2 3 4 5]) ([:a 1] [:b 2] [:c 3] [:d 4] [:e 5]) > (interpose | "asdf") (a | s | d | f) > (apply str (interpose | "asdf")) "a|s|d|f"
  • 14. Working with Maps and Sets (def m {:a 1 :b 2 :c 3}) ● (m :b) => 2 ● (:b m) => 2 ● (keys m) => (:a :b :c) ● (assoc m :d 4 :c 42) => {:d 4, :a 1, :b 2, :c 42} ● (merge-with + m {:a 2 :b 3}) => {:a 3, :b 5, :c 3} ● (union #{:a :b :c} #{:c :d :e}) => #{:d :a :b :c :e} ● (join #{{:a 1 :b 2 :c 3} {:a 1 :b 21 :c 42}} #{{:a 1 :b 2 :e 5} {:a 1 :b 21 :d 4}}) => #{{:d 4, :a 1, :b 21, :c 42} {:a 1, :b 2, :c 3, :e 5}}
  • 15. Java Interop > (.toUpperCase "fred") "FRED" > (.getName String) "java.lang.String" > (System/getProperty "user.dir") "/Users/me/clojure/interop" > Math/PI 3.141592653589793
  • 16. Java Interop > (map #(.getName %) (.getMethods java.util.Date)) ("equals" "toString" "hashCode" "clone" "compareTo" "compareTo" "parse" . . . <and many more>) > (java.util.Date.) #inst "2015-01-26T21:49:01.403-00:00" > (doto (java.util.Date.) (.setSeconds 33) (.setMinutes 22)) #inst "2015-01-26T21:22:33.785-00:00"
  • 18. Drawbacks of FP ● requires thinking differently ● real-world programs are full of side-effects ● difficulty predicting performance profile
  • 19. Benefits of Functional Programming ● Easier to reason about ● Composibility ● Separation of concern ● Huges, “Why Functional Programming Matters”, 1990 ● http://weblog.raganwald.com/2007/03/why-why-functional-programming-matters.html