Slide 1

Slide 1 text

CODE AS DATA DENIS DEFREYNE (PRONOUNS: HE/HIM) @DDFREYNE / DENIS.WS RUBYCONFBY 2019 2019-04-06

Slide 2

Slide 2 text

nanoc / cri / ddmemoize / ddmetrics / …

Slide 3

Slide 3 text

I work at

Slide 4

Slide 4 text

$$$

Slide 5

Slide 5 text

Slide 6

Slide 6 text

Why do we need a pricing algorithm?
 
 
 


Slide 7

Slide 7 text

Why do we need a pricing algorithm?
 
 * fixed price?
 


Slide 8

Slide 8 text

Why do we need a pricing algorithm?
 
 * fixed price? unfair.
 


Slide 9

Slide 9 text

Why do we need a pricing algorithm?
 
 * fixed price? unfair.
 * price per hour?


Slide 10

Slide 10 text

Why do we need a pricing algorithm?
 
 * fixed price? unfair.
 * price per hour? hard to calculate.


Slide 11

Slide 11 text

Why do we need a pricing algorithm?
 
 * fixed price? unfair.
 * price per hour? hard to calculate.
 * wait and see what the price turns out to be?

Slide 12

Slide 12 text

Why do we need a pricing algorithm?
 
 * fixed price? unfair.
 * price per hour? hard to calculate.
 * wait and see what the price turns out to be? risky.

Slide 13

Slide 13 text

Why do we need a pricing algorithm?
 
 * fixed price? unfair.
 * price per hour? hard to calculate. * wait and see what the price turns out to be? risky.
 * algorithm?

Slide 14

Slide 14 text

Why do we need a pricing algorithm?
 
 * fixed price? unfair.
 * price per hour? hard to calculate. * wait and see what the price turns out to be? risky.
 * algorithm? yes!

Slide 15

Slide 15 text

$$$

Slide 16

Slide 16 text

$$$ * Business need to know
 why prices are the way they are
 
 
 
 
 


Slide 17

Slide 17 text

$$$ * Business need to know
 why prices are the way they are
 * Developers need to be involved
 to implement new formulas
 
 


Slide 18

Slide 18 text

$$$ * Business need to know
 why prices are the way they are
 * Developers need to be involved
 to implement new formulas
 * Developers have no good way
 to verify new formulas
 
 


Slide 19

Slide 19 text

$$$ * Business need to know
 why prices are the way they are
 * Developers need to be involved
 to implement new formulas
 * Developers have no good way
 to verify new formulas
 * It is difficult
 to dry-run new formulas

Slide 20

Slide 20 text

def total_price 200 + volume * distance end

Slide 21

Slide 21 text

THE IDEA

Slide 22

Slide 22 text

def total_price 200 + volume * distance end

Slide 23

Slide 23 text

def total_price 200 + volume * distance end sum

Slide 24

Slide 24 text

def total_price 200 + volume * distance end sum product

Slide 25

Slide 25 text

def total_price 200 + volume * distance end sum product variable

Slide 26

Slide 26 text

def total_price 200 + volume * distance end sum product constant variable

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

class SumExpr def initialize(left, right) @left = left @right = right end end

Slide 29

Slide 29 text

class SumExpr def initialize(left, right) @left = left @right = right end end class ProductExpr def initialize(left, right) @left = left @right = right end end

Slide 30

Slide 30 text

class SumExpr def initialize(left, right) @left = left @right = right end end class ProductExpr def initialize(left, right) @left = left @right = right end end class ConstantExpr def initialize(value) @value = value end end


Slide 31

Slide 31 text

class SumExpr def initialize(left, right) @left = left @right = right end end class ProductExpr def initialize(left, right) @left = left @right = right end end class ConstantExpr def initialize(value) @value = value end end
 class VariableExpr def initialize(name) @name = name end end


Slide 32

Slide 32 text

def price_formula SumExpr.new( ConstantExpr.new(200), ProductExpr.new( VariableExpr.new(Tvolume), VariableExpr.new(Tdistance), ) ) end

Slide 33

Slide 33 text

params = { volume: 20, distance: 200, } price_formula.evaluate(params)

Slide 34

Slide 34 text


 
 params = { volume: 20, distance: 200, } price_formula.evaluate(params)
 
 4200

Slide 35

Slide 35 text

def evaluate(params)

Slide 36

Slide 36 text

class VariableExpr def initialize(name) @name = name end def evaluate(params) params.fetch(@name) end end

Slide 37

Slide 37 text

class ConstantExpr def initialize(value) @value = value end def evaluate(_params) @value end end

Slide 38

Slide 38 text

class SumExpr def initialize(left, right) @left = left @right = right end def evaluate(params) @left.evaluate(params) + @right.evaluate(params) end end


Slide 39

Slide 39 text

class ProductExpr def initialize(left, right) @left = left @right = right end def evaluate(params) @left.evaluate(params) * @right.evaluate(params) end end


Slide 40

Slide 40 text


 
 params = { volume: 20, distance: 200, } 
 


Slide 41

Slide 41 text


 
 params = { volume: 20, distance: 200, } price_formula.evaluate(params)
 


Slide 42

Slide 42 text


 
 params = { volume: 20, distance: 200, } price_formula.evaluate(params)
 
 4200

Slide 43

Slide 43 text

THE POINT

Slide 44

Slide 44 text

SumExpr.new( ConstantExpr.new(200), ProductExpr.new( VariableExpr.new(Tvolume), VariableExpr.new(Tdistance), ) )
 
 
 
 
 


Slide 45

Slide 45 text

SumExpr.new( ConstantExpr.new(200), ProductExpr.new( VariableExpr.new(Tvolume), VariableExpr.new(Tdistance), ) )
 
 + Z[> 4200 const Z[> 200 * Z[> 4000 var volume Z[> 20 var distance Z[> 200

Slide 46

Slide 46 text

class Node def initialize(name, value, children) @name = name @value = value @children = children end end

Slide 47

Slide 47 text

+ Z[> 4200 const Z[> 200 * Z[> 4000 var volume Z[> 20 var distance Z[> 200

Slide 48

Slide 48 text

+ Z[> 4200 const Z[> 200 * Z[> 4000 var volume Z[> 20 var distance Z[> 200 Node.new("+", 4200, [const_node, product_node])

Slide 49

Slide 49 text

+ Z[> 4200 const Z[> 200 * Z[> 4000 var volume Z[> 20 var distance Z[> 200 Node.new("+", 4200, [const_node, product_node]) Node.new("var volume", 20, [])

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

class VariableExpr def initialize(name) @name = name end def evaluate(params) params.fetch(@name) end end

Slide 52

Slide 52 text

class VariableExpr def initialize(name) @name = name end def evaluate(params) Node.new( "var #{@name}", params.fetch(@name), [], ) end end

Slide 53

Slide 53 text

class ConstantExpr def initialize(value) @value = value end def evaluate(_params) @value end end

Slide 54

Slide 54 text

class ConstantExpr def initialize(value) @value = value end def evaluate(_params) Node.new( 'const', @value, [], ) end end

Slide 55

Slide 55 text

class SumExpr def initialize(left, right) @left = left @right = right end def evaluate(params) left_node = @left.evaluate(params) right_node = @right.evaluate(params) Node.new( '+', left_node.value + right_node.value, [left_node, right_node], ) end end

Slide 56

Slide 56 text

class ProductExpr def initialize(left, right) @left = left @right = right end def evaluate(params) left_node = @left.evaluate(params) right_node = @right.evaluate(params) Node.new( '*', left_node.value * right_node.value, [left_node, right_node], ) end end

Slide 57

Slide 57 text

class Node def to_s # … left to your imagination … end end

Slide 58

Slide 58 text

puts price_formula.evaluate(params)
 
 
 
 
 


Slide 59

Slide 59 text

puts price_formula.evaluate(params) + >?> 4200 const >?> 200 * >?> 4000 var volume >?> 20 var distance >?> 200

Slide 60

Slide 60 text

puts price_formula.evaluate(params).value
 
 
 
 
 


Slide 61

Slide 61 text

puts price_formula.evaluate(params).value
 
 4200
 
 
 


Slide 62

Slide 62 text

THE BEST PART: The price formula did not change!

Slide 63

Slide 63 text

* Business need to know
 why prices are the way they are
 * Developers need to be involved
 to implement new formulas
 * Developers have no good way
 to verify new formulas
 * It is difficult
 to dry-run new formulas

Slide 64

Slide 64 text

* Business need to know
 why prices are the way they are
 * Developers need to be involved
 to implement new formulas
 * Developers have no good way
 to verify new formulas
 * It is difficult
 to dry-run new formulas

Slide 65

Slide 65 text

* Business need to know
 why prices are the way they are
 * Developers need to be involved
 to implement new formulas
 * Developers have no good way
 to verify new formulas
 * It is difficult
 to dry-run new formulas

Slide 66

Slide 66 text

THE POINT (PART II): THE GUI

Slide 67

Slide 67 text

Slide 68

Slide 68 text

"200 + volume * distance" 
 
 
 
 
 


Slide 69

Slide 69 text

"200 + volume * distance" C> SumExpr.new( ConstantExpr.new(200), ProductExpr.new( VariableExpr.new(Nvolume), VariableExpr.new(Ndistance), ) )

Slide 70

Slide 70 text

parse("200 + volume * distance") C> SumExpr.new( ConstantExpr.new(200), ProductExpr.new( VariableExpr.new(Nvolume), VariableExpr.new(Ndistance), ) )

Slide 71

Slide 71 text

require 'd-parse'

Slide 72

Slide 72 text

module Grammar extend DParseijDSL # … end

Slide 73

Slide 73 text

DIGIT = char_in('0'..'9')

Slide 74

Slide 74 text

CONSTANT_EXPR = repeat1(DIGIT) .capture .map { |d| ConstantExpr.new(d.to_i(10)) }

Slide 75

Slide 75 text

VARIABLE_EXPR = repeat1(char_in('a'..'z')) .capture .map { |d| VariableExpr.new(d) }

Slide 76

Slide 76 text

OPERAND = alt( CONSTANT_EXPR, VARIABLE_EXPR, )

Slide 77

Slide 77 text

OPERATOR = alt( char('+'), char('*'), ).capture

Slide 78

Slide 78 text

OP_SEQ = intersperse( OPERAND, OPERATOR, ).map { |d| OpSeqExpr.new(d) }

Slide 79

Slide 79 text

ROOT = seq( OP_SEQ, eof, ).first

Slide 80

Slide 80 text

input = "200 + volume * distance"

Slide 81

Slide 81 text

input = "200 + volume * distance" raw_expr = GrammarijROOT.apply( input_string, )

Slide 82

Slide 82 text

OpSeqExpr.new( [ ConstantExpr.new(200), "+", VariableExpr.new(Tvolume), "*", VariableExpr.new(Tdistance), ] )

Slide 83

Slide 83 text

def resolve_op_seq_exprs(expr) # … apply shunting yard or so … end

Slide 84

Slide 84 text

resolve_op_seq_exprs(raw_expr)

Slide 85

Slide 85 text

SumExpr.new( ConstantExpr.new(200), ProductExpr.new( VariableExpr.new(Tvolume), VariableExpr.new(Tdistance), ) )

Slide 86

Slide 86 text

input = "200 + volume * distance"

Slide 87

Slide 87 text

input = "200 + volume * distance" GrammarijROOT.apply( input, )

Slide 88

Slide 88 text

input = "200 + volume * distance" resolve_op_seq_exprs( GrammarijROOT.apply( input, ), )

Slide 89

Slide 89 text

input = "200 + volume * distance" price_formula = resolve_op_seq_exprs( GrammarijROOT.apply( input, ), )

Slide 90

Slide 90 text

params = { volume: 20, distance: 200, } 
 


Slide 91

Slide 91 text

params = { volume: 20, distance: 200, } puts price_formula.evaluate(params).value
 


Slide 92

Slide 92 text

params = { volume: 20, distance: 200, } puts price_formula.evaluate(params).value
 
 4200

Slide 93

Slide 93 text

* Business need to know
 why prices are the way they are
 * Developers need to be involved
 to implement new formulas
 * Developers have no good way
 to verify new formulas
 * It is difficult
 to dry-run new formulas

Slide 94

Slide 94 text

* Business need to know
 why prices are the way they are
 * Developers need to be involved
 to implement new formulas
 * Developers have no good way
 to verify new formulas
 * It is difficult
 to dry-run new formulas

Slide 95

Slide 95 text

* Business need to know
 why prices are the way they are
 * Developers need to be involved
 to implement new formulas
 * Developers have no good way
 to verify new formulas
 * It is difficult
 to dry-run new formulas

Slide 96

Slide 96 text

* Business need to know
 why prices are the way they are
 * Developers need to be involved
 to implement new formulas
 * Developers have no good way
 to verify new formulas
 * It is difficult
 to dry-run new formulas

Slide 97

Slide 97 text

THE POINT (PART III): MORE BENEFITS

Slide 98

Slide 98 text

MORE BENEFITS: 
 


Slide 99

Slide 99 text

MORE BENEFITS: * Versioning & locking
 


Slide 100

Slide 100 text

MORE BENEFITS: * Versioning & locking
 * Translate to Ruby 


Slide 101

Slide 101 text

MORE BENEFITS: * Versioning & locking
 * Translate to Ruby * Translate to SQL


Slide 102

Slide 102 text

MORE BENEFITS: * Versioning & locking
 * Translate to Ruby * Translate to SQL
 * Compile with LLVM

Slide 103

Slide 103 text

Is this over-engineering?

Slide 104

Slide 104 text

TAKE-AWAYS

Slide 105

Slide 105 text

* It’s much easier to reason about data
 than to reason about code. 
 


Slide 106

Slide 106 text

* It’s much easier to reason about data
 than to reason about code. * Try novel approaches; they can
 reveal new opportunities.

Slide 107

Slide 107 text

DENIS DEFREYNE @DDFREYNE / DENIS.WS