<?xml version="1.0" encoding="utf-8"?><!-- generator="wordpress/2.1.1" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: More factor: tabular to triples</title>
	<link>http://phildawes.net/blog/2007/10/11/more-factor-tabular-to-triples/</link>
	<description>Mostly programming with a few bits of other stuff</description>
	<pubDate>Sun, 05 Jul 2009 01:36:20 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.1.1</generator>

	<item>
		<title>By: Neil Bartlett</title>
		<link>http://phildawes.net/blog/2007/10/11/more-factor-tabular-to-triples/#comment-48193</link>
		<author>Neil Bartlett</author>
		<pubDate>Thu, 11 Oct 2007 21:58:48 +0000</pubDate>
		<guid>http://phildawes.net/blog/2007/10/11/more-factor-tabular-to-triples/#comment-48193</guid>
					<description>Just for a laugh, here it is in Haskell. You can type this at the interpreter.

let insertList x = map (insert x) where insert x (y,z) = (x,y,z)

let cols = ["col1","col2","col3"]

let rows = [["a","b","c"],["e","f","g"]]

concat $ zipWith insertList [0..] $ map (zip cols) rows

This prints [(0,"col1","a"),(0,"col2","b"),(0,"col3","c"),(1,"col1","e"),(1,"col2","f"),(1,"col3","g")]

No preview available so I don't know if your blog software is going to mangle that!</description>
		<content:encoded><![CDATA[<p>Just for a laugh, here it is in Haskell. You can type this at the interpreter.</p>
<p>let insertList x = map (insert x) where insert x (y,z) = (x,y,z)</p>
<p>let cols = [&#8221;col1&#8243;,&#8221;col2&#8243;,&#8221;col3&#8243;]</p>
<p>let rows = [[&#8221;a&#8221;,&#8221;b&#8221;,&#8221;c&#8221;],[&#8221;e&#8221;,&#8221;f&#8221;,&#8221;g&#8221;]]</p>
<p>concat $ zipWith insertList [0..] $ map (zip cols) rows</p>
<p>This prints [(0,&#8221;col1&#8243;,&#8221;a&#8221;),(0,&#8221;col2&#8243;,&#8221;b&#8221;),(0,&#8221;col3&#8243;,&#8221;c&#8221;),(1,&#8221;col1&#8243;,&#8221;e&#8221;),(1,&#8221;col2&#8243;,&#8221;f&#8221;),(1,&#8221;col3&#8243;,&#8221;g&#8221;)]</p>
<p>No preview available so I don&#8217;t know if your blog software is going to mangle that!</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: paddy3118</title>
		<link>http://phildawes.net/blog/2007/10/11/more-factor-tabular-to-triples/#comment-48258</link>
		<author>paddy3118</author>
		<pubDate>Fri, 12 Oct 2007 04:55:43 +0000</pubDate>
		<guid>http://phildawes.net/blog/2007/10/11/more-factor-tabular-to-triples/#comment-48258</guid>
					<description>I thought from the input format and the output that a Python list comprehension would be the way I'd solve it and came up with this:

&#62;&#62;&#62; from pprint import pprint as pp
&#62;&#62;&#62; tabular = [["col1","col2","col3"],[["a","b","c"],["e","f","g"]]]
&#62;&#62;&#62; triples = [(row[0], col[1], row[1][col[0]])  
  for row in enumerate(tabular[1]) 
  for col in enumerate(tabular[0])]
&#62;&#62;&#62; pp(triples)
[(0, 'col1', 'a'),
 (0, 'col2', 'b'),
 (0, 'col3', 'c'),
 (1, 'col1', 'e'),
 (1, 'col2', 'f'),
 (1, 'col3', 'g')]
&#62;&#62;&#62; 


(I added a couple of newlines to the list comprehension to get it into the comment box).

If I were using it then I might wrap it in a function and add a docstring to test/explain it.

- Paddy.</description>
		<content:encoded><![CDATA[<p>I thought from the input format and the output that a Python list comprehension would be the way I&#8217;d solve it and came up with this:</p>
<p>&gt;&gt;&gt; from pprint import pprint as pp<br />
&gt;&gt;&gt; tabular = [[&#8221;col1&#8243;,&#8221;col2&#8243;,&#8221;col3&#8243;],[[&#8221;a&#8221;,&#8221;b&#8221;,&#8221;c&#8221;],[&#8221;e&#8221;,&#8221;f&#8221;,&#8221;g&#8221;]]]<br />
&gt;&gt;&gt; triples = [(row[0], col[1], row[1][col[0]])<br />
  for row in enumerate(tabular[1])<br />
  for col in enumerate(tabular[0])]<br />
&gt;&gt;&gt; pp(triples)<br />
[(0, &#8216;col1&#8242;, &#8216;a&#8217;),<br />
 (0, &#8216;col2&#8242;, &#8216;b&#8217;),<br />
 (0, &#8216;col3&#8242;, &#8216;c&#8217;),<br />
 (1, &#8216;col1&#8242;, &#8216;e&#8217;),<br />
 (1, &#8216;col2&#8242;, &#8216;f&#8217;),<br />
 (1, &#8216;col3&#8242;, &#8216;g&#8217;)]<br />
&gt;&gt;&gt; </p>
<p>(I added a couple of newlines to the list comprehension to get it into the comment box).</p>
<p>If I were using it then I might wrap it in a function and add a docstring to test/explain it.</p>
<p>- Paddy.</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Christopher Diggins</title>
		<link>http://phildawes.net/blog/2007/10/11/more-factor-tabular-to-triples/#comment-48325</link>
		<author>Christopher Diggins</author>
		<pubDate>Fri, 12 Oct 2007 20:21:52 +0000</pubDate>
		<guid>http://phildawes.net/blog/2007/10/11/more-factor-tabular-to-triples/#comment-48325</guid>
					<description>Hi Phil,

Small typo in the first sentence: "I’ve been playing with  for a couple of weeks.", I believe you meant "factor" instead of "for".

If you are interested in concatenative languages and the semantic web have you seen Ripple? http://ripple.fortytwo.net/

There is also my own relatively immature language Cat (http://www.cat-language.com), which will be stable "real soon now".

Cheers,
Christopher Diggins</description>
		<content:encoded><![CDATA[<p>Hi Phil,</p>
<p>Small typo in the first sentence: &#8220;I’ve been playing with  for a couple of weeks.&#8221;, I believe you meant &#8220;factor&#8221; instead of &#8220;for&#8221;.</p>
<p>If you are interested in concatenative languages and the semantic web have you seen Ripple? <a href="http://ripple.fortytwo.net/" rel="nofollow">http://ripple.fortytwo.net/</a></p>
<p>There is also my own relatively immature language Cat (http://www.cat-language.com), which will be stable &#8220;real soon now&#8221;.</p>
<p>Cheers,<br />
Christopher Diggins</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Phil Dawes</title>
		<link>http://phildawes.net/blog/2007/10/11/more-factor-tabular-to-triples/#comment-48394</link>
		<author>Phil Dawes</author>
		<pubDate>Sat, 13 Oct 2007 19:32:33 +0000</pubDate>
		<guid>http://phildawes.net/blog/2007/10/11/more-factor-tabular-to-triples/#comment-48394</guid>
					<description>Thanks Christopher - it was a missing close brace in the factor link preventing it from being displayed.
I'll definitely check out both Cat and ripple - thanks for the links</description>
		<content:encoded><![CDATA[<p>Thanks Christopher - it was a missing close brace in the factor link preventing it from being displayed.<br />
I&#8217;ll definitely check out both Cat and ripple - thanks for the links</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Slava Pestov</title>
		<link>http://phildawes.net/blog/2007/10/11/more-factor-tabular-to-triples/#comment-48627</link>
		<author>Slava Pestov</author>
		<pubDate>Mon, 15 Oct 2007 18:56:26 +0000</pubDate>
		<guid>http://phildawes.net/blog/2007/10/11/more-factor-tabular-to-triples/#comment-48627</guid>
					<description>Hi,

I had a go at implementing this. The key, as you've noticed, is to decompose the problem into steps where you're working with no more than a handful of values at once.

Don'write a big loop, instead break it up into multiple iterations over the data and use library words where possible.

USE: math.ranges

: (column-names) ( cols row -- pairs )
    2array flip ;

: column-names ( cols rows -- seqs-of-pairs )
    [ (column-names) ] curry* map ;

: (number-rows) ( pairs n -- triples )
    [ add* ] curry map ;

: number-rows ( seqs-of-pairs -- triples )
    tuck length [a,b] [ (number-rows) ] 2map concat ;

: tabular&#62;triples ( start-rowid cols rows -- triples )
    column-names number-rows ;</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I had a go at implementing this. The key, as you&#8217;ve noticed, is to decompose the problem into steps where you&#8217;re working with no more than a handful of values at once.</p>
<p>Don&#8217;write a big loop, instead break it up into multiple iterations over the data and use library words where possible.</p>
<p>USE: math.ranges</p>
<p>: (column-names) ( cols row &#8212; pairs )<br />
    2array flip ;</p>
<p>: column-names ( cols rows &#8212; seqs-of-pairs )<br />
    [ (column-names) ] curry* map ;</p>
<p>: (number-rows) ( pairs n &#8212; triples )<br />
    [ add* ] curry map ;</p>
<p>: number-rows ( seqs-of-pairs &#8212; triples )<br />
    tuck length [a,b] [ (number-rows) ] 2map concat ;</p>
<p>: tabular&gt;triples ( start-rowid cols rows &#8212; triples )<br />
    column-names number-rows ;</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Greg M</title>
		<link>http://phildawes.net/blog/2007/10/11/more-factor-tabular-to-triples/#comment-48670</link>
		<author>Greg M</author>
		<pubDate>Tue, 16 Oct 2007 03:01:39 +0000</pubDate>
		<guid>http://phildawes.net/blog/2007/10/11/more-factor-tabular-to-triples/#comment-48670</guid>
					<description>I prefer the Haskell list comprehension version:

[(i,col,cell)&#124;(i,row)&#60;-zip [startid..] rows, (col,cell) &#60;- zip cols row]

Now I wonder how this will get formatted? Need a preview button.</description>
		<content:encoded><![CDATA[<p>I prefer the Haskell list comprehension version:</p>
<p>[(i,col,cell)|(i,row)&lt;-zip [startid..] rows, (col,cell) &lt;- zip cols row]</p>
<p>Now I wonder how this will get formatted? Need a preview button.</p>
]]></content:encoded>
				</item>
</channel>
</rss>
