This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# a fake repository | |
class Repository | |
def self.save_calculation(op, n1, n2, result) | |
puts "just saved #{op}, #{n1}, #{n2}, #{result}" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# implemented in the controller action, result from the | |
# calculator operation is passed to the Repository | |
# app/controllers/calculation.rb | |
def create | |
h = ActiveSupport::JSON.decode(request.body) | |
fx = lambda do |op, mod, repo| | |
lambda do |x, y| | |
result = mod.method(op.to_sym).to_proc[x, y] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StatefulCalculator | |
def initialize(total) | |
@total = total | |
end | |
def add(x) | |
StatefulCalculator.new(@total + x) | |
end | |
def sub(x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative '../foo' | |
describe StatefulCalculator do | |
before(:all) do | |
@subject = StatefulCalculator.new | |
end | |
it 'should add some numbers' do | |
expect(@subject.add(5).add(3).add(10).result).to eq(18) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Prunable | |
def from_assets(assets) | |
assets.select &method(:is_prunable?) | |
end | |
def is_prunable?(asset) | |
asset.created_at < Date.today - 30 | |
end | |
module_function from_assets, is_prunable? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lambda is impure: mutates shared state (the fruits hash) | |
fruits = {} | |
hashify = lambda do |fruit| | |
fruits[fruit] = fruit | |
end | |
["apple", "pear"].each &hashify | |
# lambda is pure: operates only on its arguments | |
to_nv_pair = lambda do |fruit| | |
[fruit, fruit] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StatefulCalculator | |
def initialize | |
@total = 0 | |
end | |
def add(x) | |
@total += x | |
self | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module StatelessCalculator | |
def add(x, y) | |
x + y | |
end | |
def div(num, denom) | |
num / denom | |
end | |
module_function :add, :div |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe StatefulCalculator do | |
before(:all) do | |
@subject = StatefulCalculator.new | |
end | |
it 'should add some numbers' do | |
expect(@subject.add(5).add(3).add(10).result).to eq(18) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe StatefulCalculator do | |
before(:all) do | |
@subject = StatefulCalculator.new | |
end | |
it 'should add some numbers' do | |
expect(@subject.add(5).add(3).add(10).result).to eq(18) | |
end | |
it 'should divide some numbers' do |