<?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/"
	>

<channel>
	<title>Bubble Foundry &#187; caseclass.js</title>
	<atom:link href="http://www.bubblefoundry.com/blog/tag/caseclassjs/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bubblefoundry.com</link>
	<description></description>
	<lastBuildDate>Wed, 08 Feb 2012 18:23:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<atom:link rel='hub' href='http://www.bubblefoundry.com/?pushpress=hub'/>
		<item>
		<title>Artvertentie</title>
		<link>http://www.bubblefoundry.com/blog/2010/02/artvertentie/</link>
		<comments>http://www.bubblefoundry.com/blog/2010/02/artvertentie/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 04:41:39 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Artvertentie]]></category>
		<category><![CDATA[caseclass.js]]></category>
		<category><![CDATA[Steganographia 2.0]]></category>

		<guid isPermaLink="false">http://www.bubblefoundry.com/?p=315</guid>
		<description><![CDATA[At the end of last year I found myself with a bunch of advertising credit for the Amsterdam newspaper Het Parool and no pressing need to advertise my business (you&#8217;re already here on the Bubble Foundry blog, right?). So, my friends and I decided to have some fun and make it into an art project, [...]]]></description>
			<content:encoded><![CDATA[<p>At the end of last year I found myself with a bunch of advertising credit for the Amsterdam newspaper Het Parool and no pressing need to advertise my business (you&#8217;re already here on the Bubble Foundry blog, right?). So, my friends and I decided to have some fun and make it into an art project, which we called <a href="http://artvertentie.com/">Artvertentie</a>. In the January 2, 2010 issue of the newspaper&#8217;s Saturday magazine we had ads throughout the magazine showing our artworks where ads would normally be. My contribution, <a href="http://artvertentie.com/bubblefoundry.html">Steganographia 2.0</a>, in an ASCII art version of the Bubble Foundry logo based upon an encoded version of <a href="http://github.com/pr1001/caseclass.js">caseclass.js</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bubblefoundry.com/blog/2010/02/artvertentie/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Introduction to Case Classes</title>
		<link>http://www.bubblefoundry.com/blog/2009/12/introduction-to-case-classes/</link>
		<comments>http://www.bubblefoundry.com/blog/2009/12/introduction-to-case-classes/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 02:53:38 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[actor.js]]></category>
		<category><![CDATA[Actors]]></category>
		<category><![CDATA[case classes]]></category>
		<category><![CDATA[caseclass.js]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[match-js]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.bubblefoundry.com/?p=305</guid>
		<description><![CDATA[I&#8217;ve been recently chatting with the creator of match-js about how his library and caseclass.js might work together and I ended up writting quite a bit about case classes. Enjoy. Scala is a newish object-oriented/functional hybrid language that runs on the JVM. It actually takes a lot of concepts from Erlang, such as its actor [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been recently chatting with the creator of <a href="http://github.com/jfd/match-js">match-js</a> about how his library and <a href="http://github.com/pr1001/caseclass.js">caseclass.js</a> might work together and I ended up writting quite a bit about case classes. Enjoy.</p>
<p>Scala is a newish object-oriented/functional hybrid language that runs on the JVM. It actually takes a lot of concepts from Erlang, such as its actor library. One of its key features is <a href="http://www.scala-lang.org/node/120">pattern matching</a>, which at the most basic can be used to test equality. With the match operator that means you have something like switch/case in most languages:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">val</span> myVal <span style="color: #000080;">=</span> <span style="color: #F78811;">3</span><span style="color: #000080;">;</span>
myVal <span style="color: #0000ff; font-weight: bold;">match</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #F78811;">1</span> <span style="color: #000080;">=&gt;</span> <span style="color: #6666FF;">&quot;one&quot;</span>
  <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #F78811;">2</span> <span style="color: #000080;">=&gt;</span> <span style="color: #6666FF;">&quot;two&quot;</span>
  <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #F78811;">3</span> <span style="color: #000080;">=&gt;</span> <span style="color: #6666FF;">&quot;three&quot;</span>
  <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #000080;">_</span> <span style="color: #000080;">=&gt;</span> <span style="color: #6666FF;">&quot;other&quot;</span>
<span style="color: #F78811;">&#125;</span> <span style="color: #008000; font-style: italic;">// returns &quot;three&quot;</span></pre></div></div>

<p><a href="http://www.scala-lang.org/node/107">Case classes</a> at a glance are just simple classes that be created without the new operator and automatic getters and setters:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #0000ff; font-weight: bold;">class</span> Person<span style="color: #F78811;">&#40;</span>name, age<span style="color: #F78811;">&#41;</span>
<span style="color: #0000ff; font-weight: bold;">val</span> peter <span style="color: #000080;">=</span> Person<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Peter Robinett&quot;</span>, <span style="color: #F78811;">25</span><span style="color: #F78811;">&#41;</span>
peter.<span style="color: #000000;">age</span> <span style="color: #000080;">==</span> <span style="color: #F78811;">25</span> <span style="color: #008000; font-style: italic;">// -&gt; true</span></pre></div></div>

<p>However, you can do a lot more advanced stuff:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">unknownVar <span style="color: #0000ff; font-weight: bold;">match</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">case</span> n<span style="color: #000080;">:</span> String <span style="color: #000080;">=&gt;</span> println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;unknowVar is a String with value: &quot;</span> + n<span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">case</span> n<span style="color: #000080;">:</span> Int <span style="color: #0000ff; font-weight: bold;">if</span> n <span style="color: #000080;">&gt;</span> <span style="color: #F78811;">0</span> <span style="color: #000080;">=&gt;</span> println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;unknownVar is an Int with a value greater than 0&quot;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">case</span> Person<span style="color: #F78811;">&#40;</span>name, <span style="color: #F78811;">100</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;unknownVar is a Person case class where property age is 100 and name is: &quot;</span> + name<span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>The last case example shows a cool use of a case class. Scala knows that when a case class instance is created in a case, you are testing for matching. So, it calls the class&#8217;s unapply method (any class can have one, case classes just have them automatically) to compare the instance based upon its properties. If the parameters match, the two instances are equal. However, you can also not specify properties to match. If you don&#8217;t care about them all you can just give a wildcard (the <code>_</code> character) or not give any parameters (<code>case Person</code> or <code>case person: Person</code>) but if you&#8217;d like to extract the value, you can. This is what I did. By giving it the undefined variable &#8216;name&#8217;, it knows that I am not comparing the name properties but instead want to assign <code>unknownVar.name</code> to <code>name</code> (assuming that <code>unknownVar</code> is an instance of <code>Person</code>!), which is then within the scope of the subsequent code block.</p>
<p>So, that&#8217;s a lot of Scala. What do I have in Javascript? Right now you can match based upon object types:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">CaseClass.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Person&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;age&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> peter <span style="color: #339933;">=</span> CaseClass.<span style="color: #660066;">Person</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Peter Robinett&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">25</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
peter.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span>
  <span style="color: #009900;">&#123;</span>
    caseTest<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Car&quot;</span><span style="color: #339933;">,</span>
    caseFunction<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#123;</span>
    caseTest<span style="color: #339933;">:</span> CaseClass.<span style="color: #660066;">Person</span><span style="color: #339933;">,</span>
    caseFunction <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// -&gt; return 1 – I'm not a Car!</span></pre></div></div>

<p>Parameter definition and extraction also works, though it&#8217;s limited:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> undef<span style="color: #339933;">;</span>
peter.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span>
  <span style="color: #009900;">&#123;</span>
    caseTest<span style="color: #339933;">:</span> CaseClass.<span style="color: #660066;">Person</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Barack Obama&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">47</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    caseFunction<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#123;</span>
    caseTest<span style="color: #339933;">:</span> CaseClass.<span style="color: #660066;">Person</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Peter Robinett&quot;</span><span style="color: #339933;">,</span> undef<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    caseFunction <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> age<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// -&gt; return 25</span></pre></div></div>

<p>As you can see, matching works fine though I need pass an uninitialized variable to Person() to indicate that I want to extract the corresponding property in peter. Because I cannot discover the name of this variable, I just have to create variables based upon the property names. I&#8217;d like to improve this, but the consensus seems to be that it&#8217;s <a href="http://stackoverflow.com/questions/1879915/javascript-function-parameter-names-at-time-of-call/">impossible with Javascript today</a> (ironically, earlier versions could). Beyond extractors, I&#8217;d like to be able to do more complex matching (better support for different object types, conditional stations) and be able to extend native objects to support my matching. I also like that your <code>Match()</code> method can stand alone, letting it be used as a callback and meaning that it acts as a sort of partial function, which is very cool. In Scala partial functions are functions that needn&#8217;t always return a value:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">def</span> myPF<span style="color: #F78811;">&#40;</span>num<span style="color: #000080;">:</span> Int<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #F78811;">1</span> <span style="color: #000080;">=&gt;</span> <span style="color: #6666FF;">&quot;one&quot;</span>
  <span style="color: #0000ff; font-weight: bold;">case</span> -<span style="color: #F78811;">1</span> <span style="color: #000080;">=&gt;</span> <span style="color: #6666FF;">&quot;negative one&quot;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>To be honest, I&#8217;m not sure if our libraries could or should work together but they&#8217;re similar enough in spirit that I thought it might be exploring how they can complement each other. I see you&#8217;re doing stuff with web workers. I&#8217;ve also tried to take Scala&#8217;s actors and use them to do something in Javascript. <a href="http://github.com/pr1001/actor.js">actor.js</a> is the result and is a place where I think case classes would be great for message passing (this is a key use case for case classes in Scala). However, actors are both more complex and something that I have less experience with, so I haven&#8217;t put as much time into it as caseclass.js. However, the demo does work and is, in my opinion, pretty cool!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bubblefoundry.com/blog/2009/12/introduction-to-case-classes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript Console</title>
		<link>http://www.bubblefoundry.com/blog/2009/09/javascript-console/</link>
		<comments>http://www.bubblefoundry.com/blog/2009/09/javascript-console/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 17:09:02 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[actor.js]]></category>
		<category><![CDATA[caseclass.js]]></category>
		<category><![CDATA[functionalism]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jsc]]></category>
		<category><![CDATA[option.js]]></category>
		<category><![CDATA[Rhino]]></category>

		<guid isPermaLink="false">http://www.bubblefoundry.com/?p=256</guid>
		<description><![CDATA[With all my recent Javascript experiments, I wanted a browser-independent Javascript console more and more. I found Rhino and have been using it for the last month or so. It&#8217;s proven pretty useful, and it was quite easy to install: I simply copied js.jar to /usr/local/rhino/ and then added the following to my .profile for [...]]]></description>
			<content:encoded><![CDATA[<p>With all my <a href="http://www.bubblefoundry.com/blog/2009/07/scala-and-javascript/">recent</a> <a href="http://www.bubblefoundry.com/blog/2009/08/javascript-coolness/">Javascript</a> <a href="http://www.bubblefoundry.com/blog/2009/08/actorjs/">experiments</a>, I wanted a browser-independent Javascript console more and more. I found <a href="https://developer.mozilla.org/en/Rhino">Rhino</a> and have been <a href="https://developer.mozilla.org/en/Rhino_Shell">using</a> it for the last month or so. It&#8217;s proven pretty useful, and it was quite easy to install: I simply copied <code>js.jar</code> to <code>/usr/local/rhino/</code> and then added the following to my <code>.profile</code> for convenience:<code> alias js="java -jar /usr/local/rhino/js.jar"</code></p>
<p>I just <a href="http://www.phpied.com/javascript-shell-scripting/">learned about jsc</a>, a console Apple includes with the JavascriptCore library that I believe every version of OS X has installed by default. It&#8217;s found at <code>/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc</code>, so again you&#8217;re going to benefit from an alias or adding the directory to your <code>PATH</code>.</p>
<p>While Rhino has more features, namely the ability to load Java libraries, the <code>jsc</code> experience is better: the console strikes me as more responsive (it definitely launches much faster), a command history is available and you can use <code>ctrl+a</code> and <code>ctrl+e</code> to jump to the beginnings and ends of lines. Whoever made <code>jsc</code> seems to have a functional bent, as every expression&#8217;s return value is printed out. This means you get <code>undefined</code> on screen a lot if you&#8217;re doing lots of loops and/or prints, which can be annoying.</p>
<p><strong>Update, 2010-06-22:</strong> Using <code>rlwrap</code> (e.g. <code>alias js="rlwrap java -jar /usr/local/rhino/js.jar"</code>) solves many of the problems I had with Rhino.</p>
<p>Speaking of that, I think they handle the return values of loops incorrectly. As I understand it, all loops should return <code>undefined</code>, i.e. nothing. But consider this <code>jsc</code> session:</p>
<pre>&gt; var a = 1; while (a &lt; 4) { print(a); a++; }
1
2
3
3</pre>
<p>3 is printed out twice because it is &#8216;returned&#8217; by the <code>while</code> loop. Of course, <code>while</code> does not really return a value, as this session shows:</p>
<pre>&gt; var a = 0; var b = while (a &lt; 4) { a++; }
Exception: SyntaxError: Parse error</pre>
<p>But where does the 3 come from, the <code>a</code> variable or the loop&#8217;s condition? From the following sessions, I believe it comes from the last time the condition evaluates to <code>true</code>:</p>
<pre>&gt; while (1 != 1) {}
undefined
&gt; var a = 1; while (1 != 1) { a; }
undefined
var a = 1; while (1 != 1) { a = 2; }
undefined
&gt; var a = 1; while (a != 2) { a = 2; }
2</pre>
<p>Note that when the condition <em>never</em> is true, there is no defined value outputted. Likewise, assignment within the loop has no effect on what is &#8216;returned&#8217;. Or rather, I wish it was that way. Consider this:</p>
<pre>&gt; var a = 0; while (a &lt; 2) { a = 2; }
2
&gt; var a = 0; while (a &lt; 2) { a++; }
1</pre>
<p>To be honest, I really am confused about what <code>jsc</code> is doing here. It seems wrong, though irrelevant since these values printed out cannot be assigned to a variable and used. Perhaps someone from the <code>jsc</code> team can explain this?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bubblefoundry.com/blog/2009/09/javascript-console/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Javascript Coolness</title>
		<link>http://www.bubblefoundry.com/blog/2009/08/javascript-coolness/</link>
		<comments>http://www.bubblefoundry.com/blog/2009/08/javascript-coolness/#comments</comments>
		<pubDate>Sat, 08 Aug 2009 22:01:50 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[caseclass.js]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[option.js]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.bubblefoundry.com/?p=239</guid>
		<description><![CDATA[I&#8217;m really excited about how option.js and caseclass.js are coming along. I just committed a bunch of additions, with case classes now supporting simple extractors and options now iterable.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m really excited about how <a href="http://github.com/pr1001/option.js/tree/master">option.js</a> and <a href="http://github.com/pr1001/caseclass.js/tree/master">caseclass.js</a> are coming along. I just committed a bunch of additions, with case classes now supporting simple <a href="http://www.scala-lang.org/node/112">extractors</a> and options now iterable.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bubblefoundry.com/blog/2009/08/javascript-coolness/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

