This file contains 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
# Functional coding in Ruby - better than chunky bacon | |
print_to_console = ->(msg) { puts msg } | |
print_to_motd_file = ->(msg) { File.write("motd.txt", msg) } | |
# Display the "message of the day" via the selected printer | |
motd = ->(printer) { | |
msg = ["Hello World,\n", | |
" Sincerely,\n", | |
" thatrubylove"].join | |
printer.(msg) |
This file contains 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
sum = ->(num_list) { num_list.reduce(:+) } | |
square = ->(number ) { number * number } | |
squares = ->(num_list) { num_list.map {|num| square.(num) } } | |
sum_squares = ->(num_list) { sum.(squares.(num_list)) } | |
square_sum = ->(num_list) { square.(sum.(num_list)) } | |
gem 'minitest' | |
require 'minitest/autorun' | |
describe "sum_squares" do |
This file contains 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
[ | |
{:type=>"Functional::Lists::SinglyLinked", :operation=>"#plus", :sample_size=>10000, :benchmark=>0.116519289}, | |
{:type=>"Functional::Lists::SinglyLinked", :operation=>"#plus", :sample_size=>50000, :benchmark=>0.557987583}, | |
{:type=>"Functional::Lists::SinglyLinked", :operation=>"#plus", :sample_size=>750000, :benchmark=>18.900261197}, | |
{:type=>"Functional::Lists::SinglyLinked", :operation=>"#plus", :sample_size=>2000000, :benchmark=>145.656489316} | |
], | |
[ | |
{:type=>"Array", :operation=>"#plus", :sample_size=>10000, :benchmark=>1.640012864}, | |
{:type=>"Array", :operation=>"#plus", :sample_size=>50000, :benchmark=>5.108800796}, | |
{:type=>"Array", :operation=>"#plus", :sample_size=>750000, :benchmark=>71.655338726}, |
This file contains 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
Rehearsal ----------------------------------------------------- | |
array#push (100) 0.000000 0.000000 0.000000 ( 0.000013) | |
list#push (100) 0.000000 0.000000 0.000000 ( 0.000078) | |
-------------------------------------------- total: 0.000000sec | |
user system total real | |
array#push (100) 0.000000 0.000000 0.000000 ( 0.000012) | |
list#push (100) 0.000000 0.000000 0.000000 ( 0.000044) | |
Rehearsal ------------------------------------------------------ | |
array#push (1000) 0.000000 0.000000 0.000000 ( 0.000084) |
This file contains 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
Rank = Struct.new(:rank, :value) do | |
def to_s; rank; end | |
end | |
Card = Struct.new(:rank, :suit) do | |
def to_s; "#{rank} of #{suit}"; end | |
end | |
class Deck | |
SUITS = %w(hearts clubs spades diamonds) |
This file contains 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
# Conditional reach-around | |
User = Struct.new(:name, :role) do; end | |
def greet(user) | |
if user.name && user.role | |
"Hello, I am an #{user.role} user. My name is #{user.name}." | |
elsif user.name | |
"Hello, my name is #{user.name}." | |
else |
This file contains 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
DECK = %w[Ace 2 3 4 5 6 7 8 9 10 Jack Queen King].product(%w[Clubs Diamonds Hearts Spades]).map {|rank, suit| "#{rank} of #{suit}" } |
This file contains 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 'minitest/autorun' | |
describe "#lines_after_word(lines, target)" do | |
let(:file) { File.expand_path("../innisfree.txt", __FILE__) } | |
let(:lines) { File.readlines(file) } | |
it "must return an empty array if no lines include the target word" do | |
lines_after_word(lines, "hola").must_equal [] | |
end | |
end |
This file contains 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 lines_after_word(lines, target) | |
index = lines.find_index {|line| line =~ /#{target}/ } | |
index.nil? ? [] : "" | |
end |
This file contains 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 'minitest/autorun' | |
describe "#lines_after_word(lines, target)" do | |
let(:lines) { File.readlines(File.expand_path("../innisfree.txt", __FILE__)) } | |
it "must return all the lines if the first line includes the target word" do | |
lines_after_word(lines, "Innisfree").count.must_equal lines.count | |
end | |
end |
OlderNewer