Slide 1

Slide 1 text

THE A LEVER FOR MIND @tomstuart at RubyConf on 2014-11-19

Slide 2

Slide 2 text

Give me a lever long enough and a fulcrum on which to place it, and I shall move the world.” — Archimedes “

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

ABSTRACTION

Slide 5

Slide 5 text

NUMBERS

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

3

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

4 3 2

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

<

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

3

Slide 29

Slide 29 text

2 3 5 combine add count count count REAL ABSTRACT

Slide 30

Slide 30 text

2 3 5 add ABSTRACT

Slide 31

Slide 31 text

SUMS

Slide 32

Slide 32 text

1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = ?

Slide 33

Slide 33 text

1 + 2 = 3 3 + 3 = 6 6 + 4 = 10 10 + 5 = 15

Slide 34

Slide 34 text

15 + 6 = 21 21 + 7 = 28 28 + 8 = 36 36 + 9 = 45 45 + 10 = 55

Slide 35

Slide 35 text

1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55

Slide 36

Slide 36 text

1 + 2 + 3 + 4 + 5 + … … + 98 + 99 + 100 = ?

Slide 37

Slide 37 text

? 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 =

Slide 38

Slide 38 text

? 1 + 10 + 2 + 9 + 3 + 8 + 4 + 7 + 5 + 6 =

Slide 39

Slide 39 text

11 + 11 + 11 + 11 + 11 = ?

Slide 40

Slide 40 text

11 + 11 + 11 + 11 + 11 = 55

Slide 41

Slide 41 text

1 + 10 + 2 + 9 + 3 + 8 + 4 + 7 + 5 + 6 = 55

Slide 42

Slide 42 text

1 + 2 + … + 9 + 10 = (10 ÷ 2) × (10 + 1)

Slide 43

Slide 43 text

1 + 2 + 3 + 4 + 5 + … … + 98 + 99 + 100 = ?

Slide 44

Slide 44 text

1 + 100 + 2 + 99 + 49 + 52 + 50 + 51 = ? ⋮

Slide 45

Slide 45 text

101 + 101 + 101 + 101 = ? ⋮

Slide 46

Slide 46 text

101 + 101 + 101 + 101 = 5,050 ⋮

Slide 47

Slide 47 text

1 + 2 + … + 99 + 100 = (100 ÷ 2) × (100 + 1)

Slide 48

Slide 48 text

1 + 2 + … + n = (n ÷ 2) × (n + 1)

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

10 11

Slide 52

Slide 52 text

(n + 1) × n ÷ 2 1 + 2 + … + n =

Slide 53

Slide 53 text

1 + 2 + … + n = (n + 1) × n ÷ 2 ( )

Slide 54

Slide 54 text

1 + 2 + … + 999 + 1000 = ?

Slide 55

Slide 55 text

1 + 2 + … + 999 + 1000 = (1000 ÷ 2) × (1000 + 1)

Slide 56

Slide 56 text

1 + 2 + … + 999 + 1000 = 500 × 1001

Slide 57

Slide 57 text

1 + 2 + … + 999 + 1000 = 500,500

Slide 58

Slide 58 text

GRAPHS

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

5 3 3 3

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

DEGREE 2

Slide 78

Slide 78 text

DEGREE 3

Slide 79

Slide 79 text

WALK

Slide 80

Slide 80 text

CONNECTED

Slide 81

Slide 81 text

CONNECTED

Slide 82

Slide 82 text

CONNECTED

Slide 83

Slide 83 text

CONNECTED

Slide 84

Slide 84 text

TRAIL

Slide 85

Slide 85 text

EULERIAN TRAIL

Slide 86

Slide 86 text

EULERIAN TRAIL CONNECTED OF ODD DEGREE AT MOST 2 NODES

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

TRIANGLES

Slide 89

Slide 89 text

4 3 5

Slide 90

Slide 90 text

4 4 5.7

Slide 91

Slide 91 text

4 5 6.4

Slide 92

Slide 92 text

3 5.8 5

Slide 93

Slide 93 text

a b c a² + b² = c²

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

a² b²

Slide 99

Slide 99 text

c² a² b²

Slide 100

Slide 100 text

c² a² b² + =

Slide 101

Slide 101 text

c² a² b²

Slide 102

Slide 102 text

c² a² b²

Slide 103

Slide 103 text

c² a² b²

Slide 104

Slide 104 text

a b c a² + b² = c²

Slide 105

Slide 105 text

MATHEMATICS

Slide 106

Slide 106 text

SPOT PATTERNS, BUILD REUSABLE ABSTRACTIONS

Slide 107

Slide 107 text

BAD MATHEMATICS EDUCATION

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

POKER

Slide 111

Slide 111 text

HIGH CARD ONE PAIR TWO PAIR

Slide 112

Slide 112 text

THREE OF A KIND STRAIGHT FLUSH

Slide 113

Slide 113 text

Card = Struct.new(:rank, :suit) three_of_diamonds = Card.new(3, :diamonds) queen_of_hearts = Card.new(12, :hearts) ace_of_spades = Card.new(14, :spades)

Slide 114

Slide 114 text

Hand = Struct.new(:cards) do include Comparable def <=>(other) # TODO end end

Slide 115

Slide 115 text

Hand = Struct.new(:cards) do include Comparable def <=>(other) highest_card_rank <=> other.highest_card_rank end # … end

Slide 116

Slide 116 text

Hand = Struct.new(:cards) do include Comparable def <=>(other) if highest_card_rank == other.highest_card_rank without { |c| c.rank == highest_card_rank } <=> other.without { |c| c.rank == highest_card_rank } else highest_card_rank <=> other.highest_card_rank end end # … end

Slide 117

Slide 117 text

Hand = Struct.new(:cards) do include Comparable def <=>(other) if empty? && other.empty? 0 elsif highest_card_rank == other.highest_card_rank without { |c| c.rank == highest_card_rank } <=> other.without { |c| c.rank == highest_card_rank } else highest_card_rank <=> other.highest_card_rank end end # … end

Slide 118

Slide 118 text

Hand = Struct.new(:cards) do include Comparable def <=>(other) if one_pair? if other.one_pair? if highest_pair_rank == other.highest_pair_rank without { |c| c.rank == highest_pair_rank } <=> other.without { |c| c.rank == highest_pair_rank } else highest_pair_rank <=> other.highest_pair_rank end else 1 end else if empty? && other.empty? 0 elsif other.one_pair? -1 elsif highest_card_rank == other.highest_card_rank without { |c| c.rank == highest_card_rank } <=> other.without { |c| c.rank == highest_card_rank } else highest_card_rank <=> other.highest_card_rank end end end # … end

Slide 119

Slide 119 text

Hand = Struct.new(:cards) do include Comparable def <=>(other) if two_pair? if other.two_pair? if highest_pair_rank == other.highest_pair_rank without { |c| c.rank == highest_pair_rank } <=> other.without { |c| c.rank == highest_pair_rank } else highest_pair_rank <=> other.highest_pair_rank end else 1 end elsif one_pair? if other.two_pair? -1 elsif other.one_pair? if highest_pair_rank == other.highest_pair_rank without { |c| c.rank == highest_pair_rank } <=> other.without { |c| c.rank == highest_pair_rank } else highest_pair_rank <=> other.highest_pair_rank end else 1 end else if empty? && other.empty? 0 elsif other.one_pair? || other.two_pair? -1 elsif highest_card_rank == other.highest_card_rank without { |c| c.rank == highest_card_rank } <=> other.without { |c| c.rank == highest_card_rank } else highest_card_rank <=> other.highest_card_rank end end end # … end

Slide 120

Slide 120 text

Hand = Struct.new(:cards) do include Comparable def <=>(other) if three_of_a_kind? if other.three_of_a_kind? if highest_triple_rank == other.highest_triple_rank without { |c| c.rank == highest_triple_rank } <=> other.without { |c| c.rank == highest_triple_rank } else highest_triple_rank <=> other.highest_triple_rank end else 1 end elsif two_pair? if other.three_of_a_kind? -1 elsif other.two_pair? if highest_pair_rank == other.highest_pair_rank without { |c| c.rank == highest_pair_rank } <=> other.without { |c| c.rank == highest_pair_rank } else highest_pair_rank <=> other.highest_pair_rank end else 1 end elsif one_pair? if other.two_pair? || other.three_of_a_kind? -1 elsif other.one_pair? if highest_pair_rank == other.highest_pair_rank without { |c| c.rank == highest_pair_rank } <=> other.without { |c| c.rank == highest_pair_rank } else highest_pair_rank <=> other.highest_pair_rank end else 1 end else if empty? && other.empty? 0 elsif other.one_pair? || other.two_pair? || other.three_of_a_kind? -1 elsif highest_card_rank == other.highest_card_rank without { |c| c.rank == highest_card_rank } <=> other.without { |c| c.rank == highest_card_rank } else highest_card_rank <=> other.highest_card_rank end end end # … end

Slide 121

Slide 121 text

Hand = Struct.new(:cards) do include Comparable def <=>(other) if straight? if other.straight? if highest_straight_rank == other.highest_straight_rank without { |c| c.rank == highest_straight_rank } <=> other.without { |c| c.rank == highest_straight_rank } else highest_straight_rank <=> other.highest_straight_rank end else 1 end elsif three_of_a_kind? if other.straight? -1 elsif other.three_of_a_kind? if highest_triple_rank == other.highest_triple_rank without { |c| c.rank == highest_triple_rank } <=> other.without { |c| c.rank == highest_triple_rank } else highest_triple_rank <=> other.highest_triple_rank end else 1 end elsif two_pair? if other.three_of_a_kind? || other.straight? -1 elsif other.two_pair? if highest_pair_rank == other.highest_pair_rank without { |c| c.rank == highest_pair_rank } <=> other.without { |c| c.rank == highest_pair_rank } else highest_pair_rank <=> other.highest_pair_rank end else 1 end elsif one_pair? if other.two_pair? || other.three_of_a_kind? || other.straight? -1 elsif other.one_pair? if highest_pair_rank == other.highest_pair_rank without { |c| c.rank == highest_pair_rank } <=> other.without { |c| c.rank == highest_pair_rank } else highest_pair_rank <=> other.highest_pair_rank end else 1 end else if empty? && other.empty? 0 elsif other.one_pair? || other.two_pair? || other.three_of_a_kind? || other.straight? -1 elsif highest_card_rank == other.highest_card_rank without { |c| c.rank == highest_card_rank } <=> other.without { |c| c.rank == highest_card_rank } else highest_card_rank <=> other.highest_card_rank end end end # … end

Slide 122

Slide 122 text

Card = Struct.new(:rank, :suit) class Card::Rank TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE = 13.times.map { new } end class Card::Suit CLUBS, HEARTS, SPADES, DIAMONDS = 4.times.map { new } end three_of_diamonds = Card.new(Card::Rank::THREE, Card::Suit::DIAMONDS) queen_of_hearts = Card.new(Card::Rank::QUEEN, Card::Suit::HEARTS) ace_of_spades = Card.new(Card::Rank::ACE, Card::Suit::SPADES)

Slide 123

Slide 123 text

class Card::Rank include Comparable ORDER = ( TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE = 13.times.map { new } ) def <=>(other) ORDER.index(self) <=> ORDER.index(other) end end

Slide 124

Slide 124 text

class Hand::Category include Comparable ORDER = ( HIGH_CARD, ONE_PAIR, TWO_PAIR, THREE_OF_A_KIND, STRAIGHT, FLUSH, FULL_HOUSE, FOUR_OF_A_KIND, STRAIGHT_FLUSH = 9.times.map { new } ) def <=>(other) ORDER.index(self) <=> ORDER.index(other) end end

Slide 125

Slide 125 text

class << Hand::Category::HIGH_CARD def matches?(hand) true end def compare(a, b) # … end end

Slide 126

Slide 126 text

class << Hand::Category::ONE_PAIR def matches?(hand) pairs(hand).size == 1 end def compare(a, b) # … end end

Slide 127

Slide 127 text

Hand::Rank = Struct.new(:hand) do include Comparable def category Hand::Category::ORDER. select { |category| category.matches?(hand) }.max end def <=>(other) if category == other.category category.compare(hand, other.hand) else category <=> other.category end end end

Slide 128

Slide 128 text

Hand = Struct.new(:cards) do def rank Hand::Rank.new(self) end end if my_hand.rank > your_hand.rank puts 'I win!' end

Slide 129

Slide 129 text

class Card::Rank ORDER = ( TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE = 13.times.map { new } ) def succ ORDER[ORDER.index(self).succ] end end

Slide 130

Slide 130 text

No content

Slide 131

Slide 131 text

No content

Slide 132

Slide 132 text

low high

Slide 133

Slide 133 text

class Card::Rank ORDER = ( TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE = 13.times.map { new } ) def succ ORDER[ORDER.index(self).succ % ORDER.size] end end

Slide 134

Slide 134 text

“DESIGN”?

Slide 135

Slide 135 text

When a thing looks complicated, it’s possible that we’re looking at it wrong and missing some of the pieces of the puzzle.” — Richard Feynman “

Slide 136

Slide 136 text

HOW?

Slide 137

Slide 137 text

FIND THE HONEST ABSTRACTION

Slide 138

Slide 138 text

FIND THE MINIMAL ABSTRACTION

Slide 139

Slide 139 text

FIND THE INTERESTING THING INSIDE THE BORING THING

Slide 140

Slide 140 text

BE EXCITED!

Slide 141

Slide 141 text

KEEP YOUR PERSPECTIVE

Slide 142

Slide 142 text

IF YOU CAN WRITE A PROGRAM, YOU’RE GOOD AT MATHEMATICS.

Slide 143

Slide 143 text

SORRY!* * not sorry

Slide 144

Slide 144 text

THANKS @tomstuart, [email protected], http:/ /codon.com/