<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-9162692450121869291</id><updated>2012-01-31T10:38:16.883-08:00</updated><category term='compact framework'/><category term='shaim'/><category term='sandbox'/><category term='excuses'/><category term='introductions'/><category term='scala'/><category term='ubuntu'/><category term='java'/><category term='work'/><category term='shaim-cf'/><category term='security'/><category term='programming'/><title type='text'>Infrequently updated blog!</title><subtitle type='html'>Things Calum may be responsible for</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://calumleslie.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9162692450121869291/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://calumleslie.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Calum</name><uri>http://www.blogger.com/profile/10640889759221203494</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/_DImyC0GhiKQ/Ssxuhk8PBEI/AAAAAAAAAAM/8OswsIvpXgE/S220/me-on-twitter.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>8</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-9162692450121869291.post-1298444682332567855</id><published>2009-04-04T04:10:00.000-07:00</published><updated>2010-08-18T16:48:43.249-07:00</updated><title type='text'>Silly placeholder post</title><content type='html'>&lt;p&gt;I should update this blog more. Maybe just a little thing right now: I really like the option parsing library &lt;a href="http://trollop.rubyforge.org/"&gt;Trollop&lt;/a&gt; for Ruby. It's a great little system, and its values (or the ones important to me, at least) are that &lt;em&gt;it is as easy as possible for the library consumer to use&lt;/em&gt;. I've been experimenting with making one with similar values for Scala, adding in the type safety and the like that you get with that language, while aiming to still keep a fluent interface.&lt;/p&gt;
&lt;p&gt;Using it looks like this:&lt;/p&gt;
&lt;pre class="prettyprint lang-scala"&gt;object Main extends OptionApplication {
 val options = new Options {
   banner = """
This is my test application.
There are many like it, but this one is mine.

Usage: Main [options]
"""
  
   val name = string( "Name to greet", default( "Nobody" ) )
   val alternativeGreeting = flag( "Use an alternative greeting" )
 }

 import options._

 def main() {
   val greeting = if( alternativeGreeting.value ) {
     "Bonjour, %s!"
   } else {
     "Hello, %s!"
   }
  
   println( greeting format name.value )
 }
}&lt;/pre&gt;
&lt;p&gt;Calling it from the "command line" (okay, I did it through Eclipse):&lt;/p&gt;
&lt;pre&gt;&amp;gt; Main --help
This is my test application.
There are many like it, but this one is mine.

Usage: Main [options]

Options:
-a, --alternative-greeting - Use an alternative greeting
               -h, --help - This help page
               -n, --name - Name to greet (default: Nobody)&lt;/pre&gt;
&lt;pre&gt;&amp;gt; Main -n Calum --alternative-greeting
Bonjour, Calum!&lt;/pre&gt;
&lt;pre&gt;&amp;gt; Main --pleasehelpme
Problem with options: unrecognised argument 'pleasehelpme'
Try --help for help.&lt;/pre&gt;
&lt;p&gt;Anyway, beyond just a random tour of that, why do I think this sort of thing is interesting? I like the idea that Scala works as a statically-type "scripting" language, as well as in the domains where Java works. I like the idea that command line tools and so on could be built with this easily. Java tends to only be used for huge big systems run inside containers and the like, because it's geared very much towards building the sort of huge system where this makes sense. Scala is fluid enough to give a lot of the advantages in fluidity you see from languages like Python and Ruby, and while it's a little more verbose, I think it's a useful alternative depending on your values.&lt;/p&gt;
&lt;p&gt;What Scala is really missing, to me, is little "fun" libraries like you find in the form of Python Eggs and Ruby Gems. There's a lot of nice, impressive, projects but I'd like to see more in the realm of small projects that aren't necessarily a work of architectural genius, but are useful (or quirky) and encourage people to dabble and play. Java lacks a sense of fun and there's no reason that Scala should end up down that route. I might go into more detail with that feeling later; this is pretty poorly-drafted. Oh well!&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9162692450121869291-1298444682332567855?l=calumleslie.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://calumleslie.blogspot.com/feeds/1298444682332567855/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9162692450121869291&amp;postID=1298444682332567855' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9162692450121869291/posts/default/1298444682332567855'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9162692450121869291/posts/default/1298444682332567855'/><link rel='alternate' type='text/html' href='http://calumleslie.blogspot.com/2009/04/silly-placeholder-post.html' title='Silly placeholder post'/><author><name>Calum</name><uri>http://www.blogger.com/profile/10640889759221203494</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/_DImyC0GhiKQ/Ssxuhk8PBEI/AAAAAAAAAAM/8OswsIvpXgE/S220/me-on-twitter.jpg'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9162692450121869291.post-8364813598985053242</id><published>2008-06-22T10:01:00.001-07:00</published><updated>2010-08-19T05:33:31.658-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='sandbox'/><category scheme='http://www.blogger.com/atom/ns#' term='security'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Simple JVM sandboxing</title><content type='html'>&lt;p&gt;I have written an dumb IRC bot which (among other things) can &lt;a href="http://calumleslie.tumblr.com/post/39267836/my-stupid-irc-bot-works"&gt;run scripts in arbitrary languages&lt;/a&gt;, provided they have Java implementations wrapped by the &lt;a href="https://scripting.dev.java.net/"&gt;JSR-223 scripting API&lt;/a&gt; (a.k.a. &lt;code&gt;javax.script&lt;/code&gt; in Java 6). At the moment I've got it doing Ruby, Groovy, Javascript and Scheme.&lt;/p&gt;
&lt;p&gt;As part of this I decided that I needed to security restrict the script runner, since by design people will essentially be able to run arbitrary code in it. Java is so kind as to provide this sort of sandboxing functionality in its core framework (this is the same sort of security protection offered when you ran Java as an applet on a webpage years ago when applets were still cool), and although it's very well documented on a high-level online, I found it difficult to find a low-level explanation on exactly how to use the code to build a sandbox like I wanted, so here's how I ended up doing it.&lt;/p&gt;
&lt;p&gt;It's worth noting that my IRC bot is actually written in Scala, so the actual Java code here (translated) is probably not well-tested. I've put the Scala wrapper I made at the foot of the entry in case it's useful to anyone.&lt;/p&gt;
&lt;h2&gt;First step &amp;ndash; enable a SecurityManager&lt;/h2&gt;
&lt;p&gt;The first thing to realise (I certainly didn't) is that none of the security infrastructure in Java does anything until you enable it. By default the security policy checks are disabled (presumably for performance reasons); to enable them you need to attach an instance of &lt;code&gt;SecurityManager&lt;/code&gt; to the system. You only need to do this once, and I'd recommend doing it as part of your app setup. It's really easy to do:&lt;/p&gt;
&lt;pre class="prettyprint lang-java"&gt;System.setSecurityManager(new SecurityManager());&lt;/pre&gt;
&lt;p&gt;The important thing to notice is that the current security policy &amp;mdash; as defined by your &lt;code&gt;.java.policy&lt;/code&gt; file in your home directory (I believe this is the same on Windows, incidentally), which will fall back to a very restrictive policy (the one used for applets) if the file doesn't exist. Now really I'd like any code that resides on my machine to run without sandboxing even when a security manager is in place (I like to live dangerously), so I can enter the following into my &lt;code&gt;/home/calum/.java.policy&lt;/code&gt; file:&lt;/p&gt;
&lt;pre&gt;grant codeBase "file:/home/calum/-" {
  permission java.security.AllPermission;
};&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;/-&lt;/code&gt; at the end matches all files below that directory recursively. One can also use &lt;code&gt;/*&lt;/code&gt; to match all JAR and class files in a directory, &lt;code&gt;/&lt;/code&gt; to match only class files, and specify individual files. For more information on policy files, check out &lt;a href="http://java.sun.com/j2se/1.3/docs/guide/security/PolicyFiles.html"&gt;this tasty document&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Important&lt;/strong&gt;: Once you create this file, it overrides the default Java policy for anything you have a rule for. &lt;em&gt;Do not just blanket grant permissions without a &lt;code&gt;codeBase&lt;/code&gt;&lt;/em&gt;, you will be opening yourself to a world of hurt, in security terms.&lt;/p&gt;
&lt;p&gt;Once you've restricted your JVM's permissions by adding a &lt;code&gt;SecurityManager&lt;/code&gt;, then un-restricted them by granting the permissions back, you're ready to actually manipulate security on parts of code in your app.&lt;/p&gt;
&lt;h2&gt;Constructing an simple sandbox&lt;/h2&gt;
&lt;p&gt;Code can be invoked at modified privilege levels by the static &lt;code&gt;AccessController.doPrivileged(PrivilegedAction,AccessControlContext)&lt;/code&gt; method. For this, we need to construct a &lt;code&gt;PrivilegedAction&amp;lt;T&amp;gt;&lt;/code&gt; (essentially a &lt;code&gt;Callable&amp;lt;T&amp;gt;&lt;/code&gt;) which encapsulates the logic to be run (if the logic throws an exception, you can use &lt;code&gt;PrivilegedExceptionAction&amp;lt;T&amp;gt;&lt;/code&gt; instead). For our simple case we can fill this context with a single &lt;code&gt;ProtectionDomain&lt;/code&gt; with a &lt;code&gt;CodeSource&lt;/code&gt; (codesource can never be null) which is constructed will &lt;code&gt;null&lt;/code&gt; values to indicate unsigned code from an unknown location, and a &lt;code&gt;PermissionCollection&lt;/code&gt; consisting &lt;em&gt;only&lt;/em&gt; of the permissions we'd like to grant our code (represented below as &lt;code&gt;perms&lt;/code&gt;:&lt;/p&gt;
&lt;pre class="prettyprint lang-java"&gt;// Cast to Certificate[] required because of ambiguity:
ProtectionDomain domain = new ProtectionDomain( new CodeSource( null, (Certificate[]) null ), perms ); 
AccessControlContext context = new AccessControlContext( new ProtectionDomain[] { domain } );
T result = AccessController.doPrivileged(action, context);&lt;/pre&gt;
&lt;p&gt;Here's a full Java program which demonstrates the use of these methods (huge, I'm afraid &amp;mdash; I've omitted the imports for brevity):&lt;p&gt;
&lt;pre class="prettyprint lang-java"&gt;public class SandboxTest {
 private static final File file = new File( "/etc/motd" );
 
 public static void main(String[] args) throws Exception
 {
  // Do not forget this line!
  System.setSecurityManager( new SecurityManager() );
  
  System.out.println( "Calling method directly:" );
  
  System.out.println( readFirstFileLine(file).run() );
  
  System.out.println( "Calling method in restrictive sandbox:" );
  
  System.out.println( callInSandbox( new Permissions() ) );
  
  System.out.println( "Calling method with file read permission:" );
  
  Permissions perms = new Permissions();
  perms.add( new FilePermission( "/etc/motd", "read" ) );
  
  System.out.println( callInSandbox(perms) );
 }
 
 private static String callInSandbox( PermissionCollection perms )
 {
  ProtectionDomain domain = new ProtectionDomain( new CodeSource( null, (Certificate[]) null ), perms );
  AccessControlContext context = new AccessControlContext( new ProtectionDomain[] { domain } );
  try
  {
   return AccessController.doPrivileged( readFirstFileLine(file), context );
  }
  catch( Exception e )
  {
   return e.toString();
  }
 }
 
 private static PrivilegedExceptionAction&amp;lt;String&amp;gt; readFirstFileLine( final File file )
 {
  return new PrivilegedExceptionAction&amp;lt;String&amp;gt;() 
  {
   @Override
   public String run() throws Exception {
    BufferedReader reader = new BufferedReader( new FileReader( file ) );
    
    try
    {
     return reader.readLine();
    }
    finally
    {
     reader.close();
    }
   }
  };
 }
}&lt;/pre&gt;
&lt;p&gt;On my machine, this yields:&lt;/p&gt;
&lt;pre&gt;Calling method directly:
Linux thoth 2.6.24-19-generic #1 SMP Wed Jun 4 16:35:01 UTC 2008 i686
Calling method in restrictive sandbox:
java.security.AccessControlException: access denied (java.io.FilePermission /etc/motd read)
Calling method with file read permission:
Linux thoth 2.6.24-19-generic #1 SMP Wed Jun 4 16:35:01 UTC 2008 i686&lt;/pre&gt;
&lt;h2&gt;Touch of Scala&lt;/h2&gt;
&lt;p&gt;Since I developed my app in Scala, here's a little object I built for simplifying this process. It works similarly to the &lt;code&gt;callInSandbox&lt;/code&gt; method above, but also wraps Scala functions so you can use closures pretty simply:&lt;/p&gt;
&lt;pre class="prettyprint lang-scala"&gt;import java.security._
import java.net.URL

object Security {
  private val codeSource = new CodeSource( null: URL, null: Array[cert.Certificate] )

  def sandboxed[T]( perms: PermissionCollection )( f: =&gt; T): T = {
    val domain = new ProtectionDomain( codeSource, perms )
    val context = new AccessControlContext( Array( domain ) )

    AccessController.doPrivileged( new Action( f ), context )
  }

  private class Action[T]( f: =&gt; T ) extends PrivilegedAction[T] {
    override def run() = f
  }
}&lt;/pre&gt;
&lt;p&gt;In order to use this, construct your &lt;code&gt;Permissions&lt;/code&gt; that you want to grant to the item, import &lt;code&gt;Security.sandboxed&lt;/code&gt;, and just wrap like this:&lt;/p&gt;
&lt;pre class="prettyprint lang-scala"&gt;sandboxed( perms ) {
  println( "This code is running within the sandbox!" )
}&lt;/pre&gt;
&lt;p&gt;I hope this helps someone out!&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9162692450121869291-8364813598985053242?l=calumleslie.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://calumleslie.blogspot.com/feeds/8364813598985053242/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9162692450121869291&amp;postID=8364813598985053242' title='19 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9162692450121869291/posts/default/8364813598985053242'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9162692450121869291/posts/default/8364813598985053242'/><link rel='alternate' type='text/html' href='http://calumleslie.blogspot.com/2008/06/simple-jvm-sandboxing.html' title='Simple JVM sandboxing'/><author><name>Calum</name><uri>http://www.blogger.com/profile/10640889759221203494</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/_DImyC0GhiKQ/Ssxuhk8PBEI/AAAAAAAAAAM/8OswsIvpXgE/S220/me-on-twitter.jpg'/></author><thr:total>19</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9162692450121869291.post-6836806578507959284</id><published>2008-03-16T06:25:00.000-07:00</published><updated>2010-08-19T05:23:12.649-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Better classing through Scala</title><content type='html'>&lt;p&gt;Rather than the promised follow-up to my last post (it's coming, honest), I'm just going to update the way that I did the first section of that post. I realised earlier that rather than making my hastily-constructed &lt;code&gt;RichIterable&lt;/code&gt; a &lt;em&gt;class&lt;/em&gt;, I should have made it a &lt;em&gt;trait&lt;/em&gt;. In this way, other classes can use its functionality explicitly if they know about it and, critically, can override the functionality in a way that makes more sense to them.&lt;/p&gt;
&lt;p&gt;To do this and still be able to do the implicit cast thing I showed, we also need a concrete class to mix the trait in with. Since you can't (&lt;em&gt;as far as I know&lt;/em&gt;) mix in traits after you've created an object, if we just create a wrapper class for &lt;code&gt;Iterable&lt;/code&gt; objects:&lt;/p&gt;
&lt;pre&gt;class IterableWrapper[A]( underlying: Iterable[A] ) extends Iterable[A]
{
  override def elements = underlying.elements
}&lt;/pre&gt;
&lt;p&gt;Which means we can now express the implicit function from before as so, creating a new wrapper with our trait mixed in:&lt;/p&gt;
&lt;pre&gt;implicit def iterable2RichIterable[A]( iterable: Iterable[A]) = {
  new IterableWrapper( iterable ) with RichIterable[A]
}&lt;/pre&gt;
&lt;p&gt;This means that any other collection can just extend the &lt;code&gt;RichIterable&lt;/code&gt; trait if they don't want to require the type conversion to use the new methods, or if they want to replace the functions with better implementations. Our actual &lt;code&gt;RichIterable&lt;/code&gt; trait can be a subtrait of &lt;code&gt;Iterable&lt;/code&gt;, too, meaning less indirection. It's wonderful I tells ya.&lt;/p&gt;
&lt;p&gt;The extra curly brackets here do absolutely nothing; I just feel it's clearer to add them in if an expression like this one is too long to fit on a line.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9162692450121869291-6836806578507959284?l=calumleslie.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://calumleslie.blogspot.com/feeds/6836806578507959284/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9162692450121869291&amp;postID=6836806578507959284' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9162692450121869291/posts/default/6836806578507959284'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9162692450121869291/posts/default/6836806578507959284'/><link rel='alternate' type='text/html' href='http://calumleslie.blogspot.com/2008/03/better-classing-through-scala.html' title='Better classing through Scala'/><author><name>Calum</name><uri>http://www.blogger.com/profile/10640889759221203494</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/_DImyC0GhiKQ/Ssxuhk8PBEI/AAAAAAAAAAM/8OswsIvpXgE/S220/me-on-twitter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9162692450121869291.post-592799253480128552</id><published>2008-03-15T11:45:00.000-07:00</published><updated>2010-08-19T05:29:00.039-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Here's a feature I made earlier</title><content type='html'>&lt;p&gt;After being suitably wowed by &lt;a href="http://blog.omega-prime.co.uk"&gt;Max Bolingbroke&lt;/a&gt;'s &lt;a href="http://blog.omega-prime.co.uk/2007/08/25/implementing-the-disposable-pattern-in-scala/"&gt;implementing the Disposable pattern from C# in my current language-of-obsession Scala&lt;/a&gt;, I was looking at some of the other features of C#/.NET which are quite nice, and wondering how they could be implemented.&lt;/p&gt;
&lt;p&gt;One set of stuff that is available in C# is the much-talked-about 
&lt;abbr title="Language INtegrated Query"&gt;LINQ&lt;/abbr&gt; system, which lets you combine and query lists in a way fairly similar to SQL. They also do a bunch of clever things (like actually translating these queries into SQL, from what I can tell) but let's gloss over that for now.&lt;/p&gt;
&lt;p&gt;In any case, there was a few of these operators don't exist in the Scala library, and since it's fairly easy to push new things onto objects in Scala, I decided to see how easily I could model them. For kicks.&lt;/p&gt;

&lt;p&gt;Before I start proper I should mention two things:&lt;/p&gt;
&lt;ul&gt;
 &lt;li&gt;I'm assuming programming knowledge, and probably some Scala experience here. That said, if you're used to this type of language you shouldn't have too much trouble reading the code examples, and hopefully you'll like the way the language works. If there's any questions, though, just comment. I don't imagine I'm a great writer.&lt;/li&gt;
 &lt;li&gt;There is probably better ways to do this stuff; I'm not hugely experienced with the language (I have at least thought of alternate ways to do some of the things). I'd be interested to hear any other approaches people have on this.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is the first of two posts. In this one I'm going to set the stage, and do something simple. In the next one I'm going to continue down the same road, and so something a little more complicated.&lt;/p&gt;

&lt;h2&gt;"Adding" our own methods onto an existing class&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;UPDATE&lt;/strong&gt;: &lt;a href="http://calumleslie.blogspot.com/2008/03/better-classing-through-scala.html"&gt;This follow-up article&lt;/a&gt; shows a way of doing this that should make the code a bit more re-usable. The principle is pretty much the same, though.&lt;/p&gt;
&lt;p&gt;This is just a bit of preparation for other things. Trust me when I say that we're going to want to put (or appear to put) extra methods onto objects with the &lt;code&gt;Iterable&lt;/code&gt; trait. It's what all the cool kids are doing.&lt;/p&gt;
&lt;p&gt;Following the naming scheme put forth by the Scala standard library, we can create a &lt;code&gt;RichIterable&lt;/code&gt; class which will house the extra stuff we're going to be putting on the normal &lt;code&gt;Iterable&lt;/code&gt; class, and make it a class which "wraps" an existing iterable object. With an implicit type conversion function which will wrap it up transparently, we can treat any &lt;code&gt;Iterable&lt;/code&gt; as a &lt;code&gt;RichIterable&lt;/code&gt; whereever the function is imported. This is done like so:&lt;/p&gt;
&lt;pre class="prettyprint lang-scala"&gt;object RichIterable
{
  implicit def iterable2RichIterable[A]( iterable: Iterable[A]) = new RichIterable( iterable )
}

class RichIterable[A]( inner: Iterable[A] )
{ 
  // Extra functions go here...
}&lt;/pre&gt;
&lt;p&gt;Now all one needs to do is add the line &lt;code&gt;import whatever.package.RichIterable._&lt;/code&gt; and the power of these new methods will be theirs.&lt;/p&gt;

&lt;h2&gt;Pushing things into a map &amp;ndash; &lt;code&gt;toMap&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;This first one is the simplest of the methods I decided to implement; it is based on the &lt;a href="http://blogs.msdn.com/timng/archive/2007/12/12/using-the-todictionary-extension-method.aspx"&gt;ToDictionary&lt;/a&gt; method available in .NET. Basically the motivation is this:&lt;/p&gt;
&lt;blockquote&gt;
 &lt;p&gt;I have a set of items with some distinguising property. I would like to put them into a Map object keyed on this property so I can look them, or maybe some property derived from them, up quickly.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Here's a more concrete example; we have our typical terrible-example-of-object-orientation class representing a car by its registration plate number and the name of its driver, which looks like so:&lt;/p&gt;
&lt;pre class="prettyprint lang-scala"&gt;class Car( registration: String, driver: String )
{
  val getRegistration = registration
  val getDriver = driver
}&lt;/pre&gt;
&lt;p&gt;We have a big list of cars (a &lt;code&gt;List[Car]&lt;/code&gt;, if you will), but what we really want is a map from the registration plate number to the driver name, so we can do something like this:&lt;/p&gt;
&lt;pre class="prettyprint lang-scala"&gt;val guiltyDriver = registrationMap( registrationOfCarSeenLeavingInAHurry )&lt;/pre&gt;
&lt;p&gt;We'd like to generate this map quickly and easily, and this is where the &lt;code&gt;toMap&lt;/code&gt; function comes in; we want to be able to do something like:&lt;/p&gt;
&lt;pre class="prettyprint lang-scala"&gt;val registrationMap = carList.toMap( _.getRegistration, _.getDriver )&lt;/pre&gt;
&lt;p&gt;This is fairly easy to achieve via a number of means, but I chose to build the inputs to the immutable &lt;code&gt;Map&lt;/code&gt;'s factory method. It takes any number of two-item tuples (doubles) and gives you a &lt;code&gt;Map&lt;/code&gt; object back. So to construct those tuples from values in the list in this case we could do something like this:&lt;/p&gt;
&lt;pre class="prettyprint lang-scala"&gt;a =&gt; ( a.getRegistration, a.getDriver )&lt;/pre&gt;
&lt;p&gt;Or more generally for a given function for the key, and one for the value:&lt;/p&gt;
&lt;pre class="prettyprint lang-scala"&gt;a =&gt; ( keyFunc(a), valFunc(a) )&lt;/pre&gt;
&lt;p&gt;So to map an entire Iterable object (like our List) using these two functions, we could do this:&lt;/p&gt;
&lt;pre class="prettyprint lang-scala"&gt;val tuples = inner.map( a =&gt; ( keyFunc(a), valFunc(a) ) ).toStream&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;toStream&lt;/code&gt; at the end is just there because the fussy &lt;code&gt;Map&lt;/code&gt; constructor wants a &lt;code&gt;Seq&lt;/code&gt;. Presumably because it wants some guarantee that there's a finite number of elements. Wuss.&lt;/p&gt;
&lt;p&gt;So to put this into a function we just need to take in the two functions, and push it out to the &lt;code&gt;Map&lt;/code&gt; factory method:&lt;/p&gt;
&lt;pre class="prettyprint lang-scala"&gt;def toMap[K,V]( keyFunc: (A) =&gt; (K), valFunc: (A) =&gt; (V) ): Map[K,V]  = {
  val tuples = inner.map( a =&gt; ( keyFunc(a), valFunc(a) ) ).toStream
    
  Map( tuples: _* )
}&lt;/pre&gt;
&lt;p&gt;This uses &lt;code&gt;K&lt;/code&gt; for the type of the key, and &lt;code&gt;V&lt;/code&gt; for the type of the value. The &lt;code&gt;_*&lt;/code&gt; type parameter makes the &lt;code&gt;Seq&lt;/code&gt; of tuples appear like a bunch of arguments, since this isn't automatic (this is similar to &lt;code&gt;params&lt;/code&gt; in C# or the &lt;code&gt;...&lt;/code&gt; thing in Java).&lt;/p&gt;
&lt;p&gt;This now works fine; so we can do something like this:&lt;/p&gt;
&lt;pre class="prettyprint lang-scala"&gt;import RichIterable._
  
val cars = List(
    new Car( "SANTA1", "Santa Claus" ),
    new Car( "PANDA1", "P.C. Plod" ),
    new Car( "12345", "Count Count" ) )
      
val registrationMap = cars.toMap( _.getRegistration, _.getDriver )

println( registrationMap )
  
println( registrationMap("SANTA1") )&lt;/pre&gt;
&lt;p&gt;And we get the output:&lt;/p&gt;
&lt;pre class="prettyprint lang-scala"&gt;Map(SANTA1 -&gt; Santa Claus, PANDA1 -&gt; P.C. Plod, 12345 -&gt; Count Count)
Santa Claus&lt;/pre&gt;
&lt;p&gt;...which is of course ridiculous, since Santa rides in a sleigh rather than driving a car.&lt;/p&gt;
&lt;p&gt;For convenience, we can also fulfil the situation where we just want to push our objects into a map keyed by the result of a function; that is, the same as the above, but the value should just be the input object. This is likely to be a fairly common use-case, so let's not make people type a lot to do it:&lt;/p&gt;
&lt;pre class="prettyprint lang-scala"&gt;def toMap[K]( keyFunc: (A) =&gt; (K) ): Map[K, A] = toMap( keyFunc, a =&gt; a )&lt;/pre&gt;
&lt;p&gt;This is just calling the existing &lt;code&gt;toMap&lt;/code&gt; function with &lt;code&gt;a =&gt; a&lt;/code&gt; (the identity function, a fancy name for a function which does nothing at all) as &lt;code&gt;valFunc&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In the next entry, I'm going to do something more substantial; an analogue to &lt;code&gt;GROUP BY&lt;/code&gt; in SQL, which was the feature of LINQ I wanted the most. If you're too anxious to wait, &lt;a href="http://www.zootm.co.uk/coding/RichIterable.scala"&gt;this code is available here&lt;/a&gt;. It's a little untidy and uncommented at present, though (and doesn't currently contain a bunch of improvements I've since added).&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9162692450121869291-592799253480128552?l=calumleslie.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://calumleslie.blogspot.com/feeds/592799253480128552/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9162692450121869291&amp;postID=592799253480128552' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9162692450121869291/posts/default/592799253480128552'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9162692450121869291/posts/default/592799253480128552'/><link rel='alternate' type='text/html' href='http://calumleslie.blogspot.com/2008/03/after-being-suitably-wowed-by-max.html' title='Here&apos;s a feature I made earlier'/><author><name>Calum</name><uri>http://www.blogger.com/profile/10640889759221203494</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/_DImyC0GhiKQ/Ssxuhk8PBEI/AAAAAAAAAAM/8OswsIvpXgE/S220/me-on-twitter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9162692450121869291.post-2697427375758817600</id><published>2008-02-18T12:17:00.000-08:00</published><updated>2010-08-19T05:31:04.964-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><title type='text'>Java gotcha o' the day</title><content type='html'>Here's something in Java that I just couldn't predict what it was going to do:

&lt;pre class="prettyprint lang-java"&gt;
try
{
    try
    {
        throw new RuntimeException("Lost?");
    }
    finally
    {
        throw new RuntimeException("Interloper");
    }   
}
catch (Exception e)
{
    System.out.println(e);
}
&lt;/pre&gt;

In hindsight I guess it's obvious that the exception thrown in the &lt;tt&gt;finally&lt;/tt&gt; block would take precedence, but losing exceptions without having to explicitly deal with them seems quite the dangerous thing. It's just strange to say.

That said, anyone throwing exceptions from a &lt;code&gt;finally&lt;/code&gt; block should probably be strung up anyway.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9162692450121869291-2697427375758817600?l=calumleslie.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://calumleslie.blogspot.com/feeds/2697427375758817600/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9162692450121869291&amp;postID=2697427375758817600' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9162692450121869291/posts/default/2697427375758817600'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9162692450121869291/posts/default/2697427375758817600'/><link rel='alternate' type='text/html' href='http://calumleslie.blogspot.com/2008/02/java-gotcha-o-day.html' title='Java gotcha o&apos; the day'/><author><name>Calum</name><uri>http://www.blogger.com/profile/10640889759221203494</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/_DImyC0GhiKQ/Ssxuhk8PBEI/AAAAAAAAAAM/8OswsIvpXgE/S220/me-on-twitter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9162692450121869291.post-3065706256817786658</id><published>2007-12-17T14:20:00.002-08:00</published><updated>2007-12-17T14:55:17.751-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='excuses'/><category scheme='http://www.blogger.com/atom/ns#' term='work'/><title type='text'>Oh good lord</title><content type='html'>&lt;i&gt;April? &lt;/i&gt;That's when I last touched this? And to update on  &lt;i&gt;why I hadn't been updating?&lt;/i&gt;

This is a disgrace. I'll have to start posting something soon.  Honest, I'll make a better effort.

I do post reasonably frequently to my &lt;a href="http://calumleslie.tumblr.com/"&gt;Tumblog&lt;/a&gt;, which is the sort of bloggin' that suits me best - low content. I've been busy at work (since I had to get another job, which will hopefully stick a little longer), that's my excuse.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9162692450121869291-3065706256817786658?l=calumleslie.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://calumleslie.blogspot.com/feeds/3065706256817786658/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9162692450121869291&amp;postID=3065706256817786658' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9162692450121869291/posts/default/3065706256817786658'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9162692450121869291/posts/default/3065706256817786658'/><link rel='alternate' type='text/html' href='http://calumleslie.blogspot.com/2007/12/oh-good-lord.html' title='Oh good lord'/><author><name>Calum</name><uri>http://www.blogger.com/profile/10640889759221203494</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/_DImyC0GhiKQ/Ssxuhk8PBEI/AAAAAAAAAAM/8OswsIvpXgE/S220/me-on-twitter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9162692450121869291.post-5724894017056049719</id><published>2007-04-22T10:04:00.000-07:00</published><updated>2007-04-22T10:12:31.880-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='shaim'/><category scheme='http://www.blogger.com/atom/ns#' term='shaim-cf'/><category scheme='http://www.blogger.com/atom/ns#' term='work'/><category scheme='http://www.blogger.com/atom/ns#' term='ubuntu'/><title type='text'>Update!</title><content type='html'>Whoops, I kinda let this stagnate. In any case, since I last posted I was made redundant from my old job; nothing I did wrong, just not enough work coming into the company. Unfortunately this means I've had to abandon the projects mentioned in the last post, as they were all PDA-based, and I no longer have access to a PDA. Furthermore I've switch to using the recently-released (and excellent) Feisty Fawn (7.04) release of &lt;a href="http://www.ubuntu.com/"&gt;Ubuntu&lt;/a&gt;, which has the knock-on effect of meaning I no longer have access to the development tools for that environment.

What I've been doing in place of that is doing some work porting &lt;a href="https://shaim.bountysource.com/"&gt;Shaim&lt;/a&gt; so that it works with the open-source implementation of .NET, &lt;a href="http://www.mono-project.com/Main_Page"&gt;Mono&lt;/a&gt;. Although I've had success getting the back-end to work, I need to write an entirely new front end, as the current one is written in Microsoft's super-flashy next generation UI library &lt;a href="http://en.wikipedia.org/wiki/Windows_Presentation_Foundation"&gt;WPF&lt;/a&gt;, which is unlikely to work with Mono any time soon.

Most of my personal projects have been put on the back-burner while looking for a job, though, and although I've found one now, I'm away on holiday for a week before starting. Hopefully I'll be back into the swing of these systems before long, though.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9162692450121869291-5724894017056049719?l=calumleslie.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://calumleslie.blogspot.com/feeds/5724894017056049719/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9162692450121869291&amp;postID=5724894017056049719' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9162692450121869291/posts/default/5724894017056049719'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9162692450121869291/posts/default/5724894017056049719'/><link rel='alternate' type='text/html' href='http://calumleslie.blogspot.com/2007/04/update.html' title='Update!'/><author><name>Calum</name><uri>http://www.blogger.com/profile/10640889759221203494</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/_DImyC0GhiKQ/Ssxuhk8PBEI/AAAAAAAAAAM/8OswsIvpXgE/S220/me-on-twitter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9162692450121869291.post-2840822521611419321</id><published>2007-01-25T06:47:00.000-08:00</published><updated>2007-01-25T07:10:31.712-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='shaim'/><category scheme='http://www.blogger.com/atom/ns#' term='introductions'/><category scheme='http://www.blogger.com/atom/ns#' term='compact framework'/><category scheme='http://www.blogger.com/atom/ns#' term='shaim-cf'/><title type='text'>Obligatory "first post" post. (first, obligatory)</title><content type='html'>Well, this is theoretically going to replace the previous blog I had on zootm.co.uk, because really most of the time there's no real reason to host your own blog, and I realised that I could use my GMail/Google  account to write on this one.

So this is going to chronicle things I &lt;a href="http://www.amazon.com/Things-Make-Do-Moloko/dp/B00004RIV4"&gt;make and do&lt;/a&gt;, since I've decided that I need to spend my copious (exaggeration) free time doing something other than yelling at people online about things that don't matter.

At the moment the only thing of note I've got going is that I'm attempting to shrink down open source .NET instant messenger &lt;a href="https://shaim.bountysource.com/"&gt;Shaim&lt;/a&gt; so that  it (well, not the fancy frontend, but the core) works on the .NET Compact Framework, and Mono. The project is hosted on the fancy-pants code hosting thing &lt;a href="http://code.google.com/hosting/"&gt;Google Code project hosting&lt;/a&gt;,  and at the moment it's mostly just an experiment, we'll see how it goes. The project is hosted under the name &lt;a href="http://code.google.com/p/shaim-cf/"&gt;shaim-cf&lt;/a&gt; if anyone wants to look at it, but all I've done is branch the original Shaim code for now.

I've also written a little sketchpad for my PDA, and I might well write something else which takes the images off of it automagically and chucks them up here at some point.

We'll see what happens on this blog anyway. It might interest people. Maybe.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9162692450121869291-2840822521611419321?l=calumleslie.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://calumleslie.blogspot.com/feeds/2840822521611419321/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9162692450121869291&amp;postID=2840822521611419321' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9162692450121869291/posts/default/2840822521611419321'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9162692450121869291/posts/default/2840822521611419321'/><link rel='alternate' type='text/html' href='http://calumleslie.blogspot.com/2007/01/obligatory-first-post-post-first.html' title='Obligatory &quot;first post&quot; post. (first, obligatory)'/><author><name>Calum</name><uri>http://www.blogger.com/profile/10640889759221203494</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/_DImyC0GhiKQ/Ssxuhk8PBEI/AAAAAAAAAAM/8OswsIvpXgE/S220/me-on-twitter.jpg'/></author><thr:total>0</thr:total></entry></feed>
