Bodil Stokke

@bodil

Functional programming

  1. Functions are values
  2. Functions can take other functions as arguments
  3. Functions can return other functions
MONADS
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;
    }
}

Haskell

Pure Functions

  1. Referential transparency
  2. No side effects
### Memoisation ```haskell fib 0 = 0 fib 1 = 1 fib n = fib (n - 1) + fib (n - 2) ```
### Memoisation ```clojure (defn fib [n] (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2))))) ```

Now Hold On

  1. No side effects means you can't change your state
  2. State that doesn't change is not state
...then how do you get anything done?
MONADS

Type System

  1. Powerful compile time static analysis
  2. Extensible types
  3. The core of your program
  4. Monads enabled

Immutable Data Structures

  1. No unexpected side effects guaranteed
  2. The easy way to thread safety
### Look ma, no mutation ```clojure (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 ```clojure (cons 5 nil) (cons 4 (cons 5 nil)) (cons 3 (cons 4 (cons 5 nil))) ```

Clojure

The New Lisp

  1. Homoiconic like a boss
  2. Values, not objects
  3. Polymorphism without inheritance
  4. Concurrent by design
  5. And yet, the JVM

Homoiconic

### Code and data are one ```clojure ;; 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! ```clojure (use 'hiccup.core) (html [:body [:h1#title "Hello"] [:p.article "Hello world!"]]) ```
### DSLs like a boss! ```clojure (use 'korma.core) (defentity person) (dry-run (select person (where {:name "Bodil" :age [> 18]}) (order :name :ASC))) ```
### Values, not objects ```clojure ; 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? ```clojure (def ponies-as-string (str ponies)) ((read-string ponies-as-string) 2) ```
MULTI-CORE LIKE A BOSS
### State gonna state ```clojure (def counter-state (atom 0)) (defn count-up [] (swap! counter-state inc) @counter-state) ```
### Threads so simple you could cry ```clojure (defn sum-to [n] (reduce + (range n))) ; (sum-to 250000000) ```
Yeah, I'm not sure...
Which language is best in life?
Seriously.

Tribalism is dumb

  1. Haskell doesn't let you write bugs
  2. Erlang runs forever
  3. Clojure is infinitely extensible
  4. Java is a mess but very, very fast
  5. Go is a clever systems language
  6. Even Scala is probably good for something

It depends.

Be the traveller,

not the tribesman.

There is no such thing as a perfect language.

kthxbye.

http://bodil.org/more-than-functions