Skip to content

Instantly share code, notes, and snippets.

@kyontan

kyontan/atom_map.rb

Created Jun 13, 2018
Embed
What would you like to do?
# [1, 2].atom_map{|x| Integer === x ? x * 2 : x }
# => [2, 4]
#
# [1, 2, [3, 4]].atom_map{|x| Integer === x ? x * 2 : x }
# => [2, 4, [6, 8]]
#
# [1, 2, [3, {k: 4, v: 3}]].atom_map{|x| Integer === x ? x * 2 : x }
# => [2, 4, [6, {:k=>8, :v=>6}]]
#
module Kernel
def atom_map(&block)
return (yield self) unless self.respond_to? :map
map{|args| args.atom_map(&block) }.yield_self{|x| Hash === self ? x.to_h : x }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.