SlideShare a Scribd company logo
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

PDF
From Javascript To Haskell
PDF
Om (Cont.)
PPT
Cpp tutorial
PDF
Metaprogramming
PDF
Merge sort
PDF
Short intro to the Rust language
DOCX
A Shiny Example-- R
PDF
Programming Language: Ruby
From Javascript To Haskell
Om (Cont.)
Cpp tutorial
Metaprogramming
Merge sort
Short intro to the Rust language
A Shiny Example-- R
Programming Language: Ruby

What's hot (20)

PDF
Christian Gill ''Functional programming for the people''
PDF
Linked list int_data_fdata
PDF
Ooprc4 b
DOCX
Data Visualization with R.ggplot2 and its extensions examples.
PPTX
PPTX
Array matrix example programs - C language
PPTX
CrystalBall - Compute Relative Frequency in Hadoop
PDF
Fragmentation
DOCX
ggtimeseries-->ggplot2 extensions
PDF
Py lecture5 python plots
PDF
Coding with Vim
PPT
Module 2 topic 2 notes
PDF
Parallel binary search
DOCX
R-ggplot2 package Examples
PDF
20160616技術會議
PDF
Monads from Definition
PDF
PDF
Зависимые типы в GHC 8. Максим Талдыкин
Christian Gill ''Functional programming for the people''
Linked list int_data_fdata
Ooprc4 b
Data Visualization with R.ggplot2 and its extensions examples.
Array matrix example programs - C language
CrystalBall - Compute Relative Frequency in Hadoop
Fragmentation
ggtimeseries-->ggplot2 extensions
Py lecture5 python plots
Coding with Vim
Module 2 topic 2 notes
Parallel binary search
R-ggplot2 package Examples
20160616技術會議
Monads from Definition
Зависимые типы в GHC 8. Максим Талдыкин
Ad

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

PDF
The Curious Clojurist - Neal Ford (Thoughtworks)
PDF
Do snow.rwn
PDF
Pune Clojure Course Outline
PDF
RDataMining slides-r-programming
ODP
Clojure basics
PPTX
python beginner talk slide
PPTX
Clojure for Data Science
PDF
Brief intro to clojure
PPTX
Modern technologies in data science
PDF
Refactoring to Macros with Clojure
PDF
R basics
PPTX
R programming language
KEY
Clojure Intro
PDF
Thinking Functionally In Ruby
ODP
Introduction to R
PDF
Full Stack Clojure
PPT
R tutorial for a windows environment
PDF
ClojureScript loves React, DomCode May 26 2015
PPTX
Reproducible Computational Research in R
PPTX
lecture-Basic-programing-R-1-basic-eng.pptx
The Curious Clojurist - Neal Ford (Thoughtworks)
Do snow.rwn
Pune Clojure Course Outline
RDataMining slides-r-programming
Clojure basics
python beginner talk slide
Clojure for Data Science
Brief intro to clojure
Modern technologies in data science
Refactoring to Macros with Clojure
R basics
R programming language
Clojure Intro
Thinking Functionally In Ruby
Introduction to R
Full Stack Clojure
R tutorial for a windows environment
ClojureScript loves React, DomCode May 26 2015
Reproducible Computational Research in R
lecture-Basic-programing-R-1-basic-eng.pptx
Ad

More from Paul Lam (9)

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

Recently uploaded (20)

PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
AI in Product Development-omnex systems
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPT
Introduction Database Management System for Course Database
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Transform Your Business with a Software ERP System
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
top salesforce developer skills in 2025.pdf
PPTX
Online Work Permit System for Fast Permit Processing
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Introduction to Artificial Intelligence
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Design an Analysis of Algorithms I-SECS-1021-03
Navsoft: AI-Powered Business Solutions & Custom Software Development
Design an Analysis of Algorithms II-SECS-1021-03
Upgrade and Innovation Strategies for SAP ERP Customers
AI in Product Development-omnex systems
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Introduction Database Management System for Course Database
Softaken Excel to vCard Converter Software.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
PTS Company Brochure 2025 (1).pdf.......
How to Choose the Right IT Partner for Your Business in Malaysia
Wondershare Filmora 15 Crack With Activation Key [2025
Transform Your Business with a Software ERP System
Odoo Companies in India – Driving Business Transformation.pdf
top salesforce developer skills in 2025.pdf
Online Work Permit System for Fast Permit Processing
ManageIQ - Sprint 268 Review - Slide Deck
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Introduction to Artificial Intelligence
2025 Textile ERP Trends: SAP, Odoo & Oracle
Design an Analysis of Algorithms I-SECS-1021-03

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