Top Banner
There and Back Again: a developers travels in the land of Clojure Mike Harris
114

There and Back Again

Jul 21, 2015

Download

Technology

Mike Harris
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: There and Back Again

There and Back Again: a developers travels in the land of Clojure

Mike Harris

Page 2: There and Back Again

Travel Log• Equipment

• Higher Order Functions

• Variadic Functions with Splat / Apply

• Destructuring

• Map Structure

Page 3: There and Back Again
Page 4: There and Back Again
Page 5: There and Back Again

— J.R.R. Tolkien, The Lord of the Rings

“It's a dangerous business, Frodo, going out your door. You step onto the road, and if you

don't keep your feet, there's no knowing where you might be swept off to.”

Page 6: There and Back Again
Page 7: There and Back Again

Travel Log• Equipment

• Higher Order Functions

• Variadic Functions with Splat / Apply

• Destructuring

• Map Structure

Page 8: There and Back Again

ECMAScript 6

• Nodev0.12.2

• Babel5.1.13

Page 9: There and Back Again

lodash

• Nodev0.12.2

• lodash3.7.0

Page 10: There and Back Again

Clojure

• Clojure1.6.0

Page 11: There and Back Again

JS

Page 12: There and Back Again

var should = require('should'); var companySize = function() {return 15;}; companySize().should.equal(15);

JS

Page 13: There and Back Again

import * as should from 'should' let companySize = () => 15;companySize().should.equal(15);

ES 6

Page 14: There and Back Again

(() => 15)().should.equal(15);

ES 6

Page 15: There and Back Again
Page 16: There and Back Again

(ns presentation.example (require [clojure.test :refer :all])) (is (= 4 (+ 2 2)))

Page 17: There and Back Again

(defn companySize [] 15) (is (= 15 (companySize)))

Page 18: There and Back Again

(is (= 15 ((fn [] 15))))

Page 19: There and Back Again

Travel Log• Equipment

• Higher Order Functions

• Variadic Functions with Splat / Apply

• Destructuring

• Map Structure

Page 20: There and Back Again

— Alan J. Perlis, Epigrams on Programming, #5

“If a program manipulates a large amount of data, it does so in a small number of ways.”

Page 21: There and Back Again
Page 22: There and Back Again

function A

Produces

function B

Page 23: There and Back Again

JS

Page 24: There and Back Again

var a = function() { return function b() { return 'Bilbo'; };};a()().should.equal(‘Bilbo');

JS

Page 25: There and Back Again

let a = () => () => ‘Bilbo';a()().should.equal(‘Bilbo');

ES 6

Page 26: There and Back Again
Page 27: There and Back Again

(defn a [] (fn b [] "Bilbo"))(is (= "Bilbo" ((a))))

Page 28: There and Back Again

function A

Consum

es

function B

Page 29: There and Back Again

JS

Page 30: There and Back Again

var a = function(b) { return b();};a(function b() { return 'Bilbo'; }).should.equal(‘Bilbo');

JS

Page 31: There and Back Again

let a = (b) => b();a(() => 'Bilbo') .should.equal('Bilbo')

ES 6

Page 32: There and Back Again
Page 33: There and Back Again

(defn a [b] (b))(is (= "Bilbo" (a (fn b [] "Bilbo"))))

Page 34: There and Back Again

dwarfdwarfdwarf name gender

Plucked

name

name

Page 35: There and Back Again

JS

Page 36: There and Back Again

let dwarves = [{name: "Ori", gender: "Male"}, {name: "Dori", gender: "Male"}, {name: "Nori", gender: “Male”}];_.pluck(dwarves, 'name') .should.eql(['Ori', 'Dori', 'Nori'])

ES 6

Page 37: There and Back Again
Page 38: There and Back Again

(def dwarves [{:name "Ori" :gender “Male"} {:name "Dori" :gender "Male"} {:name "Nori" :gender “Male"}])

(is (= ["Ori" "Dori" "Nori"] (map :name dwarves)))

Page 39: There and Back Again

1 2

Reduce

3 4

10

+ 0

Page 40: There and Back Again

JS

Page 41: There and Back Again

_.reduce( [1, 2, 3, 4], (m, x) => m + x, 0) .should.equal(10)

ES 6

Page 42: There and Back Again

_.reduce( [1, 2, 3, 4], (m, x) => m + x) .should.equal(10)

ES 6

Page 43: There and Back Again

_.reduce( [1, 2, 3, 4], (m, x) => m + x, 100) .should.equal(110)

ES 6

Page 44: There and Back Again
Page 45: There and Back Again

(is (= 10 (reduce + 0 [1 2 3 4])))

Page 46: There and Back Again

(is (= 10 (reduce + [1 2 3 4])))

Page 47: There and Back Again

(is (= 110 (reduce + 100 [1 2 3 4])))

Page 48: There and Back Again

Travel Log• Equipment

• Higher Order Functions

• Variadic Functions with Splat / Apply

• Destructuring

• Map Structure

Page 49: There and Back Again

— J.R.R. Tolkien, The Hobbit

“‘Go back?’ he thought. ‘No good at all! Go sideways? Impossible! Go forward? Only thing

to do! On we go!’”

Page 50: There and Back Again
Page 51: There and Back Again

summer values …

Splat

summer 1 2 3 4 5 6

Page 52: There and Back Again

JS

Page 53: There and Back Again

function summer() { return _.reduce( Array.prototype.slice.call( arguments, summer.length), function(m, x) { return m + x; }, 0);};summer( 1, 2, 3, 4, 5, 6).should.equal(21);

Page 54: There and Back Again

summer(1, 2).should.equal(3); summer().should.equal(0); summer(8).should.equal(8);

Page 55: There and Back Again

let summer = (...values) => _.reduce( values, (m, x) => m + x, 0); summer( 1, 2, 3, 4, 5, 6).should.equal(21);

ES 6

Page 56: There and Back Again

summer(1, 2).should.equal(3); summer().should.equal(0); summer(8).should.equal(8);

ES 6

Page 57: There and Back Again
Page 58: There and Back Again

(defn summer [& values] (apply + values))(is (= 21 (summer 1 2 3 4 5 6)))

Page 59: There and Back Again

(is (= 3 (summer 1 2))) (is (= 0 (summer)))(is (= 8 (summer 8)))

Page 60: There and Back Again

(is (= 21 (+ 1 2 3 4 5 6)))

(is (= 3 (+ 1 2)))

summer is built into Clojure as +

Page 61: There and Back Again

(is (= 0 (+)))(is (= 8 (+ 8)))

summer is built into Clojure as +

Page 62: There and Back Again

Travel Log• Equipment

• Higher Order Functions

• Variadic Functions with Splat / Apply

• Destructuring

• Map Structure

Page 63: There and Back Again

— J.R.R. Tolkien, The Hobbit

“Escaping goblins to be caught by wolves!”

Page 64: There and Back Again
Page 65: There and Back Again

hobbit name age

Destructured

name family

family pasttime

Page 66: There and Back Again

JS

Page 67: There and Back Again

let [a, ,c] = [1, 2, 3, 4, 5]; a.should.equal(1);c.should.equal(3);

ES 6

Page 68: There and Back Again

let [a=0, ,c, ,e=99] = [1, 2]; a.should.equal(1);should(c).be.equal(undefined);e.should.equal(99);

ES 6

Page 69: There and Back Again

let bilbo = { name: ‘Bilbo', age: 50, family: ['Baggins', 'Took'], pastTime: ['writing']}; let {name, family:parents} = bilbo;

ES 6

Page 70: There and Back Again

name.should.equal(‘Bilbo');parents.should.eql( ['Baggins', ‘Took']);

ES 6

Page 71: There and Back Again

let hobbies = ({name, pastTime:hobbies}) => name + ' enjoys ' + hobbies.join(‘,');

ES 6

Page 72: There and Back Again

hobbies(bilbo).should .equal('Bilbo enjoys writing’); hobbies( {name: ‘Gollum', something: ‘else’ pastTime:[‘precious!']}) .should.equal( 'Gollum enjoys precious!');

ES 6

Page 73: There and Back Again
Page 74: There and Back Again

(def values [1 2 3 4 5]) (let [[a _ c] values] (is (= a 1)) (is (= c 3)))

Page 75: There and Back Again

(def values [1 2 3 4 5])(let [[a _ & xs] values] (is (= a 1)) (is (= [3 4 5] xs)))

Page 76: There and Back Again

(def bilbo {:name "Bilbo" :age 50 :family ["Baggins" "Took"] :past-time ["writing"]})

Page 77: There and Back Again

(let [{name :name parents :family} bilbo] (is (= "Bilbo" name)) (is (= ["Baggins" "Took"] parents))

Page 78: There and Back Again

(defn hobbies [{ name :name hobbies :past-time}] (str name " enjoys " (clojure.string/join ", " hobbies)))

Page 79: There and Back Again

(is (= "Bilbo enjoys writing" (hobbies bilbo)))(is (= "Gollum enjoys precious!" (hobbies {:name “Gollum" :something “else” :past-time ["precious!"]})))

Page 80: There and Back Again

Travel Log• Equipment

• Higher Order Functions

• Variadic Functions with Splat / Apply

• Destructuring

• Map Structure

Page 81: There and Back Again

— Alan J. Perlis, Epigrams on Programming, #9

“It is better to have 100 functions operate on one data structure than 10 functions on 10 data

structures.”

Page 82: There and Back Again
Page 83: There and Back Again

hobbit name age

Made up of

family pasttime

name age family pasttime

Page 84: There and Back Again

JS

Page 85: There and Back Again

var bilbo = { name: 'Bilbo', age: 50, family: ['Baggins', ‘Took'], pastTime: ['writing']};

Page 86: There and Back Again

_.pick(bilbo, 'name') .should.eql({name: ‘Bilbo'});_.pick(bilbo, ['name', 'family']) .should.eql( {name: 'Bilbo', family: ['Baggins', ‘Took']}); _.pick(bilbo, ‘none').should.eql({});

Page 87: There and Back Again

_.get(bilbo, 'name') .should.equal(‘Bilbo');_.get(bilbo, 'none', 'not found') .should.equal('not found’);should(_.get(bilbo, 'none')) .equal(undefined);

Page 88: There and Back Again
Page 89: There and Back Again

(def bilbo {:name "Bilbo" :age 50 :family ["Baggins" "Took"] :past-time ["writing"]})

Page 90: There and Back Again

(is (= "Bilbo" (:name bilbo))) (is (= "Bilbo" (bilbo :name))) (is (= "Bilbo" (get bilbo :name)))

Page 91: There and Back Again

(is (= nil (get bilbo :none))) (is (= :not-found (get bilbo :none :not-found))) (is (= nil (bilbo :none))) (is (= nil (:none bilbo)))

Page 92: There and Back Again

(is (= {:name "Bilbo"} (select-keys bilbo [:name])))

Page 93: There and Back Again

(is (= {:name "Bilbo" :family ["Baggins" "Took"]} (select-keys bilbo [:name :family])))

Page 94: There and Back Again

(is (= {} (select-keys bilbo [:none]))) (is (= {:name "Bilbo"} (select-keys bilbo [:name :none])))

Page 95: There and Back Again

A B

Com

bined

A B

A C

C

Page 96: There and Back Again

JS

Page 97: There and Back Again

_.assign( {a: 1, b: 1}, {a: 2, c: 2}) .should.eql({a: 2, b: 1, c: 2});

Page 98: There and Back Again

_.merge( {a: 1, b: 1}, {a: 2, c: 2}) .should.eql({a: 2, b: 1, c: 2});

Page 99: There and Back Again

_.assign( {a: {x: 1}, b: 1}, {a: {y: 2}, c: 2}) .should.eql( {a: {y: 2}, b: 1, c: 2});

Page 100: There and Back Again

_.merge( {a: {x: 1}, b: 1}, {a: {y: 2}, c: 2}) .should.eql( {a: {x: 1, y: 2}, b: 1, c: 2});

Page 101: There and Back Again
Page 102: There and Back Again

(is (= {:a 2 :b 1 :c 2} (merge {:a 1 :b 1} {:a 2 :c 2})))

Page 103: There and Back Again

(is (= {:a {:y 2} :b 1 :c 2} (merge {:a {:x 1} :b 1} {:a {:y 2} :c 2})))

Page 104: There and Back Again

(is (= {:a {:x 1 :y 2} :b 1 :c 2} (merge-with conj {:a {:x 1} :b 1} {:a {:y 2} :c 2})))

Page 105: There and Back Again

— J.R.R. Tolkien, The Hobbit

“As all things come to an end, even this story.”

Page 106: There and Back Again
Page 107: There and Back Again

Travel Log• Equipment

• Higher Order Functions

• Variadic Functions with Splat / Apply

• Destructuring

• Map Structure

Page 108: There and Back Again

Thank you!

Mike Harris@MikeMKHhttp://comp-phil.blogspot.com/

Page 109: There and Back Again

— J.R.R. Tolkien, The Hobbit

“May the wind under your wings bear you where the sun sails and the moon walks.”

Page 110: There and Back Again

Bibliography(conference session)

• “Simple Made Easy” by Rich Hickey at Strange Loop 2011 http://www.infoq.com/presentations/Simple-Made-Easy

• “Coder Decoder: Functional Programming Lingo Explained, with Pictures” by Katie Miller at Strange Loop 2014 https://www.youtube.com/watch?v=uwrCQmpZ8Ts

• “Pattern Matching in Clojure” by Sean Johnson at Clojure/West 2015 https://www.youtube.com/watch?v=n7aE6k8o_BU

Page 111: There and Back Again

Bibliography(egghead.io videos)

• “Asynchronous Programming: The End of Loop” by Jafar Husain https://egghead.io/series/mastering-asynchronous-programming-the-end-of-the-loop

• “Introduction to Lodash” by John Lindquist https://egghead.io/lessons/core-javascript-introduction-to-lodash

• “Lodash: Refactoring Simple For Loops” by John Lindquist https://egghead.io/lessons/javascript-lodash-refactoring-simple-for-loops

• “ECMAscript6” by John Lindquist https://egghead.io/technologies/es6

Page 112: There and Back Again

Bibliography(Clojure books)

• “The Joy of Clojure” by Michael Fogus and Chris Houser

• “Programming Clojure” by Stuart Halloway and Aaron Bedra

• “Clojure Programming” by Chas Emerick, Brian Carper, and Christophe Grand

Page 113: There and Back Again

Bibliography(Functional JavaScript books)

• “Functional JavaScript: Introducing Functional Programming with Underscore.js” by Michael Fogus

• “JavaScript Allongé” by Reginald Braithwaite https://leanpub.com/javascript-allonge

Page 114: There and Back Again

Image Sources• The Hobbit book covers from http://commons.wikimedia.org/wiki/File:HMCoSecondEdHobbits.jpg

• Socrates take by Eric Gaba from http://en.wikipedia.org/wiki/File:Socrates_Louvre.jpg

• The Divided Line created by Neal Burton from http://outre-monde.com/2010/09/25/platonic-myths-the-sun-line-and-cave/

• The Hobbit images* are by Mikhail Belomlinsky from http://viola.bz/the-first-illustrator-of-the-hobbit/

• Babel from https://github.com/babel/babel

• Lodash from https://github.com/lodash/lodash

• Clojure from http://clojure.org/

• The Hobbit image (wolves)* by Tove Jansson from http://www.tolkien.com.pl/hobbit/collection/hobbit-swedish-1962.php

• Me taken by Kelsey Harris at Strange Loop 2014