Slide 1

Slide 1 text

Real-world Functional Ruby Southeast Ruby 2017

Slide 2

Slide 2 text

@timriley

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

@icelab

Slide 5

Slide 5 text

!

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

@dry_rb @rom_rb

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

PODCASTERS

Slide 11

Slide 11 text

“TBA”

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

“Programmers at work maintaining a Ruby on Rails application” Eero Järnefelt 
 Oil on canvas, 1893

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

user.rb after_save accepts_nested_attributes_for

Slide 18

Slide 18 text

⏱ DESIGN STAMINA HYPOTHESIS

Slide 19

Slide 19 text

⏱ DESIGN STAMINA HYPOTHESIS

Slide 20

Slide 20 text

⏱ DESIGN STAMINA HYPOTHESIS

Slide 21

Slide 21 text

⏱ DESIGN STAMINA HYPOTHESIS No design

Slide 22

Slide 22 text

⏱ DESIGN STAMINA HYPOTHESIS No design

Slide 23

Slide 23 text

⏱ DESIGN STAMINA HYPOTHESIS No design

Slide 24

Slide 24 text

⏱ DESIGN STAMINA HYPOTHESIS No design Good design

Slide 25

Slide 25 text

⏱ DESIGN STAMINA HYPOTHESIS No design Good design

Slide 26

Slide 26 text

⏱ DESIGN STAMINA HYPOTHESIS No design Good design Design payoff line

Slide 27

Slide 27 text

Object-oriented design

Slide 28

Slide 28 text

The gap.

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

Haskell

Slide 32

Slide 32 text

Haskell

Slide 33

Slide 33 text

Haskell

Slide 34

Slide 34 text

Real-world Functional Ruby Southeast Ruby 2017 Tim Riley // @timriley

Slide 35

Slide 35 text

FP is hot AF

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

App as series of transformations

Slide 38

Slide 38 text

Request → [*] → [*] → Response

Slide 39

Slide 39 text

App as series of transformations

Slide 40

Slide 40 text

App as series of functions

Slide 41

Slide 41 text

Functional objects

Slide 42

Slide 42 text

class ImportProducts def call(feed) import_from(feed) end end

Slide 43

Slide 43 text

class ImportProducts def call(feed) import_from(feed) end end

Slide 44

Slide 44 text

class ImportProducts def call(feed) import_from(feed) end end

Slide 45

Slide 45 text

class ImportProducts def call(feed) import_from(feed) end end

Slide 46

Slide 46 text

Separate data from behaviour

Slide 47

Slide 47 text

Immutable

Slide 48

Slide 48 text

class ImportProducts def call(feed) end end records = download_feed.(feed) records.each do |attrs| products_repo.create(attrs) end

Slide 49

Slide 49 text

class ImportProducts def call(feed) records = download_feed.(feed) records.each do |attrs| products_repo.create(attrs) end end end

Slide 50

Slide 50 text

class ImportProducts def call(feed) records = download_feed.(feed) records.each do |attrs| product_repo.create(attrs) end end end

Slide 51

Slide 51 text

download_feed: product_repo:

Slide 52

Slide 52 text

class ImportProducts attr_reader :download_feed attr_reader :products_repo def initialize(download_feed:, product_repo:) @download_feed = download_feed @products_repo = products_repo end def call(feed) # ... end end

Slide 53

Slide 53 text

class ImportProducts attr_reader :download_feed attr_reader :product_repo def initialize(download_feed:, product_repo:) @download_feed = download_feed @product_repo = product_repo end def call(feed) # ... end end

Slide 54

Slide 54 text

# Initialize once import = ImportProducts.new( download_feed: download, product_repo: repo, ) # Reuse many times import.(:books_feed) import.(:dvds_feed)

Slide 55

Slide 55 text

# Initialize once import = ImportProducts.new( download_feed: download, product_repo: repo, ) # Call many times import.(book_feed) import.(film_feed)

Slide 56

Slide 56 text

Feeds::Create Feeds::Update Feeds::Delete Products::Update

Slide 57

Slide 57 text

Behaviour

Slide 58

Slide 58 text

Data

Slide 59

Slide 59 text

Types

Slide 60

Slide 60 text

Values

Slide 61

Slide 61 text

class Product attr_reader :title, :isbn def initialize(**attrs) @title = attrs[:title] @isbn = attrs[:isbn] end def slug "#{to_slug(title)}-#{isbn}" end end

Slide 62

Slide 62 text

class Product attr_reader :title, :isbn def initialize(**attrs) @title = attrs[:title] @isbn = attrs[:isbn] end def slug "#{to_slug(title)}-#{isbn}" end end

Slide 63

Slide 63 text

Values objects are first-class

Slide 64

Slide 64 text

Values & Functions

Slide 65

Slide 65 text

Values Functions Hold data Operate on data

Slide 66

Slide 66 text

Values Functions Hold data Operate on data

Slide 67

Slide 67 text

Values Functions Hold data Operate on data Inert Active

Slide 68

Slide 68 text

Values Functions Hold data Operate on data Inert Active Content Pipeline

Slide 69

Slide 69 text

Functional design

Slide 70

Slide 70 text

Functional, object-oriented design

Slide 71

Slide 71 text

Functional Ruby yields better
 object-oriented Ruby!

Slide 72

Slide 72 text

RSpec.describe ImportProducts

Slide 73

Slide 73 text

download_feed product_repo

Slide 74

Slide 74 text

let(:download) { double(:download) } let(:repo) { double(:repo) }

Slide 75

Slide 75 text

subject(:import) { ImportProducts.new( download_feed: download, product_repo: repo, ) }

Slide 76

Slide 76 text

let(:feed) { Feed.new(…) } before do allow(download_feed) .to receive(:call) .with(feed) .and_return(books_data) end

Slide 77

Slide 77 text

let(:feed) { Feed.new(…) } before do allow(download) .to receive(:call) .with(feed) .and_return(BOOKS_DATA) end

Slide 78

Slide 78 text

it "creates the products" do expect(repo) .to receive(:create).with(title: "…") import.(feed) end

Slide 79

Slide 79 text

Import
 Products download_feed product_repo

Slide 80

Slide 80 text

Dependencies

Slide 81

Slide 81 text

App as graph

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

Good design makes change easier

Slide 85

Slide 85 text

Functional design makes change easier

Slide 86

Slide 86 text

Real developers hate him! He’s idealistic He’s making too many .rb files He found a #method to build better apps. Learn the one WEIRD trick to his stunning results! GET FUNCTIONAL NOW

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

A real-world functional Ruby ecosystem

Slide 89

Slide 89 text

dry-rb

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

Dependency management

Slide 93

Slide 93 text

dry-system & dry-auto_inject

Slide 94

Slide 94 text

# lib/my_app/import_products.rb MyApp::Container[“import_products"] #

Slide 95

Slide 95 text

# lib/my_app/import_products.rb MyApp::Container["import_products"] #

Slide 96

Slide 96 text

class ImportProducts include MyApp::Import[ "products.download_feed", "product_repo", ] def call(feed) # ... end end

Slide 97

Slide 97 text

# lib/my_app/import_products.rb MyApp::Container["import_products"] #

Slide 98

Slide 98 text

# lib/my_app/import_products.rb MyApp::Container["import_products"] # product_repo=#>

Slide 99

Slide 99 text

ImportProducts.new( download_feed: double(:download) ) # product_repo=#>

Slide 100

Slide 100 text

ImportProducts.new( download_feed: double(:download) ) # product_repo=#>

Slide 101

Slide 101 text

Result handling

Slide 102

Slide 102 text

dry-monads & dry-matcher

Slide 103

Slide 103 text

# age, or nil Maybe(age) # successful import results Right(imported_products)

Slide 104

Slide 104 text

# age, or nil Maybe(age) Maybe(33) => Some(33) # successful import results Right(imported_products)

Slide 105

Slide 105 text

# age, or nil Maybe(age) Maybe(33) => Some(33) Maybe(nil) => None

Slide 106

Slide 106 text

# Successful import result Right(imported_products)

Slide 107

Slide 107 text

import_products.() do |m| m.success do |imported_products| # … end m.failure do |error| # … end end

Slide 108

Slide 108 text

Data & types

Slide 109

Slide 109 text

dry-validation

Slide 110

Slide 110 text

Dry::Validation.Schema do required(:name).filled required(:url).filled(format?: /…/) required(:active).filled(:bool?) end

Slide 111

Slide 111 text

dry-types & dry-struct

Slide 112

Slide 112 text

ISBN = Strict::String .constrained(format: /…/)

Slide 113

Slide 113 text

class Product < Dry::Struct attribute :title, Types::Strict::String attribute :isbn, Types::ISBN end

Slide 114

Slide 114 text

View rendering

Slide 115

Slide 115 text

dry-view

Slide 116

Slide 116 text

expose :product do |slug:| product_repo.by_slug(slug) end

Slide 117

Slide 117 text

HTTP & routing

Slide 118

Slide 118 text

dry-web-roda

Slide 119

Slide 119 text

$ dry-web-roda new my_app

Slide 120

Slide 120 text

. ├── Gemfile ├── README.md ├── Rakefile ├── apps │ └── main │ ├── lib │ │ └── main │ │ └── views │ │ └── welcome.rb │ ├── system │ │ ├── boot │ │ │ └── view.rb │ │ ├── boot.rb │ │ └── main │ │ ├── application.rb │ │ ├── container.rb │ │ ├── import.rb │ │ ├── transactions.rb │ │ ├── view_context.rb │ │ └── view_controller.rb

Slide 121

Slide 121 text

│ │ └── relations │ └── types.rb ├── log ├── spec │ ├── app_helper.rb │ ├── db_helper.rb │ ├── spec_helper.rb │ └── support │ ├── db │ │ └── test_factories.rb │ └── test_helpers.rb └── system ├── blog │ ├── application.rb │ ├── container.rb │ ├── import.rb │ └── settings.rb ├── boot │ └── rom.rb └── boot.rb 28 directories, 37 files

Slide 122

Slide 122 text

Persistence

Slide 123

Slide 123 text

rom-rb

Slide 124

Slide 124 text

Dependency management Result handling Data validation & types Views Routing & HTTP Database persistence

Slide 125

Slide 125 text

No content

Slide 126

Slide 126 text

A functional Ruby movement

Slide 127

Slide 127 text

No content

Slide 128

Slide 128 text

No content

Slide 129

Slide 129 text

hanami

Slide 130

Slide 130 text

RailsClub 2017 // @jodosha Functional Web with Hanami

Slide 131

Slide 131 text

Ruby is ready

Slide 132

Slide 132 text

There is nothing more powerful than an idea whose time has come. — Victor Hugo

Slide 133

Slide 133 text

Bridging the gap for Ruby

Slide 134

Slide 134 text

Real-world Functional Ruby

Slide 135

Slide 135 text

FP + OO = Win!

Slide 136

Slide 136 text

FP + OO + Community = Win!

Slide 137

Slide 137 text

Functional Ruby is supported

Slide 138

Slide 138 text

Functional Ruby is practical

Slide 139

Slide 139 text

Functional Ruby is worth a try!

Slide 140

Slide 140 text

dry-rb/dry-web-blog

Slide 141

Slide 141 text

Thank you! @timriley dry-rb/dry-web-blog bit.ly/se-functional-ruby