Functional programming
Functions are values
Functions can take other functions as arguments
Functions can return other functions
A monad is a monoid in the category of endofunctors.
What's the problem?
Not Philip Wadler
class EvenJavaCanMap {
interface Fn<T> {
T apply(T value);
}
public static List<T> map(Fn<T> fn, List<T> list) {
List<T> newList = new LinkedList<T>();
for (T item : list)
newList.add(fn.apply(item));
return newList;
}
}
Pure Functions
Referential transparency
No side effects
Memoisation
fib 0 = 0
fib 1 = 1
fib n = fib (n - 1) + fib (n - 2)
Memoisation
(defn fib [n]
(if (< n 2) n
(+ (fib (- n 1))
(fib (- n 2)))))
Now Hold On
No side effects means you can't change your state
State that doesn't change is not state
...then how do you get anything done?
Type System
Powerful compile time static analysis
Extensible types
The core of your program
Monads enabled
Immutable Data Structures
No unexpected side effects guaranteed
The easy way to thread safety
Look ma, no mutation
(def primes [2 3 5 7 11 13])
(assoc primes 3 42)
Now wait just a minute...
Wouldn't that be really slow?
Building a linked list
(cons 5 nil)
(cons 4 (cons 5 nil))
(cons 3 (cons 4 (cons 5 nil)))
The New Lisp
Homoiconic like a boss
Values, not objects
Polymorphism without inheritance
Concurrent by design
And yet, the JVM
Code and data are one
;; This is a function call:
(max 4 5 6)
;; This is a list:
'(max 4 5 6)
Send men to summon macros
(defn invoke-func [name arg]
`(~(symbol name) ~arg))
DSLs like a boss!
(use 'hiccup.core)
(html [:body
[:h1#title "Hello"]
[:p.article "Hello world!"]])
DSLs like a boss!
(use 'korma.core)
(defentity person)
(dry-run
(select person
(where {:name "Bodil" :age [> 18]})
(order :name :ASC)))
Values, not objects
; Maps
(def pony-types
{"Pinkie Pie" "Earth pony",
"Rainbow Dash" "Pegasus pony",
"Rarity" "Unicorn pony"})
; Sequences
(def ponies
["Pinkie Pie" "Applejack" "Rainbow Dash"
"Rarity" "Fluttershy" "Twilight Sparkle"])
Serialisation, you say?
(def ponies-as-string (str ponies))
((read-string ponies-as-string) 2)
State gonna state
(def counter-state (atom 0))
(defn count-up []
(swap! counter-state inc)
@counter-state)
Threads so simple you could cry
(defn sum-to [n]
(reduce + (range n)))
; (sum-to 250000000)
Which language is best in life?
Tribalism is dumb
Haskell doesn't let you write bugs
Erlang runs forever
Clojure is infinitely extensible
Java is a mess but very, very fast
Go is a clever systems language
Even Scala is probably good for something
Be the traveller,
not the tribesman.
There is no such thing as a perfect language.