<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Trypticon: Tag programming</title>
    <link>http://trypticon.org/articles/tag/programming?tag=programming</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>If it ain't broke, break it.</description>
    <item>
      <title>Just Another Monday Morning</title>
      <description>&lt;p&gt;Monday morning came with a request to change the company web site to American &amp;#8220;English&amp;#8221;.&lt;/p&gt;

&lt;p&gt;My first thought was that we&amp;#8217;re not a US company, so we shouldn&amp;#8217;t need to conform to US spelling.  The reasoning I got back was that US companies got scared off by British spelling.&lt;/p&gt;

&lt;p&gt;This reasoning sounds pretty ludicrous to me, and besides, if we switch to American spelling, won&amp;#8217;t that &amp;#8220;scare off&amp;#8221; the entire rest of the English speaking world?&lt;/p&gt;

&lt;p&gt;So I thought, OK&amp;#8230; maybe we can do this and yet not do this.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#!/usr/bin/ruby
#
# Breaks proper English spelling for the benefit of yanks.
# Word list is incomplete, but covers enough to be useful for now.

ARGF.each_line do |line|
    line.sub!(/\b(amort|apolog|author|capital|critic|emphas|general|harmon|initial|maxim|minim|optim|real|recogn|visual)is(e|ed|es|er|or|ation)\b/, '\1iz\2')
    line.sub!(/\b(anal|catal|paral)ys(e|ed|es|er|or)\b/, '\1yz\2')
    line.sub!(/\b(arm|behavi|col|flav|harb|hon|hum|neighb|sav|savi)our(s?)\b/, '\1or\2')
    line.sub!(/\b(defen|licen|offen|preten)ce(s?)\b/, '\1se\2')
    line.sub!(/\b(calib|cent|fib|kilomet|lit|meag|met|theat)re(s?)\b/, '\1er\2')
    line.sub!(/\bpractis(e|ed|ing)\b/, 'practic\1')
    puts line;
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;My only concern is that it might match an ID somewhere in the HTML which will mess the CSS up, but otherwise, it&amp;#8217;s looking like a damn good idea.&lt;/p&gt;</description>
      <pubDate>Mon, 14 May 2007 10:13:00 +1000</pubDate>
      <guid isPermaLink="false">urn:uuid:5e8de16b-be1b-459d-a5f0-3fb4c9ac6e85</guid>
      <author>Trejkaz</author>
      <link>http://trypticon.org/articles/2007/05/14/just-another-monday-morning</link>
      <category>programming</category>
      <category>monday</category>
      <category>spelling</category>
      <category>english</category>
      <category>ruby</category>
    </item>
    <item>
      <title>Higher Order Messages for Mapping</title>
      <description>&lt;p&gt;Say you want to convert an array of strings to uppercase. You could write&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;result = names.map { |name| name.upcase }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/ToProc.rdoc" title="PragDave (Blog)"&gt;PragDave&lt;/a&gt; suggests an alternative which uses the &lt;a href="http://extensions.rubyforge.org/" title="Ruby Extensions Project (Web Site)"&gt;Ruby Extensions Project&lt;/a&gt;&amp;#8217;s Symbol#to_proc method to pass a symbol.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;result = names.map(&amp;amp;:upcase)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is all well and good, but can you really call it elegant?  Suppose you want to add 1 to all elements in a list.  How can this method let you pass in that parameter?  It seems that it can&amp;#8217;t.&lt;/p&gt;

&lt;p&gt;Higher Order Messages (HOM) provide a better way, although a way which seems even more like magic.&lt;/p&gt;

&lt;p&gt;The &lt;a href="http://rubyforge.org/projects/homer" title="Homer: Higher Order Message Extension to Ruby"&gt;Homer&lt;/a&gt; project offers HOM for Ruby, and although it didn&amp;#8217;t have a method which did something like map, it was relatively simple to whip something together.&lt;/p&gt;

&lt;p&gt;My own version of the above functionality:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;result = names.map_using.upcase
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Isn&amp;#8217;t that better?  I couldn&amp;#8217;t call it map without causing a little mayhem, having to alias the old version and still provide complete compatibility was a bit much for me at this point in time.&lt;/p&gt;

&lt;p&gt;But better yet, this version &lt;em&gt;does&lt;/em&gt; permit you to use parameters:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;costs_with_gst = costs.map_using * 1.1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Don&amp;#8217;t bother asking for the source code for this&amp;#8230; I&amp;#8217;ll be shuffling it up to the Homer project as soon as I hit submit. ;-)&lt;/p&gt;</description>
      <pubDate>Fri, 04 Nov 2005 10:29:00 +1100</pubDate>
      <guid isPermaLink="false">urn:uuid:eebc6dfa-f369-4556-92d9-b28b223bdaa3</guid>
      <author>Trejkaz</author>
      <link>http://trypticon.org/articles/2005/11/04/higher-order-messages-for-mapping</link>
      <category>programming</category>
      <category>ruby</category>
    </item>
    <item>
      <title>Weak References Save Lives</title>
      <description>&lt;p&gt;(Or, How MVC Ate My Memory)&lt;/p&gt;

&lt;p&gt;Part of the problem with using excessive MVC in any language, and particularly with using Swing in Java, is that if you create and destroy a large number of GUI components over the lifetime of your application, you will end up with a lot of deadweight.&lt;/p&gt;&lt;p&gt;A na</description>
      <pubDate>Tue, 15 Feb 2005 16:45:00 +1100</pubDate>
      <guid isPermaLink="false">urn:uuid:3f322388b1a9fe297a63eda88e5b6b94</guid>
      <author>Trejkaz</author>
      <link>http://trypticon.org/articles/2005/02/15/weak-references-save-lives</link>
      <category>programming</category>
      <category>java</category>
    </item>
    <item>
      <title>Relative File Path Anomalies</title>
      <description>&lt;p&gt;Sometimes the subtlety in Java is almost enough to be annoying.&lt;/p&gt;

&lt;p&gt;For instance, let&amp;#8217;s take the java.io.File constructors for today. Ignoring the versions of the constructors which take File objects themselves, you basically have a choice between three ways to construct your File path.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use an absolute path (almost never used if you want portability, since an absolute path inevitably requires knowing what OS you&amp;#8217;re using before you create it);&lt;/li&gt;
&lt;li&gt;Use a relative path, providing the base path to resolve it relative to.&lt;/li&gt;
&lt;li&gt;Use a relative path, providing no base path.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The last option is where problems will occur. Suppose you create a file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;File file = new File("foo.txt");
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As you can probably guess, this is supposed to point at the file called &amp;#8220;foo.txt&amp;#8221; relative to the current directory. But what you might not expect, is that this is not always the case.&lt;/p&gt;

&lt;p&gt;Suppose you have a native library loaded at some point in your application. This native library then does the unthinkable, and changes the current working directory. As soon as this happens, everything goes to shit.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;System.out.println(System.getProperty("user.dir"));
File file = new File("foo.txt");
System.out.println(file);
System.out.println(file.getAbsoluteFile());
InputStream stream = new FileInputStream(file);
stream.close();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you run this &lt;em&gt;after&lt;/em&gt; some cretinous native library has changed the path, you&amp;#8217;ll see something like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;/home/trejkaz/test
foo.txt
/home/trejkaz/test/foo.txt
java.io.FileNotFoundException: Cannot find the file: foo.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As far as the first three lines go, everything looks okay. And looking in the current directory, you&amp;#8217;ll see that &amp;#8220;foo.txt&amp;#8221; is indeed present. Yet, the file doesn&amp;#8217;t exist&amp;#8230; why?&lt;/p&gt;

&lt;p&gt;Well, it turns out that non-absolute file paths are resolved relative to the new current directory. And what&amp;#8217;s funny, and perhaps wrong on the part of the Java API, is that calling getAbsoluteFile() on that File object actually results in Java resolving it relative to the value of the &amp;#8220;user.dir&amp;#8221; property, instead of the real current directory. So it will appear that the file path is right, even though it&amp;#8217;s wrong.&lt;/p&gt;

&lt;p&gt;So this is why you should always call getAbsoluteFile() on any paths which are created which weren&amp;#8217;t already absolute, and/or construct them relative to some other explicit path.&lt;/p&gt;

&lt;p&gt;And I suppose it&amp;#8217;s also why native libraries are bad, and why everything should just be implemented in Java in the first place. :-) &lt;/p&gt;</description>
      <pubDate>Fri, 28 Jan 2005 13:29:00 +1100</pubDate>
      <guid isPermaLink="false">urn:uuid:dc6e1464d484a0f61b47351ae293ebdc</guid>
      <author>Trejkaz</author>
      <link>http://trypticon.org/articles/2005/01/28/relative-file-path-anomalies</link>
      <category>programming</category>
      <category>java</category>
    </item>
  </channel>
</rss>
