by markov_twain · 13y ago · 0 comments
p ->(*_) {
_.reduce({}, :merge).tap { |_| return "<none>" if _.empty? }.keys.uniq.map(&:to_s).sort.join(", ")
}.({c:3,d:4,b:2}, {f:5,e:4,a:1})
p ->(*_) {
_.reduce({}, :merge).tap { |_| return "<none>" if _.empty? }.keys.uniq.map(&:to_s).sort.join(", ")
}.({}, {})
# "a, b, c, d, e, f"
# "<none>"
edit: a little bit better. func = ->(*_) {
return "<none>" if _.all?(&:empty?)
_.flat_map(&:keys).uniq.map(&:to_s).sort.join(", ")
}
func.({c:3,d:4,b:2}, {f:5,e:4,a:1})
#=> "a, b, c, d, e, f"
func.({}, {})
#=> "<none>"
https://gist.github.com/66b45e765a2aa6a97143 def unique_keys(*hashes)
result = {}
hashes.each do |hash|
result.merge! hash
end
return result.keys.map(&:to_s).sort.join(', ') unless result.empty?
'<none>'
end
https://gist.github.com/4695388I reckon what makes his first example somewhat unidiomatic is the arbitrary lambda and the use & inner return from the #taps block.
His updated example is just good ol tacit Ruby. But then again the amount of times I see an #each block with inner logic used instead of a simple point-free one-liner in Ruby really just proves your point.
(map1.keys | map2.keys).map(&:to_s).sort.join(", ").instance_eval { empty? ? "<none>" : self }a = {"fred": 4, "wilma": 8} b = {"t-shirt": 6, "fred": 0}
print ", ".join(sorted(set(a).union(b))) or "<none>"