<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>MettaProgramming &#187; context</title>
	<atom:link href="http://mettadore.com/tag/context/feed/" rel="self" type="application/rss+xml" />
	<link>http://mettadore.com</link>
	<description>Thoughts on Software and Technology</description>
	<lastBuildDate>Fri, 03 Feb 2012 17:39:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Ruby Blocks: seeking closure</title>
		<link>http://mettadore.com/ruby/ruby-blocks-seeking-closure/</link>
		<comments>http://mettadore.com/ruby/ruby-blocks-seeking-closure/#comments</comments>
		<pubDate>Mon, 15 Mar 2010 00:38:09 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[block]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[context]]></category>
		<category><![CDATA[scope]]></category>

		<guid isPermaLink="false">http://mettadore.com/?p=354</guid>
		<description><![CDATA[Lately, I&#8217;ve been quickly wrapping my head around Ruby&#8211; that seductive language that is quickly becoming the very reason I want to get out of bed in the morning. In my attempt to grok Ruby Blocks, I started writing this sort of &#8220;if you can teach it, then you know it&#8221; series called Block Party. [...]]]></description>
			<content:encoded><![CDATA[<p>Lately, I&#8217;ve been quickly wrapping my head around Ruby&#8211; that seductive language that is quickly becoming the very reason I want to get out of bed in the morning. In my attempt to grok Ruby Blocks, I started writing this sort of &#8220;if you can teach it, then you know it&#8221; series called Block Party. As such, it might help someone who stops by to read it, but it&#8217;s fundamentally written to help me.</p>
<p>Because I&#8217;m a long-time Pythonista, I decided to come to Ruby blocks from a Python perspective, and discussed Python&#8217;s Lambda construct in <a href="http://mettadore.com/ruby/lambdas-a-python-example/">the last post</a> in this series. In this one, I dive full on into a basic discussion of Ruby blocks, continuing where that post left off.</p>
<h3>We all need closure</h3>
<p>Time for as little Computer Science as possible. Python lambdas and Ruby blocks are implementations of a construct called <a href="http://en.wikipedia.org/wiki/Closure_(computer_science)">a Closure</a>. There&#8217;s a great deal to be said about what this actually means, but the name itself hints at all we <em>really</em> need to know about the warm and chewy parts.</p>
<p>In a &#8220;boil it down so you can explain it to an 8th grader&#8221; way a closure is this: <em>a little piece of something that will wrap itself around something else.</em> Like a blanket. A blanket can be a closure. I take a blanket, send it an 8th grader, and the blanket can wrap itself around the 8th grader and do what it does, keep them warm.<sup><a href="http://mettadore.com/ruby/ruby-blocks-seeking-closure/#footnote_0_354" id="identifier_0_354" class="footnote-link footnote-identifier-link" title="Well, technically, the 8th grader would wrap the blanket around themselves. Maybe not the most appropriate analogy.">1</a></sup></p>
<p>Okay, examples. Here&#8217;s one:</p>
<pre class="brush: ruby; title: ; notranslate">
def tuck_in
  kids.each_one do |kid|
    blanket.wrap(kid)
  end
end
</pre>
<p>Simple right? We make a little &#8220;tucking in&#8221; method. For each kid in our list of kids, wrap each one in a blanket.</p>
<p>Wait! That&#8217;s just a loop!</p>
<p>Well, almost. Technically, it&#8217;s a <em>closure</em>. Let&#8217;s go back to Python for a second to compare the two:</p>
<pre class="brush: python; title: ; notranslate">
def tuck_in:
  for kid in kids:
    blanket.wrap(kid)
</pre>
<p>Same thing, right? Wrong.</p>
<p>Our Python method actually calls the kid and makes the kid come to us to get wrapped up. It&#8217;s saying &#8220;call each kid, bring them here, wrap them in a blanket.&#8221;</p>
<p>The Ruby method is different. It actually <em>goes to the kids</em>. The Ruby method says &#8220;Take this &#8216;blanket wrap&#8217; idea, and send it to each of the kids individually.&#8221;</p>
<p>What the hell am I talking about? Patience, young padawan.</p>
<h3>A Question of Scope</h3>
<p>In order for us to understand what I&#8217;m talking about, we need to do a bit of implementation. Let&#8217;s implement our &#8220;kids&#8221; type. Technically, kids is just a list of all the kids we have, we don&#8217;t really need to implement an array to do this, but it&#8217;ll explain a lot:</p>
<pre class="brush: ruby; title: ; notranslate">
class Kids &lt; Array
  def each_one
    self.each_with_index do |n, i|
      yield(n)
    end
  end
end

kids = Kids.new

%w(Wendy Michael John).each{|n| kids &lt;&lt; n}

kids.each_one do |n|
 p &quot;Tucking in #{n}&quot;
end
</pre>
<p>Okay, what are we doing? Well, it&#8217;s pretty silly and contrived, but good for an example.</p>
<p>We created a subclass of Array with a special iterator. That iterator has a &#8216;yield&#8217; call, which makes it a block. After doing this, we grabbed an instance of our new class, populated it with some kids, and then for each kid, we tucked them in. Output?</p>
<pre class="brush: ruby; title: ; notranslate">

&quot;Tucking in Wendy&quot;
&quot;Tucking in Michael&quot;
&quot;Tucking in John&quot;
</pre>
<p>Simple. But wait, there&#8217;s more!</p>
<p>This is where the idea of context comes in. Let&#8217;s say that, everytime we tuck a kid in, we want to tell them the name of the <em>last</em> kid we tucked in. &#8220;I just tucked in Wendy, so I&#8217;m going to tuck you in now.&#8221; Let&#8217;s try it:</p>
<pre class="brush: ruby; title: ; notranslate">
last_kid = nil

kids.each_one do |n|
 p &quot;just tucked in #{last_kid}&quot;
 p &quot;Tucking in #{n}&quot;
 last_kid = n
end
</pre>
<p>Of course, this does what we want, but it&#8217;s not elegant. The issue is that we have to store the context of the iterator in a secondary variable and futz around with it. Basically, we&#8217;re required to store the last child in this scope, even though that information may not be appropriate for this scope.</p>
<p>This is why Ruby blocks are so sweet. Because with a block, we&#8217;re not calling the kids and telling them to come <em>to us</em>, we&#8217;re sending the tucking in method <em>to the kids</em>. Because of this, you don&#8217;t need to store the name of the last kid locally.</p>
<p>Check out this awesome sauce (hint: It&#8217;s the same as above, with two extra print statements):</p>
<pre class="brush: ruby; title: ; notranslate">
class Kids &lt; Array
  def each_one
    self.each_with_index do |n, i|
      yield(n)
      p &quot;Going now to tuck in #{self[i+1]}&quot; if i &lt; (self.length - 1)
      p &quot;Everyone all snuggled up!&quot; if i == (self.length - 1)
    end
  end
end

kids = Kids.new

%w(Wendy Michael John).each{|n| kids &lt;&lt; n}

kids.each_one do |n|
 p &quot;Tucking in #{n}&quot;
end
</pre>
<p>Okay, output?</p>
<pre class="brush: ruby; title: ; notranslate">
&quot;Tucking in Wendy&quot;
&quot;Going now to tuck in Michael&quot;
&quot;Tucking in Michael&quot;
&quot;Going now to tuck in John&quot;
&quot;Tucking in John&quot;
&quot;Everyone all snuggled up!&quot;
</pre>
<h3>Everything in its place</h3>
<p>The reason this is so awesome is that in our first attempt at remembering the last child&#8217;s name, we had to figure out a way to store it locally, even though that information is out of the local context. Furthermore, consider how difficult it would be to figure out the name of the <em>next</em> child.</p>
<p>With Ruby blocks, the logic we send is contained <em>within the context of the enclosing construct</em>. In otherwords, the &#8220;tucking in&#8221; call is not local to the toplevel, it&#8217;s local to the Kids::each_one method&#8211; in otherwords, it&#8217;s local to Kids.</p>
<p>It&#8217;s a bit to wrap your head around, but the basic story is that we are trying to keep everything in its place. We don&#8217;t want to create temporary variables to hack out a way to track which kid we just came from, or which kid we&#8217;re going to. That&#8217;s not elegant, and it&#8217;s even error prone. Rather, we want that information tracked within the appropriate scope, that way, we keep things nice and simple on the outside, and warm and chewy on the inside. With this kind of construct, we can safely do anything we want to the kids, and know that figuring out who the next kid is, or who the last kid was, can be taken care of. It minimizes our code, simplifies it, and keeps the logic separated in its appropriate place.</p>
<p>In the next post, my plan is to return to the Stream Reach example of the previous post to show, with an even more complex example, why this is such a beautiful thing.</p>
<ol class="footnotes"><li id="footnote_0_354" class="footnote">Well, technically, the 8th grader would wrap the blanket around <em>themselves</em>. Maybe not the most appropriate analogy.</li></ol>]]></content:encoded>
			<wfw:commentRss>http://mettadore.com/ruby/ruby-blocks-seeking-closure/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Tweetscore: Contextual Scoring</title>
		<link>http://mettadore.com/ruby/tweetscore-data-analysis-for-fun/</link>
		<comments>http://mettadore.com/ruby/tweetscore-data-analysis-for-fun/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 22:12:40 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[context]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[game]]></category>

		<guid isPermaLink="false">http://mettadore.com/?p=174</guid>
		<description><![CDATA[It's not Shakespeare, but you at least get SOMETHING when one million monkeys type all day on one million iPhones!]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-175" src="http://mettadore.com/files/2010/02/tweetscore.jpg" alt="tweetscore" width="300" height="174" />Twitter is not exactly the end-all-be-all of intelligent conversations. While I used it regularly to connect with colleagues and organize projects and events, I still have to admit that the <em>vast</em> majority of traffic is blubber.</p>
<p>That said, there are some interesting aspects of Twitter for those interested in data analysis. Tweetscore is an exploration of some of those aspects. @Tweetscore is a web application that&#8217;s not quite ready for prime-time, but which is nonetheless useful and interesting.</p>
<h3>Contextual Gaming</h3>
<p>There are a number of games on Twitter, like SpyMaster, which involve interaction with a game through Twitter, but the game itself has little to nothing to do with any conversations that are currently going on in Twitter. At best, people have conversations <em>about</em> SpyMaster.</p>
<p><img class="alignleft size-medium wp-image-264" src="http://mettadore.com/files/2010/02/Screen-shot-2010-02-04-at-9.08.39-AM-300x129.png" alt="Screen shot 2010-02-04 at 9.08.39 AM" width="300" height="129" />Tweetscore is different. It can be described best as &#8220;open, contextual ranking.&#8221; With Tweetscore, you give people points by sending a messages such as &#8220;@tweetscore give 3 points to @mettadore for telling me about @shizzow closing it&#8217;s doors #inpdx&#8221;</p>
<p>In this way, the game is one of points giving and points taking, but rather than being orthogonal to the conversation, the points can be, and mostly are, <em>based on the ongoing conversation</em>.</p>
<p>Users are able to use and track tags. Other features are also planned such as tagging a speaker or a talk at a convention and ranking that event based on the tag, allowing any live tweeters to add or remove points. Users can collect badges as well, for giving or taking points, for giving points to new users, etc.</p>
<h3>Why Tweetscore?</h3>
<p>I&#8217;m building Tweetscore not because I particularly care about giving Twitter users a way to rank each other, but because Twitter is one of the best live, freely available data sources in existence. My interest is in contextual scoring as well as tracking and visualizing time series data. Having real data about how people score each other and events, how they use tags, and how data changes over time is invaluable for developing other applications. By building Tweetscore, I get to explore algorithm development in a non mission critical way, things like:</p>
<ul>
<li>Building &amp; Deploying a Rails app (I&#8217;m still new at it <img src='http://mettadore.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
<li>Ranking and scoring</li>
<li>Tag parsing and scoring via tags</li>
<li>Contextual parsing of text streams</li>
<li>Badge and user status algorithms</li>
<li>Live time-series graphing and analysis</li>
</ul>
<p>In short, Tweetscore, is a way to pre-build other applications&#8211; but to have a bit of fun at the same time.</p>
<h3>What&#8217;s The Status?</h3>
<p>Right now, Tweetscore is pretty fragile, the tracking algorithms on the website need some work, as does the text parsing, but development is happening pretty fast. You&#8217;re welcome to use it. Just send a message in the format &#8220;@tweetscore give X points to @USER…&#8221; or &#8220;@tweetscore give @USER X for…&#8221;. You can also take points away by tweeting with a negative sign like &#8220;@tweetscore give -X to @USER&#8221; but be warned, you can hit people all you want, but you pay for it.</p>
<p>I&#8217;ll post a messages when Tweetscore is full on live, once we knock a few more bugs. Let me know what you think about it.</p>
]]></content:encoded>
			<wfw:commentRss>http://mettadore.com/ruby/tweetscore-data-analysis-for-fun/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

