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
Pune Clojure Course Outline
PDF
Brief intro to clojure
PDF
Clojure values
KEY
Clojure Intro
KEY
(map Clojure everyday-tasks)
ODP
Getting started with Clojure
PDF
Clojure: Functional Concurrency for the JVM (presented at OSCON)
PDF
Clojure
PDF
Continuation Passing Style and Macros in Clojure - Jan 2012
PDF
Clojure intro
PDF
Clojure made-simple - John Stevenson
ODP
Clojure: Practical functional approach on JVM
PDF
Clojure Interoperability
PPTX
Clojure 7-Languages
PDF
Introduction to clojure
ZIP
Clojure: Functional Concurrency for the JVM (presented at Open Source Bridge)
PDF
Predictably
ODP
Clojure made simple - Lightning talk
PDF
PDF
Clojure - A new Lisp
Pune Clojure Course Outline
Brief intro to clojure
Clojure values
Clojure Intro
(map Clojure everyday-tasks)
Getting started with Clojure
Clojure: Functional Concurrency for the JVM (presented at OSCON)
Clojure
Continuation Passing Style and Macros in Clojure - Jan 2012
Clojure intro
Clojure made-simple - John Stevenson
Clojure: Practical functional approach on JVM
Clojure Interoperability
Clojure 7-Languages
Introduction to clojure
Clojure: Functional Concurrency for the JVM (presented at Open Source Bridge)
Predictably
Clojure made simple - Lightning talk
Clojure - A new Lisp
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)

PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
medical staffing services at VALiNTRY
PPTX
Introduction to Artificial Intelligence
PPTX
AIRLINE PRICE API | FLIGHT API COST |
PPTX
Transform Your Business with a Software ERP System
PPTX
ai tools demonstartion for schools and inter college
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
L1 - Introduction to python Backend.pptx
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
FLIGHT TICKET RESERVATION SYSTEM | FLIGHT BOOKING ENGINE API
ManageIQ - Sprint 268 Review - Slide Deck
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Upgrade and Innovation Strategies for SAP ERP Customers
medical staffing services at VALiNTRY
Introduction to Artificial Intelligence
AIRLINE PRICE API | FLIGHT API COST |
Transform Your Business with a Software ERP System
ai tools demonstartion for schools and inter college
Which alternative to Crystal Reports is best for small or large businesses.pdf
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
How to Migrate SBCGlobal Email to Yahoo Easily
L1 - Introduction to python Backend.pptx
VVF-Customer-Presentation2025-Ver1.9.pptx
Softaken Excel to vCard Converter Software.pdf
PTS Company Brochure 2025 (1).pdf.......
FLIGHT TICKET RESERVATION SYSTEM | FLIGHT BOOKING ENGINE API

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