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

A gentle introduction to functional programming through music and clojure

  • 1.
    A gentle introductionto functional programming through music and Clojure Mødegruppe for F#unktionelle Københavnere, 24 November, 2015 Presented by Paul Lam
  • 2.
    Agenda 1. What isfunctional 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 “Incomputer 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.”
  • 4.
  • 5.
    Clojure is adynamic programming language that targets the Java Virtual Machine, Common Language Runtime, and JavaScript
  • 6.
    Data Literals Long 42 BigInteger1234567891234567 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 tohttp://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/
  • 12.
  • 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 Mapsand 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"
  • 17.
  • 18.
    Drawbacks of FP ●requires thinking differently ● real-world programs are full of side-effects ● difficulty predicting performance profile
  • 19.
    Benefits of FunctionalProgramming ● 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
  • 20.