Skip to content

Commit e16bb47

Browse files
committed
Notes for February 26, 2015
1 parent a78dc81 commit e16bb47

File tree

3 files changed

+228
-0
lines changed

3 files changed

+228
-0
lines changed

session-notes/2015-02-26/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Session Notes: Web Fundamentals
2+
## Thursday, February 26, 2015
3+
4+
## Questions!!!
5+
6+
- `@current_user` in authentication example
7+
8+
- Best ways to implement searching
9+
10+
- reduce and inject, what's up with that?
11+
Enumerable methods!!!!! yay!!!!!
12+
13+
- Accordion lists w/ JavaScript (or any way)

session-notes/2015-02-26/enum.rb

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
# (0...50).map { ('a'..'z').to_a[rand(26)] }.join
2+
3+
# map and collect are synonyms (they are aliases)
4+
# inject and reduce are synonyms (they are aliases)
5+
# array = [2, 4, 6, 8]
6+
# # puts array.inject { |x, y| x + y } # 2 + 4 + 6 + 8
7+
# product = array.reduce do |x, y|
8+
# p [x, y]
9+
# x * y
10+
# end # 2 * 4 * 6 * 8
11+
12+
# map, select, min, max, min_by, max_by
13+
# any?, all?, #reject, none?, one?, zip
14+
15+
# words = ["hello", "bananas", "apples", "hello", "bananas"]
16+
# words.map { |word| word.reverse }
17+
# words.inject([]) { |results, word| results << word.reverse }
18+
#
19+
# numbers.all? { |num| num.even? }
20+
# numbers.inject(true) { |results, num| results && num.even? }
21+
#
22+
# numbers.select { |num| num.even? }
23+
# numbers.inject([]) do |results, num|
24+
# if num.even?
25+
# results << num
26+
# else
27+
# results
28+
# end
29+
# end
30+
#
31+
# def frequency1(data)
32+
# results = Hash.new(0)
33+
#
34+
# data.each do |item|
35+
# results[item] += 1
36+
# end
37+
#
38+
# results
39+
# end
40+
#
41+
# def frequency2(data)
42+
# data.inject(Hash.new(0)) do |results, item|
43+
# results.update(item => results[item] + 1)
44+
# end
45+
# end
46+
#
47+
# data = ["hello", "bananas", "apples", "hello", "bananas"]
48+
#
49+
# # Bet 1: {"hello" => 3, "apples" => 1}
50+
# # Bet 2: nil
51+
# # Bet 3: {"bananas" => 1}
52+
# p frequency2(data)
53+
54+
# def capitalize_word(word)
55+
# word.capitalize
56+
# end
57+
#
58+
# paragraph = "Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal."
59+
# puts paragraph
60+
# puts "---"
61+
#
62+
# capitalized_paragraph = paragraph.split(" ").map do |word|
63+
# capitalize_word(word)
64+
# end.join(" ")
65+
#
66+
# puts capitalized_paragraph
67+
# map is used to apply a function to every element in a collection,
68+
# resulting in a new list with that function applied to each element
69+
# map(f, [a1, a2, a3, ..., aN]) = [f(a1), f(a2), ..., f(aN)]
70+
#
71+
# # User comes in, String goes out
72+
# def first_name(user)
73+
# user.first_name
74+
# end
75+
#
76+
# map(first_name, [user1, user2, ..., userN])
77+
# = [first_name(user1), first_name(user2), ..., first_name(userN)]
78+
# = [user1.first_name, user2.first_name, ..., userN.first_name]
79+
#
80+
#
81+
82+
def each_with_index(list, &block)
83+
i = 0
84+
85+
while i < list.length
86+
block.call(list[i], i)
87+
i += 1
88+
end
89+
90+
list
91+
end
92+
93+
def each(list, &block)
94+
each_with_index(list) do |item, i|
95+
block.call(item)
96+
end
97+
end
98+
99+
# reduce([5, 10, 15], 0) { |x, y| x + y } == 30
100+
def reduce(list, memo, &block)
101+
each(list) do |item|
102+
memo = block.call(memo, item)
103+
end
104+
105+
memo
106+
end
107+
108+
# map([5, 10, 15]) { |n| n**2 } == [25, 100, 225]
109+
def map(list, &block)
110+
reduce(list, []) do |memo, item|
111+
memo.push(block.call(item))
112+
end
113+
end
114+
115+
# select([1,2,3,4,5]) { |n| n.even? } == [2, 4]
116+
def select(list, &block)
117+
reduce(list, []) do |memo, item|
118+
block.call(item) ? memo.push(item) : memo
119+
end
120+
end
121+
122+
# reject([1,2,3,4,5]) { |n| n.even? } == [1, 3, 5]
123+
def reject(list, &block)
124+
select(list) do |item|
125+
!block.call(item)
126+
end
127+
end
128+
129+
# index([45, 19, 23, 16, 31]) { |n| n.even? } == 3
130+
def index(list, &block)
131+
reduce(list, [0, nil]) do |(i, memo), item|
132+
if memo
133+
[i + 1, memo]
134+
elsif block.call(item)
135+
[i + 1, i]
136+
else
137+
[i + 1, nil]
138+
end
139+
end.last
140+
end
141+
142+
# any?([3, 5, 7]) { |n| n.even? } == false
143+
# any?([2, 5, 7]) { |n| n.even? } == false
144+
def any?(list, &block)
145+
reduce(list, false) do |memo, item|
146+
memo || block.call(item)
147+
end
148+
end
149+
150+
# all?([3, 5, 7]).all? { |n| n.odd? } == true
151+
# all?([2, 5, 7]).all? { |n| n.odd? } == false
152+
def all?(list, &block)
153+
reduce(list, true) do |memo, item|
154+
memo && block.call(item)
155+
end
156+
end
157+
158+
def none?(list, &block)
159+
!any?(list, &block)
160+
end
161+
162+
# count(["a", "bb", "ccc"]) { |str| str.length > 1 } == 2
163+
def count(list, &block)
164+
reduce(list, 0) do |memo, item|
165+
block.call(item) ? memo + 1 : memo
166+
end
167+
end
168+
169+
def max(list)
170+
reduce(list, list.first) do |memo, item|
171+
item > memo ? item : memo
172+
end
173+
end
174+
175+
# max_by(["a", "ccc", "bb"]) { |str| str.length } == "ccc"
176+
def max_by(list, &block)
177+
reduce(list, list.first) do |memo, item|
178+
block.call(item) > block.call(memo) ? item : memo
179+
end
180+
end
181+
182+
def min(list)
183+
reduce(list, list.first) do |memo, item|
184+
item < memo ? item : memo
185+
end
186+
end
187+
188+
# min_by(["a", "ccc", "bb"]) { |str| str.length } == "a"
189+
def min_by(list, &block)
190+
reduce(list, list.first) do |memo, item|
191+
block.call(item) < block.call(memo) ? item : memo
192+
end
193+
end
194+
195+
# group_by(["a", "bb", "ccc", "ddd"]) { |str| str.length }
196+
# == {1 => ["a"], 2 => ["bb"], 3 => ["ccc", "ddd"]}
197+
def group_by(list, &block)
198+
reduce(list, Hash.new { [] }) do |memo, item|
199+
key = block.call(item)
200+
memo.update(key => memo[key].push(item))
201+
end
202+
end
203+
204+
# partition([1,2,3,4,5]) { |n| n.even? }
205+
# == [[2, 4], [1, 3, 5]]
206+
def partition(list, &block)
207+
reduce(list, [[], []]) do |memo, item|
208+
if block.call(item)
209+
[memo[0].push(item), memo[1]]
210+
else
211+
[memo[0], memo[1].push(item)]
212+
end
213+
end
214+
end

session-notes/2015-02-26/scratch.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)