Functional Ruby Idioms
Composition
Function composition combines small operations into a larger operation.
composition
Composed functions keep each step small while still producing one final result.
Composition
composition.rb
Replay: real traced execution (multi-file project)
suffix = "!"
trim = ->(text) { text.strip }
upcase = ->(text) { text.upcase }
decorate = ->(text) { "#{text}#{suffix}" }
input = " ruby "
step_one = trim.call(input)
step_two = upcase.call(step_one)
result = decorate.call(step_two)
puts "suffix=#{suffix}"
puts "step_one=#{step_one}"
puts "result=#{result}"
suffix = "?"
trim = ->(text) { text.strip }
upcase = ->(text) { text.upcase }
decorate = ->(text) { "#{text}#{suffix}" }
input = " ruby "
step_one = trim.call(input)
step_two = upcase.call(step_one)
result = decorate.call(step_two)
puts "suffix=#{suffix}"
puts "step_one=#{step_one}"
puts "result=#{result}"
suffix = "."
trim = ->(text) { text.strip }
upcase = ->(text) { text.upcase }
decorate = ->(text) { "#{text}#{suffix}" }
input = " ruby "
step_one = trim.call(input)
step_two = upcase.call(step_one)
result = decorate.call(step_two)
puts "suffix=#{suffix}"
puts "step_one=#{step_one}"
puts "result=#{result}"
suffix ← !, trim ← ⟨Proc A /tmp/execution/⟨tmp B⟩.rb:32 (lambda)⟩
1suffix→ ! = "!" #@suffix="?", "."2trim→ ⟨Proc A /tmp/execution/⟨tmp B⟩.rb:32 (lambda)⟩ = ->(text) { text.strip }3upcase→ ⟨Proc C /tmp/execution/⟨tmp B⟩.rb:40 (lambda)⟩ = ->(text) { text.upcase }4decorate→ ⟨Proc D /tmp/execution/⟨tmp B⟩.rb:48 (lambda)⟩ = ->(text) { "#{text}#{suffix}" }56input→ ruby = " ruby "7step_one→ ruby = trim.call(input)ruby8step_two→ RUBY = upcase.call(step_one)RUBY9result→ RUBY! = decorate.call(step_two)RUBY!1011puts "suffix=#{suffix!}"12puts "step_one=#{step_oneruby}"13puts "result=#{resultRUBY!}"outputsuffix=! step_one=ruby result=RUBY!
suffix ← ?, trim ← ⟨Proc A /tmp/execution/⟨tmp B⟩.rb:32 (lambda)⟩
1suffix→ ? = "?"2trim→ ⟨Proc A /tmp/execution/⟨tmp B⟩.rb:32 (lambda)⟩ = ->(text) { text.strip }3upcase→ ⟨Proc C /tmp/execution/⟨tmp B⟩.rb:40 (lambda)⟩ = ->(text) { text.upcase }4decorate→ ⟨Proc D /tmp/execution/⟨tmp B⟩.rb:48 (lambda)⟩ = ->(text) { "#{text}#{suffix}" }56input→ ruby = " ruby "7step_one→ ruby = trim.call(input)ruby8step_two→ RUBY = upcase.call(step_one)RUBY9result→ RUBY? = decorate.call(step_two)RUBY?1011puts "suffix=#{suffix?}"12puts "step_one=#{step_oneruby}"13puts "result=#{resultRUBY?}"outputsuffix=? step_one=ruby result=RUBY?
suffix ← ., trim ← ⟨Proc A /tmp/execution/⟨tmp B⟩.rb:32 (lambda)⟩
1suffix→ . = "."2trim→ ⟨Proc A /tmp/execution/⟨tmp B⟩.rb:32 (lambda)⟩ = ->(text) { text.strip }3upcase→ ⟨Proc C /tmp/execution/⟨tmp B⟩.rb:40 (lambda)⟩ = ->(text) { text.upcase }4decorate→ ⟨Proc D /tmp/execution/⟨tmp B⟩.rb:48 (lambda)⟩ = ->(text) { "#{text}#{suffix}" }56input→ ruby = " ruby "7step_one→ ruby = trim.call(input)ruby8step_two→ RUBY = upcase.call(step_one)RUBY9result→ RUBY. = decorate.call(step_two)RUBY.1011puts "suffix=#{suffix.}"12puts "step_one=#{step_oneruby}"13puts "result=#{resultRUBY.}"outputsuffix=. step_one=ruby result=RUBY.