<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Digest Tag Population in Ruby</title>
	<atom:link href="http://lojic.com/blog/2009/03/01/digest-tag-population-in-ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://lojic.com/blog/2009/03/01/digest-tag-population-in-ruby/</link>
	<description></description>
	<lastBuildDate>Sun, 25 Dec 2011 09:28:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Scott Moonen</title>
		<link>http://lojic.com/blog/2009/03/01/digest-tag-population-in-ruby/comment-page-1/#comment-6439</link>
		<dc:creator>Scott Moonen</dc:creator>
		<pubDate>Mon, 10 Aug 2009 11:25:32 +0000</pubDate>
		<guid isPermaLink="false">http://lojic.com/blog/?p=258#comment-6439</guid>
		<description>I&#039;ve posted two alternate Clojure solutions here: http://scott.andstuff.org/ClojureSpikes.  They take a slightly different approach from Raffael&#039;s above.  The high-level structure of my second solution is inspired by Raffael&#039;s second solution -- this is a helpful LISP idiom that I was glad to learn.</description>
		<content:encoded><![CDATA[<p>I&#8217;ve posted two alternate Clojure solutions here: <a href="http://scott.andstuff.org/ClojureSpikes" rel="nofollow">http://scott.andstuff.org/ClojureSpikes</a>.  They take a slightly different approach from Raffael&#8217;s above.  The high-level structure of my second solution is inspired by Raffael&#8217;s second solution &#8212; this is a helpful LISP idiom that I was glad to learn.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Adkins</title>
		<link>http://lojic.com/blog/2009/03/01/digest-tag-population-in-ruby/comment-page-1/#comment-2322</link>
		<dc:creator>Brian Adkins</dc:creator>
		<pubDate>Sat, 21 Mar 2009 15:07:11 +0000</pubDate>
		<guid isPermaLink="false">http://lojic.com/blog/?p=258#comment-2322</guid>
		<description>A Haskell version by Florian Kreidler (modifying a version by Greg Bacon) on comp.lang.haskell:
&lt;pre class=&quot;code&quot;&gt;
 import Data.List (sortBy, elemIndex)
 import Data.Ord(comparing)

 type Tag       = String
 type Pet       = (String, Int)
 type PetTag    = (Tag, [Pet])
 type TaggedPet = (Tag, Pet)

 pets :: PetTags
 pets =
   [ (&quot;dog&quot;,   [ (&quot;blab&quot;,    12)
               , (&quot;glab&quot;,    17)
               , (&quot;cbret&quot;,   82)
               , (&quot;dober&quot;,   42)
               , (&quot;gshep&quot;,   25)
               ])
   , (&quot;cat&quot;,   [ (&quot;pers&quot;,    22)
               , (&quot;siam&quot;,    7)
               , (&quot;tibet&quot;,   52)
               , (&quot;russ&quot;,    92)
               , (&quot;meow&quot;,    35)
               ])
   , (&quot;snake&quot;, [ (&quot;garter&quot;,  10)
               , (&quot;cobra&quot;,   37)
               , (&quot;python&quot;,  77)
               , (&quot;adder&quot;,   24)
               , (&quot;rattle&quot;,  40)
               ])
   , (&quot;cow&quot;,   [ (&quot;jersey&quot;,  200)
               , (&quot;heiffer&quot;, 300)
               , (&quot;moo&quot;,     400)
               ])
   ]

 digestTagPopulation :: [PetTag] -&gt; [Tag] -&gt; Int -&gt; [TaggedPet]
 digestTagPopulation tp tags c
     = sortBy (comparing $ flip elemIndex tags . fst) $
       take c $ sortBy (flip $ comparing $ snd . snd)
       [ (t, p) &#124; (t, ps) &lt;- tp, t `elem` tags, p &lt;- ps ]
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>A Haskell version by Florian Kreidler (modifying a version by Greg Bacon) on comp.lang.haskell:</p>
<pre class="code">
 import Data.List (sortBy, elemIndex)
 import Data.Ord(comparing)

 type Tag       = String
 type Pet       = (String, Int)
 type PetTag    = (Tag, [Pet])
 type TaggedPet = (Tag, Pet)

 pets :: PetTags
 pets =
   [ ("dog",   [ ("blab",    12)
               , ("glab",    17)
               , ("cbret",   82)
               , ("dober",   42)
               , ("gshep",   25)
               ])
   , ("cat",   [ ("pers",    22)
               , ("siam",    7)
               , ("tibet",   52)
               , ("russ",    92)
               , ("meow",    35)
               ])
   , ("snake", [ ("garter",  10)
               , ("cobra",   37)
               , ("python",  77)
               , ("adder",   24)
               , ("rattle",  40)
               ])
   , ("cow",   [ ("jersey",  200)
               , ("heiffer", 300)
               , ("moo",     400)
               ])
   ]

 digestTagPopulation :: [PetTag] -> [Tag] -> Int -> [TaggedPet]
 digestTagPopulation tp tags c
     = sortBy (comparing $ flip elemIndex tags . fst) $
       take c $ sortBy (flip $ comparing $ snd . snd)
       [ (t, p) | (t, ps) < - tp, t `elem` tags, p <- ps ]
</pre>
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Adkins</title>
		<link>http://lojic.com/blog/2009/03/01/digest-tag-population-in-ruby/comment-page-1/#comment-1910</link>
		<dc:creator>Brian Adkins</dc:creator>
		<pubDate>Tue, 03 Mar 2009 06:06:06 +0000</pubDate>
		<guid isPermaLink="false">http://lojic.com/blog/?p=258#comment-1910</guid>
		<description>Second version by Paul Rubin on comp.lang.haskell:

&lt;pre class=&quot;code&quot;&gt;
import Data.List
import Data.Ord

pets = [(&quot;dog&quot;, [(&quot;blab&quot;, 12),(&quot;glab&quot;, 17),(&quot;cbret&quot;, 82),
                 (&quot;dober&quot;, 42),(&quot;gshep&quot;, 25)]),
        (&quot;cat&quot;, [(&quot;pers&quot;, 22),(&quot;siam&quot;, 7),(&quot;tibet&quot;, 52),
                 (&quot;russ&quot;, 92),(&quot;meow&quot;, 35)]),
        (&quot;snake&quot;, [(&quot;garter&quot;, 10),(&quot;cobra&quot;, 37),(&quot;python&quot;, 77),
                   (&quot;adder&quot;, 24),(&quot;rattle&quot;, 40)]),
        (&quot;cow&quot;, [(&quot;jersey&quot;, 200),(&quot;heiffer&quot;, 300),(&quot;moo&quot;, 400)])]

digest_tag tag_pop pick_tags count =
    sortBy (comparing (\(a,_)-&gt;findIndex (==a) (map fst tag_pop)))
               (take count $ sortBy (flip (comparing (snd . snd)))
                         [(a,(b,c)) &#124; (a,bs) &lt;- tag_pop, a `elem` pick_tags,
                          (b,c) &lt;- bs])

main = print $ digest_tag pets [&quot;dog&quot;,&quot;cat&quot;,&quot;snake&quot;] 5
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Second version by Paul Rubin on comp.lang.haskell:</p>
<pre class="code">
import Data.List
import Data.Ord

pets = [("dog", [("blab", 12),("glab", 17),("cbret", 82),
                 ("dober", 42),("gshep", 25)]),
        ("cat", [("pers", 22),("siam", 7),("tibet", 52),
                 ("russ", 92),("meow", 35)]),
        ("snake", [("garter", 10),("cobra", 37),("python", 77),
                   ("adder", 24),("rattle", 40)]),
        ("cow", [("jersey", 200),("heiffer", 300),("moo", 400)])]

digest_tag tag_pop pick_tags count =
    sortBy (comparing (\(a,_)->findIndex (==a) (map fst tag_pop)))
               (take count $ sortBy (flip (comparing (snd . snd)))
                         [(a,(b,c)) | (a,bs) < - tag_pop, a `elem` pick_tags,
                          (b,c) <- bs])

main = print $ digest_tag pets ["dog","cat","snake"] 5
</pre>
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Adkins</title>
		<link>http://lojic.com/blog/2009/03/01/digest-tag-population-in-ruby/comment-page-1/#comment-1879</link>
		<dc:creator>Brian Adkins</dc:creator>
		<pubDate>Sun, 01 Mar 2009 19:50:22 +0000</pubDate>
		<guid isPermaLink="false">http://lojic.com/blog/?p=258#comment-1879</guid>
		<description>Two Clojure versions by Raffael Cavallaro on comp.lang.lisp:

Version 1:

&lt;pre class=&quot;code&quot;&gt;
(defn digest-tag-population [tag-population pick-tags count]
  (sort-by #(first (for [[idx elt]
			 (map vector (iterate inc 0) pick-tags)
			 :when (= elt (first%))] idx))
	   &lt; (take count (sort-by #(nth % 2) &gt;
				  (mapcat (fn [a-lst] (map #(cons (first a-lst) %)
							   (first (rest a-lst))))
					  (filter #(contains? (set pick-tags) (first %)) tag-population))))))
&lt;/pre&gt;

Version 2:

&lt;pre class=&quot;code&quot;&gt;
(defn digest-tag-population [tag-population pick-tags count]
  (let [selected (filter #(contains? (set pick-tags) (first %)) tag-population)
	union (mapcat (fn [a-lst]
			(map (fn [lst]
			       (cons (first a-lst) lst))
			     (first (rest a-lst))))
		      selected)
	pet-position (fn [petsym]
		       (first (for [[idx elt] (map vector (iterate inc 0) pick-tags) :when (= elt petsym)] idx)))
	pop-sorted (sort-by #(nth % 2) &gt; union)
	top-n (take count pop-sorted)
	pet-sorted (sort-by #(pet-position (first %)) &lt; top-n)]
    pet-sorted))
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Two Clojure versions by Raffael Cavallaro on comp.lang.lisp:</p>
<p>Version 1:</p>
<pre class="code">
(defn digest-tag-population [tag-population pick-tags count]
  (sort-by #(first (for [[idx elt]
			 (map vector (iterate inc 0) pick-tags)
			 :when (= elt (first%))] idx))
	   < (take count (sort-by #(nth % 2) >
				  (mapcat (fn [a-lst] (map #(cons (first a-lst) %)
							   (first (rest a-lst))))
					  (filter #(contains? (set pick-tags) (first %)) tag-population))))))
</pre>
<p>Version 2:</p>
<pre class="code">
(defn digest-tag-population [tag-population pick-tags count]
  (let [selected (filter #(contains? (set pick-tags) (first %)) tag-population)
	union (mapcat (fn [a-lst]
			(map (fn [lst]
			       (cons (first a-lst) lst))
			     (first (rest a-lst))))
		      selected)
	pet-position (fn [petsym]
		       (first (for [[idx elt] (map vector (iterate inc 0) pick-tags) :when (= elt petsym)] idx)))
	pop-sorted (sort-by #(nth % 2) > union)
	top-n (take count pop-sorted)
	pet-sorted (sort-by #(pet-position (first %)) < top-n)]
    pet-sorted))
</pre>
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Adkins</title>
		<link>http://lojic.com/blog/2009/03/01/digest-tag-population-in-ruby/comment-page-1/#comment-1878</link>
		<dc:creator>Brian Adkins</dc:creator>
		<pubDate>Sun, 01 Mar 2009 19:47:03 +0000</pubDate>
		<guid isPermaLink="false">http://lojic.com/blog/?p=258#comment-1878</guid>
		<description>A Python version by Ron Garret from comp.lang.lisp:

&lt;pre class=&quot;code&quot;&gt;
pets = {&#039;dog&#039; : &#039;blab 12, glab 17, cbret 82, dober 42, gshep 25&#039;,
        &#039;cat&#039; :   &#039;pers 22, siam 7, tibet 52, russ 92, meow 35&#039;,
        &#039;snake&#039; : &#039;garter 10, cobra 37, python 77, adder 24, rattle 40&#039;,
        &#039;cow&#039; : &#039;jersey 200, heiffer 300, moo 400&#039;}

for k in pets: pets[k] = [(k,tag,int(n)) for (tag,n) in  
                          [s.split() for s in pets[k].split(&#039;,&#039;)]]

def keycmp(f): return lambda x,y: cmp(f(x), f(y))

def dtp(tags, types, cnt):
  l = []
  for t in types: l.extend(pets[t])
  l.sort(keycmp(lambda x:-x[2]))
  l=l[:cnt]
  l.sort(keycmp(lambda x: types.index(x[0]))
  return l


&gt;&gt;&gt; dtp(pets, [&#039;dog&#039;,&#039;cat&#039;,&#039;snake&#039;], 5)
[(&#039;dog&#039;, &#039;cbret&#039;, 82), (&#039;dog&#039;, &#039;dober&#039;, 42), (&#039;cat&#039;, &#039;russ&#039;, 92), 
(&#039;cat&#039;, &#039;tibet&#039;, 52), (&#039;snake&#039;, &#039;python&#039;, 77)]
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>A Python version by Ron Garret from comp.lang.lisp:</p>
<pre class="code">
pets = {'dog' : 'blab 12, glab 17, cbret 82, dober 42, gshep 25',
        'cat' :   'pers 22, siam 7, tibet 52, russ 92, meow 35',
        'snake' : 'garter 10, cobra 37, python 77, adder 24, rattle 40',
        'cow' : 'jersey 200, heiffer 300, moo 400'}

for k in pets: pets[k] = [(k,tag,int(n)) for (tag,n) in
                          [s.split() for s in pets[k].split(',')]]

def keycmp(f): return lambda x,y: cmp(f(x), f(y))

def dtp(tags, types, cnt):
  l = []
  for t in types: l.extend(pets[t])
  l.sort(keycmp(lambda x:-x[2]))
  l=l[:cnt]
  l.sort(keycmp(lambda x: types.index(x[0]))
  return l

>>> dtp(pets, ['dog','cat','snake'], 5)
[('dog', 'cbret', 82), ('dog', 'dober', 42), ('cat', 'russ', 92),
('cat', 'tibet', 52), ('snake', 'python', 77)]
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Adkins</title>
		<link>http://lojic.com/blog/2009/03/01/digest-tag-population-in-ruby/comment-page-1/#comment-1877</link>
		<dc:creator>Brian Adkins</dc:creator>
		<pubDate>Sun, 01 Mar 2009 19:30:44 +0000</pubDate>
		<guid isPermaLink="false">http://lojic.com/blog/?p=258#comment-1877</guid>
		<description>A Haskell version by Paul Rubin from comp.lang.haskell:

&lt;pre class=&quot;code&quot;&gt;
import Data.List
import Data.Ord
import Control.Monad

pets =
   [(&quot;dog&quot;, [(&quot;blab&quot;, 12),(&quot;glab&quot;, 17),(&quot;cbret&quot;, 82),
             (&quot;dober&quot;, 42),(&quot;gshep&quot;, 25)]),
     (&quot;cat&quot;, [(&quot;pers&quot;, 22),(&quot;siam&quot;, 7),(&quot;tibet&quot;, 52),
              (&quot;russ&quot;, 92),(&quot;meow&quot;, 35)]),
     (&quot;snake&quot;, [(&quot;garter&quot;, 10),(&quot;cobra&quot;, 37),(&quot;python&quot;, 77),
                (&quot;adder&quot;, 24),(&quot;rattle&quot;, 40)]),
     (&quot;cow&quot;, [(&quot;jersey&quot;, 200),(&quot;heiffer&quot;, 300),(&quot;moo&quot;, 400)])]

digest_tag tag_pop pick_tags count =
  let selected_pops = 
        [(i,(-c,b)) &#124; (i,(a,bs)) &lt;- zip [0..] tag_pop, a `elem` pick_tags,
                                    (b,c) &lt;- bs]
      top_pops = sort $ take count (sortBy (comparing snd) selected_pops)
  in [(fst (tag_pop !! i), b, -c) &#124; (i, (c,b)) &lt;- top_pops]

main = print $ digest_tag pets [&quot;dog&quot;,&quot;cat&quot;,&quot;snake&quot;] 5
&lt;/pre&gt;
</description>
		<content:encoded><![CDATA[<p>A Haskell version by Paul Rubin from comp.lang.haskell:</p>
<pre class="code">
import Data.List
import Data.Ord
import Control.Monad

pets =
   [("dog", [("blab", 12),("glab", 17),("cbret", 82),
             ("dober", 42),("gshep", 25)]),
     ("cat", [("pers", 22),("siam", 7),("tibet", 52),
              ("russ", 92),("meow", 35)]),
     ("snake", [("garter", 10),("cobra", 37),("python", 77),
                ("adder", 24),("rattle", 40)]),
     ("cow", [("jersey", 200),("heiffer", 300),("moo", 400)])]

digest_tag tag_pop pick_tags count =
  let selected_pops =
        [(i,(-c,b)) | (i,(a,bs)) < - zip [0..] tag_pop, a `elem` pick_tags,
                                    (b,c) <- bs]
      top_pops = sort $ take count (sortBy (comparing snd) selected_pops)
  in [(fst (tag_pop !! i), b, -c) | (i, (c,b)) <- top_pops]

main = print $ digest_tag pets ["dog","cat","snake"] 5
</pre>
</pre>
]]></content:encoded>
	</item>
</channel>
</rss>

