Sourcecode jetzt etwas besser!

Nach Installation des GeshiSyntaxColorer und Deaktivierung des erweiterten visuellen Editors scheint es jetzt besser zu funktionieren.

require 'pp'
 
class Hash
	def lookup_or_create(key,&creator)
		if (has_key?(key))
			return self[key]
		end
		element = creator.call
		self[key]=element
		return element
	end
end
 
def group(list, &category)
	map=Hash.new	
	list.each{|element|
		cat=category.call(element)
		grp = map.lookup_or_create(cat){
			Array.new
		}		
		grp< <(element);
	}
	return map
end
 
 
names = ["Felix","Alexey","Frank","Ivan","Irina"]
 
grouped_names = group(names){|element|   
  element[0..0]
}
 
pp grouped_names
 
# Expected Output:
# {"A"=>["Alexey"], "F"=>["Felix", "Frank"], "I"=>["Ivan", "Irina"]}

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.