-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflatmap.rb
55 lines (50 loc) · 1.39 KB
/
flatmap.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
module HashFmap
refine Hash do
def fmap &block
self.reduce({}) do |memo, (k,v)|
memo[k] = block.call(v) ; memo
end
end
def fmap_keys! &block
keys_count = self.keys.count
new_hash = self.reduce({}) do |memo, (k,v)|
memo[block.call(k)] = v ; memo
end
new_keys_count = new_hash.keys.count
raise "transformation block lost keys (#{keys_count - new_keys_count} too few)" if keys_count != new_keys_count
new_hash
end
def fmap_keys &block
self.reduce({}) do |memo, (k,v)|
transformed_key = block.call(k)
if memo.keys.include?(transformed_key)
if transformed_key.is_a? Symbol
transformed_key = "#{transformed_key}2".to_sym
else
transformed_key = "#{transformed_key}2"
end
end
memo[transformed_key] = v ; memo
end
end
end
end
using HashFmap
hash = {foo: "bar", "foo" => :bar}
other_hash = {foo: "bar", crunchy: "bacon"}
p "starting with: hash"
p hash
p "and: other_hash"
p other_hash
p "Hash#fmap"
p hash.fmap {|v| v.upcase }
begin
p "Hash#fmap_keys! (with duplicate post-processed keys)"
p hash.fmap_keys! {|k| k.to_s.upcase.to_sym }
rescue => e
p "#{e}"
end
p "Hash#fmap_keys! (with unique post-processed keys)"
p other_hash.fmap_keys! {|k| k.to_s.upcase.to_sym }
p "Hash#fmap_keys"
p hash.fmap_keys {|k| k.to_s.upcase.to_sym }