{"id":442,"date":"2009-06-10T16:13:51","date_gmt":"2009-06-10T06:13:51","guid":{"rendered":"http:\/\/spiffystores.com.au\/blog\/?p=442"},"modified":"2021-09-01T07:17:33","modified_gmt":"2021-08-31T21:17:33","slug":"multisets-and-bags-in-ruby","status":"publish","type":"post","link":"https:\/\/www.spiffystores.com.au\/blog\/2009\/06\/10\/multisets-and-bags-in-ruby\/","title":{"rendered":"Multisets and Bags in Ruby"},"content":{"rendered":"\n<p>I&#8217;ve been looking around for an implementation of a Multiset\/Bag in Ruby to ease the pain of recording some of our statistics.<\/p>\n\n\n\n<p>Some of the statistics we gather are most usefully stored as some form of &#8220;super&#8221; Set in which each unique element is stored together with a count of the number of times that element has occurred. These statistics are likely to have a large number of repeated elements, so this makes sense as it saves on space and processing.<\/p>\n\n\n\n<p>So, after an extensive search, I was surprised at the paucity of solutions for this problem.<\/p>\n\n\n\n<p>Eventually I hit upon<\/p>\n\n\n\n<p><a title=\"Multiset Library for Ruby\" href=\"http:\/\/maraigue.hhiro.net\/multiset\/index-en.php\" target=\"_blank\" rel=\"noopener\">http:\/\/maraigue.hhiro.net\/multiset\/index-en.php<\/a><\/p>\n\n\n\n<p>It does exactly what I want, but I decided that I would add the following custom functions. These two functions return all the items in a Multiset with the highest\/lowest counts.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby line-numbers\">class Multiset\n  # Return all the items with the maximum count\n  def max_values\n    max_value = @items.values.max\n    @items.select { |k, v| v == max_value }.map { |i| i[0] }\n  end\n\n  # Return all the items with the minimum count\n  def min_values\n    min_value = @items.values.min\n    @items.select { |k, v| v == min_value }.map { |i| i[0] }\n  end\nend<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been looking around for an implementation of a Multiset\/Bag in Ruby to ease the pain of recording some of our statistics. Some of the statistics we gather are most usefully stored as some form of &#8220;super&#8221; Set in which &hellip; <a href=\"https:\/\/www.spiffystores.com.au\/blog\/2009\/06\/10\/multisets-and-bags-in-ruby\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-442","post","type-post","status-publish","format-standard","hentry","category-geek-stuff"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pgk4DF-78","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/posts\/442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/comments?post=442"}],"version-history":[{"count":1,"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/posts\/442\/revisions"}],"predecessor-version":[{"id":3232,"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/posts\/442\/revisions\/3232"}],"wp:attachment":[{"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/media?parent=442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/categories?post=442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/tags?post=442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}