building a deck of cards
suits = %w(S C H D)
faces = (2..10).to_a + %w(J Q K A)
deck = suits.product(faces)
Slide 82
Slide 82 text
building a deck of cards
suits = %w(S C H D)
faces = (2..10).to_a + %w(J Q K A)
deck = suits.product(faces)
=> [[“S”, 2], [“S”, 3], …, [“S”, “A”],
[“C”, 1], …, [“C”, “A”], …]
Slide 83
Slide 83 text
building a deck of cards
suits = %w(S C H D)
faces = (2..10).to_a + %w(J Q K A)
deck = suits.product(faces)
=> [“S2”, “S3”, …, “SA”, “C1”, …, “C2”,
…]
Slide 84
Slide 84 text
join(sep=$,) -> str
Returns a string created by converting each element
of the array to a string, seperated by sep.
[ “a”, “b”, “c”].join
=> “abc”
[ “a”, “b”, “c”].join(“-”)
=> “a-b-c”
Slide 85
Slide 85 text
building a deck of cards
suits = %w(S C H D)
faces = (2..10).to_a + %w(J Q K A)
deck = suits.product(faces)
deck = deck.map do |pair|
pair.join
end
Slide 86
Slide 86 text
building a deck of cards
suits = %w(S C H D)
faces = (2..10).to_a + %w(J Q K A)
deck = suits.product(faces)
deck.map! do |pair|
pair.join
end
Slide 87
Slide 87 text
building a deck of cards
.map!
Slide 88
Slide 88 text
building a deck of cards
suits = %w(S C H D)
faces = (2..10).to_a + %w(J Q K A)
deck = suits.product(faces)
deck.map! do |pair|
pair.join
end
Slide 89
Slide 89 text
building a deck of cards
suits = %w(S C H D)
faces = (2..10).to_a + %w(J Q K A)
deck = suits.product(faces)
deck.map! { |pair| pair.join }
Slide 90
Slide 90 text
building a deck of cards
suits = %w(S C H D)
faces = (2..10).to_a + %w(J Q K A)
deck = suits.product(faces).map(&:join)
Slide 91
Slide 91 text
generating poker hands
suits = %w(S C H D)
faces = (2..10).to_a + %w(J Q K A)
deck = suits.product(faces).map(&:join)
Slide 92
Slide 92 text
sample(n) -> new_ary
Choose n random elements from the array.
The elements are chosen by using random
and unique indices in order to ensure that an
element doesn’t repeat itself unless the array
already contained duplicate elements.
Slide 93
Slide 93 text
generating poker hands
suits = %w(S C H D)
faces = (2..10).to_a + %w(J Q K A)
deck = suits.product(faces).map(&:join)
deck.sample(5)
Slide 94
Slide 94 text
generating poker hands
suits = %w(S C H D)
faces = (2..10).to_a + %w(J Q K A)
deck = suits.product(faces).map(&:join)
deck.sample(5)
=> [“C2”, “D5”, “S7”, “D8”, “C8”]
Count of URL Access Frequency
The map function processes logs of web page
requests and outputs . The reduce
function adds together all values for the same
URL and emits a pair.
from Introduction to Parallel Programming and MapReduce (Google)
Count of URL Access Frequency
The map function processes logs of web page
requests and outputs . The reduce
function adds together all values for the same
URL and emits a pair.
from Introduction to Parallel Programming and MapReduce (Google)
Slide 101
Slide 101 text
generate count pairs
count_pairs = log.map do |url|
[url, 1]
end
Slide 102
Slide 102 text
Count of URL Access Frequency
The map function processes logs of web page
requests and outputs . The reduce
function adds together all values for the same URL
and emits a pair.
from Introduction to Parallel Programming and MapReduce (Google)
Slide 103
Slide 103 text
BUT FIRST
Slide 104
Slide 104 text
Collection of key-value
pairs.
Similar to an array, except
indexing is done via unique
keys.
Count of URL Access Frequency
The map function processes logs of web page
requests and outputs . The reduce
function adds together all values for the same URL
and emits a pair.
from Introduction to Parallel Programming and MapReduce (Google)
Slide 111
Slide 111 text
combining count pairs
count_pairs.reduce
Slide 112
Slide 112 text
combining count pairs
count_pairs.reduce({})
Slide 113
Slide 113 text
combining count pairs
count_pairs.reduce({}) do |hash, pair|
end
Slide 114
Slide 114 text
combining count pairs
count_pairs.reduce({}) do |hash, pair|
url, count = pair
end
Slide 115
Slide 115 text
combining count pairs
count_pairs.reduce({}) do |hash, pair|
url, count = pair
if hash.has_key? url
hash[url] += count
end
Slide 116
Slide 116 text
combining count pairs
count_pairs.reduce({}) do |hash, pair|
url, count = pair
if hash.has_key? url
hash[url] += count
else
hash[url] = count
end
end
Slide 117
Slide 117 text
combining count pairs
count_pairs.reduce({}) do |hash, pair|
url, count = pair
if hash.has_key? url
hash[url] += count
else
hash[url] = count
end
hash
end
Slide 118
Slide 118 text
counting url access frequency
log = [“example.com”, “google.com”,
“example.com”, “unb.ca”]