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 NumberCruncher | |
def initialize(calculator) | |
@calculator = calculator | |
end | |
def crunch(array_of_numbers) | |
@reduction ||= array_of_numbers.each do |number| | |
@calculator.add number | |
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
# app/services/calculation_service.rb | |
class CalculationService | |
def initialize(repository) | |
@repository = repository | |
end | |
def calculate(operation, x, y) | |
result = StatelessCalculator.send operation, x, y | |
@repository.save_calculation operation, x, y, result | |
result |
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
def add_range_safe | |
threads = (1..256).each_slice(16).map do |range| | |
Thread.new do | |
Thread.current[:sum] = range.inject(0) do |memo, n| | |
StatelessCalculator.add memo, n | |
end | |
end | |
end | |
result = threads.map { |t| t.join[:sum] } .reduce(:+) |
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
def add_range_unsafe | |
c = StatefulCalculator.new | |
threads = (1..256).each_slice(16).map do |range| | |
Thread.new do | |
range.each { |n| c.add n } | |
end | |
end | |
threads.map(&:join) |
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 StatelessCalculator do | |
it 'should add some numbers' do | |
expect(StatelessCalculator.add(10, StatelessCalculator.add(5, 3))).to eq(18) | |
end | |
it 'should divide some numbers' do | |
expect(StatelessCalculator.div(10, 2)).to eq(5) | |
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 |
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
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
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
# 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] |