A gentle introduction to functional programming through music and clojure

20
A gentle introduction to functional programming through music and Clojure Mødegruppe for F#unktionelle Københavnere, 24 November, 2015 Presented by Paul Lam

Transcript of A gentle introduction to functional programming through music and clojure

Page 1: A gentle introduction to functional programming through music and clojure

A gentle introduction to functional programming through

music and ClojureMødegruppe for F#unktionelle Københavnere, 24 November, 2015

Presented by Paul Lam

Page 2: A gentle introduction to functional programming through music and clojure

Agenda1. What is functional programming2. Setting up the environment3. Making some noise4. Making some music5. Transforming data to music6. Why functional programming

Page 3: A gentle introduction to functional programming through music and clojure

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.”

Page 4: A gentle introduction to functional programming through music and clojure

Hello World

Page 5: A gentle introduction to functional programming through music and clojure

Clojure is a dynamic programming language that targets the Java Virtual Machine, Common Language Runtime, and JavaScript

Page 6: A gentle introduction to functional programming through music and clojure

Data LiteralsLong 42

BigInteger 12345678912345678912

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”

Page 7: A gentle introduction to functional programming through music and clojure

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]}]

Page 8: A gentle introduction to functional programming through music and clojure

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

Page 9: A gentle introduction to functional programming through music and clojure

Clojure Doc

● http://clojure.org/cheatsheet ● http://clojuredocs.org/● > (doc …)

Page 10: A gentle introduction to functional programming through music and clojure

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

Page 11: A gentle introduction to functional programming through music and clojure

What is Overtone?

Overtone: “Collaborative Programmable Music”http://overtone.github.io/

Interface SuperCollider (an audio synthesis engine) using Clojurehttp://supercollider.sourceforge.net/

Page 12: A gentle introduction to functional programming through music and clojure

Workshoprepo -- https://github.com/Quantisan/functional-music

Page 13: A gentle introduction to functional programming through music and clojure

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"

Page 14: A gentle introduction to functional programming through music and clojure

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}}

Page 15: A gentle introduction to functional programming through music and clojure

Java Interop> (.toUpperCase "fred")"FRED"

> (.getName String)"java.lang.String"

> (System/getProperty "user.dir")"/Users/me/clojure/interop"

> Math/PI3.141592653589793

Page 16: A gentle introduction to functional programming through music and clojure

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"

Page 17: A gentle introduction to functional programming through music and clojure

https://github.com/Quantisan/functional-music/blob/solution/src/functional_music/tab.

clj

Solution

Page 18: A gentle introduction to functional programming through music and clojure

Drawbacks of FP

● requires thinking differently

● real-world programs are full of side-effects● difficulty predicting performance profile

Page 19: A gentle introduction to functional programming through music and clojure

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

Page 20: A gentle introduction to functional programming through music and clojure

Contact

Paul Lam@[email protected]