<?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>hackinghat.com</title>
	<atom:link href="http://www.hackinghat.com/index.php/feed" rel="self" type="application/rss+xml" />
	<link>http://www.hackinghat.com</link>
	<description></description>
	<lastBuildDate>Wed, 19 May 2010 07:48:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Programming Like It&#8217;s 1995</title>
		<link>http://www.hackinghat.com/index.php/programming/programming-like-its-1995</link>
		<comments>http://www.hackinghat.com/index.php/programming/programming-like-its-1995#comments</comments>
		<pubDate>Tue, 18 May 2010 21:58:52 +0000</pubDate>
		<dc:creator>Steve Knight</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.hackinghat.com/?p=208</guid>
		<description><![CDATA[Object-oriented programming, where is it now? When I was at college we learnt a little about object methods and techniques and when I left I kept reading about them. I remember in the 90s I was incensed that an employer was NOT using object methods appropriately. I EVEN read books about it. You could say [...]]]></description>
			<content:encoded><![CDATA[<p>Object-oriented programming, where is it now? When I was at college we learnt a little about object methods and techniques and when I left I kept reading about them. I remember in the 90s I was incensed that an employer was NOT using object methods appropriately. I EVEN <a href="http://www.amazon.co.uk/Object-Lessons-Object-Oriented-Development-Technology/dp/0134724321/ref=sr_1_2?ie=UTF8&amp;s=books&amp;qid=1272267895&amp;sr=8-2">read books</a> about it. You could say I was an object fan-boy.    But even I have to admit that OOP didn&#8217;t exactly deliver how I expected it would.  </p>
<p>You may say that this is a crazy talk and that OOP was/is hugely succesful.   <a href="http://en.wikipedia.org/wiki/Jumping_the_shark">Shark-jumping</a> mumbo jumbo.   But I might disagree.   In fact, sometimes I think the object-baggage we&#8217;ve inherited is perhaps as much a hinderance as it is a help.   The wake-up call came a few months ago when I realised that almost every piece of code that I&#8217;m in contact with these days makes only limited use of object-techniques.</p>
<p>Before I talk about all the ways that OOP is dead let me be clear in the ways that it is not.   Because when I say OOP is mostly-dead, I mean it is mostly dead <strong>to me</strong>.   In some areas OOP has delivered in spades.  For example in modern OO programming languages nearly all come complete with large object libraries.   This much is true. There&#8217;s objects in <strong>them-thar</strong> binaries for sure.   But my code?   Not so much.</p>
<p>Here&#8217;s all the ways I manage NOT to write truly object-oriented software:</p>
<h3><strong>The Task is Too Small</strong></h3>
<p>Some systems are just too small to develop a complex object class hierarchy for. It would be a waste of time to do so. I estimate these &#8216;glue&#8217; applications could take up as much as 5% of the total LOC of a large enterprise system.   It doesn&#8217;t matter whether these glue applications are written in Java or Python or Bash because really they&#8217;re just scripts.    Scripts are their own thing.   I would argue that the less scripts you have in your system the better you designed it because you&#8217;re not just duct-taping over the seams.</p>
<h3><strong>The Spreadhseets Rule</strong></h3>
<p>I would also estimate that some appreciable percentage of your enterprise is run entirely from spreadsheets. Whether you know about it or not.   Be it phone book, accounts, trading system or stock inventory. These little beauties contain little or-no object code and are spread far-and-wide.   I&#8217;ve <a href="http://www.hackinghat.com/index.php/article/the-spread-able-system">ranted about the pervasiveness of spreadsheets before</a>, no need to go over it again.  However, as far as I know, no-one has implemented the idea of an OO spreadsheet.   For that we can all be thankful.</p>
<h3><strong>World Wide Wasteland</strong></h3>
<p>Although the web does lend itself beautifully to <a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller">model-view-controller</a>, on the client side a lot of it is only markup and Javascript.   Neither markup nor JS have particularly strong OO characteristics and both are hugely succesful without those OO characteristics.   Indeed many WWW apps are really CRUD applications.</p>
<h3><strong>CRUD</strong></h3>
<p>The <a href="http://en.wikipedia.org/wiki/Create,_read,_update_and_delete">create-update-delete</a> application is everywhere.   Be it web-or-desktop.   These apps are effectively database front-ends that organise the interactions between user and DB in a more user-centric way.    For example in .NET there&#8217;s not much need nor desire to map your data into real-objects because the data-binding layer is phenomenally powerful at making data-bound apps quickly.   There&#8217;s no support to help you map from data to objects to <a href="http://www.infragistics.com/">Infragistics</a>.   Indeed, nor should there be.</p>
<h3><strong>Enterprise Business Objects</strong></h3>
<p><img style="float:right; padding: 1em;" src="http://upload.wikimedia.org/wikipedia/commons/thumb/4/4d/JSP_RLE_output1.png/240px-JSP_RLE_output1.png" alt="JSP" /> And this is the bit that makes me a bit sad inside.  This is what OO was really meant for.   I used to have arguments with business-analysts about the right object model to use and whether a method should exist in a base-class or a derived-class.   But now it doesn&#8217;t seem like anyone, including me, really cares.   It&#8217;s just that somewhere along the line it became a little irrelevant.   Don&#8217;t get me wrong, I work with objects all the time.   But they&#8217;re not really objects that were sold to me.   </p>
<p>They&#8217;re just data-holders or as our fathers used to say: <storng>data-records</strong>.  </p>
<h3><strong>No Methods?  No Object</strong></h3>
<p>The thing is that to me at least, without methods on your objects there&#8217;s literally no object.   If objects doesn&#8217;t respond to messages they&#8217;re just data-records transferring data to some other module that can operate on those records.   Usually this other module takes the role of a controller.    This, to me, sounds very similar to the programming that our fathers used to-do before C++ and Java 1.1.     So much so that it&#8217;s tempting to break open my book on <a href="http://en.wikipedia.org/wiki/Jackson_Structured_Programming">JSP</a>.  </p>
<p>I think there are two fundamental areas where objects failed to deliver.    </p>
<h3><strong>Finance This</strong></h3>
<p>Firstly, whilst OO techniques are very flexible in some business domains they aren&#8217;t flexible in exactly the <strong>right</strong> ways for all business domains.   I&#8217;m thinking particularly about my own area of expertise, which is financial trading systems.   </p>
<p>The objects in trading systems tend to be difficult to compose into a meaningful hierarchy that is both expressive and not too abstract.   I think ultimately this failing is because financial instruments are actually themselves <em>models</em> of physical events.   This means that it&#8217;s straight-forward enough to construct an object-model of financial instruments.   However as soon as I start innovating with my financial instruments (i.e. construct new financial models from old ones) the original object models tend to break-down pretty fast.</p>
<h3><strong>The Technology Stack Sandwich</strong></h3>
<p>The second reason is that there are too many different technologies involved in many enterprise-sized solution stacks to make consistent application of OO methods viable.   What does that mean?   Well this is perhaps a post in itself but essentially as soon as you are using two or more programming languages that must share objects you&#8217;re essentially entering an object-desert.</p>
<h3><strong>The End?</strong></h3>
<p>Oh no.   Very definitely not.   Objects are the only way to make sense of a deep-and-wide library.   If the domain allows it they are the only way to go.</p>
<p>The surprise is that objects just didn&#8217;t deliver in the way that I thought that they might <strong>for me</strong>.  Which is kind-of interesting because it suggests to me that perhaps me, and a lot of people like me, might benefit from forgetting about objects sometimes and just <em>Programming Like It&#8217;s 1995</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackinghat.com/index.php/programming/programming-like-its-1995/feed</wfw:commentRss>
		<slash:comments>71</slash:comments>
		</item>
		<item>
		<title>Using Git from behind an NTLM proxy</title>
		<link>http://www.hackinghat.com/index.php/windows/using-git-from-behind-an-ntlm-proxy</link>
		<comments>http://www.hackinghat.com/index.php/windows/using-git-from-behind-an-ntlm-proxy#comments</comments>
		<pubDate>Wed, 21 Apr 2010 10:50:36 +0000</pubDate>
		<dc:creator>Steve Knight</dc:creator>
				<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://www.hackinghat.com/index.php/uncategorized/using-git-from-behind-an-ntlm-proxy</guid>
		<description><![CDATA[For some reason the Git that ships with Cygwin (v1.6.6.1) won&#8217;t do the right thing with NTLM proxies. It seems that Git uses cURL underneath and that cURL can correctly handle NTLM authentication if the options are set correctly. However, the version of Git I have isn&#8217;t capable of passing this information through. In fact, [...]]]></description>
			<content:encoded><![CDATA[<p>For some reason the Git that ships with Cygwin (v1.6.6.1) won&#8217;t do the right thing with NTLM  proxies.   It seems that Git uses cURL underneath and that cURL can correctly handle NTLM authentication if the options are set correctly.  </p>
<p>However, the version of Git I have isn&#8217;t capable of passing this information through.     In fact, some browsing of the issues that people have with this suggests that there is more than one part of Git that isn&#8217;t able to work correctly with NTLM.   So even if you&#8217;re able to get past the initial connection you probably won&#8217;t be able to fetch any of the tree.</p>
<p>The solution, in full then, is to use ntlmaps to act as a proxy to the proxy. All you need to do is to download the latest version of the app from: <a href="http://ntlmaps.sourceforge.net/">ntlmaps</a>.  Change the config file to include your authentication and proxy details, start it up and then set the proxy to be your new local one:</p>
<p><code>git config --global http.proxy http://localhost:5865</code></p>
<p>I can confirm that it works just fine. Not only that you can use it for any app that requires NTLM authentication but does not provide full NTLM support.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackinghat.com/index.php/windows/using-git-from-behind-an-ntlm-proxy/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Excel Gambit</title>
		<link>http://www.hackinghat.com/index.php/lisp/the-excel-gambit</link>
		<comments>http://www.hackinghat.com/index.php/lisp/the-excel-gambit#comments</comments>
		<pubDate>Mon, 19 Apr 2010 22:21:09 +0000</pubDate>
		<dc:creator>Steve Knight</dc:creator>
				<category><![CDATA[lisp]]></category>

		<guid isPermaLink="false">http://www.hackinghat.com/?p=165</guid>
		<description><![CDATA[In the spirt of shoving Lisp where Lisp should-not-go I have jammed Gambit Scheme into Excel. If you&#8217;re interested in trying it out you should go and get the files RIGHT NOW at GitHub. You just need to add the compiled .XLL into your add-ins and use the formula =GAMBITC(&#8220;(+ 1 1)&#8221;) in a cell [...]]]></description>
			<content:encoded><![CDATA[<p><span style="float: right;"><br />
<div id="attachment_170" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.hackinghat.com/wp-content/uploads/2010/04/xl-gambit.png"><img src="http://www.hackinghat.com/wp-content/uploads/2010/04/xl-gambit-300x189.png" alt="Excel screen shot of XL-Gambit" title="xl-gambit" width="300" height="189" class="size-medium wp-image-170" /></a><p class="wp-caption-text">XLREPL</p></div><br />
</span><br />
In the spirt of <a href="http://jlongster.com/blog/2009/06/17/write-apps-iphone-scheme/">shoving Lisp</a> where <a href="http://jlongster.com/blog/2010/04/09/scheme-dead-iphone/">Lisp should-not-go</a> I have jammed <a href="http://dynamo.iro.umontreal.ca/~gambit/wiki/index.php/Main_Page">Gambit Scheme</a> into Excel.</p>
<p>If you&#8217;re interested in trying it out you should go and get the files RIGHT NOW at <a href="http://github.com/hackinghat/misc/tree/master/xl-gambit">GitHub</a>.    You just need to add the compiled .XLL into your add-ins and use the formula <strong>=GAMBITC(&#8220;(+ 1 1)&#8221;)</strong> in a cell of your choosing.</p>
<p><strong>Please note this is a proof-of-concept.  </strong>   You probably shouldn&#8217;t use it for anything important, most importantly because it probably leaks like a sieve.  You should also know I only really tested it on Excel 2003.   It should load into Excel 2007 but I didn&#8217;t try that yet.</p>
<h3>Why?  Because it&#8217;s there, and I&#8217;m here</h3>
<p>Why did I do it?  Well the rationale behind doing this was that I wanted to write an app for Excel in Scheme.   I want to do that because I think it would be vastly superior to using .NET or VBA or COM.   Having spent a few hours on it I think I could definitely achieve what I set out to do but whether I&#8217;ll actually go this route in the end I&#8217;m undecided about.    More on that later.</p>
<h3>Mature like a good cheese</h3>
<p>The Excel API, as it turns out, is &#8230; well &#8230; mature.   Now I like old things quite a lot.   I&#8217;m old, and I like me just fine.   One consequence of age is that we get a little extra baggage on the way.   This is true of Excel too.  </p>
<p>Now, IMHO one of the more challenging aspects of programming a long-lived project is version management.   Especially version management of an API.   Broken backwards/forwards compatibility of those APIs could be serious impediments to a new Excel&#8217;s acceptance.   Unsurprisingly then Excel has a number of APIs that have all undergone various enhancements over the years.   To me though the switch from Excel 2003 to Excel 2007 was the most significant, requiring the most additional cognitive load to work with.   For now, and for simplicity, I have chosen to totally deny the existence of Excel 2007.   But I&#8217;m coming back to it, oh yes I am.</p>
<h3>I Eat Pain For Breakfast</h3>
<p>I had read a bit online about the Excel C API and found it all hugely confusing.   So I bought a <a href="http://www.amazon.co.uk/Financial-Applications-Using-Excel-Development/dp/0470027975/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1271668694&#038;sr=8-1">book</a> thinking that that would make it clearer.  Although the author does attempt to explain how you might write an Excel add-in he does it in a way that makes programming from it hard.   For example, the information I need is spread all-over the book.   Answering a single question about Excel&#8217;s behaviour with respect to calling the core Excel4/Excel12 function necessitates flicking between the index and more than 10 different page sections.   </p>
<p>The reason for this is probably that Excel is really a very versatile tool which is why a lot of the world&#8217;s business (large and small <img src='http://www.hackinghat.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ) seem to operate their database, accounts and customer details from it.    So there&#8217;s not really one track through the development process.   There&#8217;s literally hundreds, limited only by your imagination! </p>
<p>Having said that the book in combination with the <a href="http://msdn.microsoft.com/en-us/library/aa730920.aspx">Excel 2007 SDK</a> was sufficient to pick through the rubble and build something workable.  Maybe I&#8217;ll try and produce some guides/tutorials of my own to make the topic clearer.  Maybe.</p>
<p>On the Gambit side there&#8217;s not much more documentation than what&#8217;s on the <a href="http://dynamo.iro.umontreal.ca/~gambit/doc/gambit-c.html">manual HTML page</a> to help.   Like the Excel book it is also very densely populated with information.   IMHO its main failing is that it could really do with having more documentation of the C macro functions.   Did I ever mention that I hate C macros?  Well in Gambit these undocumented C macros are effectively what Gambit Scheme is written in and you kind-of need a fairly thorough explanation of how it all fits together to be able to make an effective glue with them.   I will be coming back to this later when I try and construct lists from cell ranges.</p>
<p>Finally I decided to do this all in <a href="http://www.MinGW.org">MinGW</a> rather than the more usual VC++.   Whilst going this route did cost me a bit of time I&#8217;d much rather use the GCC toolchain because I understand it better.  </p>
<h3>Where-to-now</h3>
<p>Now that I have the interpreter in Excel I will probably work on tidying it up for Excel 2007.   More importantly though, I want to see if I can exploit Excel 2007&#8242;s all important multi-threading capability. </p>
<p>If that all works out I might use it to make a custom add-in.   I will develop the app from the REPL in Emacs using Gambit Scheme.   When it&#8217;s done I&#8217;ll compile it up into a standalone XLL with Gambit.     That&#8217;s the plan anyway.</p>
<p>Let me know if you like it, or can see a use for it.   Encouragement is always good.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackinghat.com/index.php/lisp/the-excel-gambit/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Some Sums Sum, Sum Don&#8217;t</title>
		<link>http://www.hackinghat.com/index.php/education/some-sums-sum-sum-dont</link>
		<comments>http://www.hackinghat.com/index.php/education/some-sums-sum-sum-dont#comments</comments>
		<pubDate>Mon, 29 Mar 2010 08:16:24 +0000</pubDate>
		<dc:creator>Steve Knight</dc:creator>
				<category><![CDATA[education]]></category>
		<category><![CDATA[flash]]></category>

		<guid isPermaLink="false">http://www.hackinghat.com/index.php/education/some-sums-sum-sum-dont</guid>
		<description><![CDATA[My eldest daughter has been getting maths homework for a year or two now. We try and do the homework together and I try and make it fun but it&#8217;s hard. It&#8217;s hard because if she doesn&#8217;t understand something by the second time I explain it, then to her I become like the teacher in [...]]]></description>
			<content:encoded><![CDATA[<p>My eldest daughter has been getting maths homework for a year or two now.   We try and do the homework together and I try and make it fun but it&#8217;s hard.  It&#8217;s hard because if she doesn&#8217;t understand something by the second time I explain it, then to her I become like the teacher in Charlie Brown.  <a href=http://www.youtube.com/watch?v=eUyLwXhqlWU">Blah Blah Blah</a>.  This is especially true if when I&#8217;m explaining elementary mathematics, I make a mistake in my explanation.   This happens often and only causes to confuse her royally.  </p>
<p>And so it was with the topic of number bonds.   My wife discovered, by attending a parent&#8217;s maths workshop at the school, that the children were learning that certain numbers in base 10 arithmetic are <strong>bonded</strong>.    The bonds are simply as follows:</p>
<ul>
<li>1-&gt;9</li>
<li>2-&gt;8</li>
<li>3-&gt;7</li>
<li>4-&gt;6</li>
<li>5-&gt;5</li>
</ul>
<p>It&#8217;s fairly trivial but also fairly powerful in that once you know these bonds it&#8217;s a shortcut for mental arithmetic.   We, as adults, know these &#8216;bonds&#8217; so well that we probably don&#8217;t even recognise that we use them. The challenge then is to teach children this trick and thereby speed up their arithmetic ability.</p>
<p>One of the techniques that my daughter&#8217;s school uses is to send home &#8216;games&#8217; that try and reinforce the learning they have done in the class.   These games usually consist of a laminated sheet of instructions, a dice or two or other simple props.   Usually these games are quite good and instructive.  Some times they are totally confusing because they are meant to reinforce what was learnt in class and that doesn&#8217;t come across in the instructions.   You&#8217;d think that I could ask my daughter to explain &#8216;how&#8217; they should be doing it.   It doesn&#8217;t work.   Kids just aren&#8217;t that &#8216;meta&#8217;, that&#8217;s why they&#8217;re so much fun.</p>
<p>Now I have observed both my children playing on <a href="http://www.friv.com">friv</a> and <a href="http://www.clubpenguin.com">Club Penguin</a>.   They are totally immersed in the most crappy Flash apps (although I have to say that most of them are actually quite fun!).   So I thought I&#8217;d try and see if I could make a Flash game that would teach a little mathematics.    </p>
<p>A while ago an ex-colleague posted about a <strong>super-cool</strong> <a href="http://www.darkcoding.net/software/math-dodger-a-flash-game/">Flash application he&#8217;d made</a>.   From that I figured out I could download the SDK and most of what I needed for free.   However I thought I&#8217;d try the 60-day trial of <a href="http://www.adobe.com/products/flex/">Flex Builder</a>.   The added comfort of Flex builder is that it&#8217;s a pre-configured Eclipse for building Flash apps, so progress is pretty smooth-and-fast.   Not sure I&#8217;d spend the $299 for a personal license though.   It&#8217;s not that good.   </p>
<p>I had always thought that Flash development was complex but it turns out that it really isn&#8217;t.  If you can program Javascript, then there really isn&#8217;t that much extra to learn to make it work because you can use <a href="http://en.wikipedia.org/wiki/ActionScript">ActionScript</a> (which is a derivative of <a href="http://en.wikipedia.org/wiki/ECMAScript">ECMAscript</a> as Javascript is).   The biggest difficulty I found was with laying out text.   Something that HTML laps up, but turns out to be really cumbersome in Flex.   I&#8217;m probably just doing it wrong.</p>
<p>And so the unimaginatively titled &#8216;Number Bonds&#8217; was born.   Enjoy.   </p>
<p><object width="500" height="375"><param name="movie" value="/NumberBonds.swf" /><embed src="/NumberBonds.swf" width="500" height="375" type="application/x-shockwave-flash" /></object></p>
<p>If you&#8217;re interested in the code it&#8217;s <a href="http://github.com/hackinghat/misc/tree/master/Number%20Bonds">here</a>.   It&#8217;s pretty messy though.  I&#8217;ve also made a back-story-free page about &#8216;Number Bonds&#8217; <a href="/index.php/number-bonds">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackinghat.com/index.php/education/some-sums-sum-sum-dont/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Pythagoras Would Puke</title>
		<link>http://www.hackinghat.com/index.php/lisp/pythagoras-would-puke</link>
		<comments>http://www.hackinghat.com/index.php/lisp/pythagoras-would-puke#comments</comments>
		<pubDate>Mon, 15 Mar 2010 21:01:34 +0000</pubDate>
		<dc:creator>Steve Knight</dc:creator>
				<category><![CDATA[lisp]]></category>

		<guid isPermaLink="false">http://www.hackinghat.com/index.php/lisp/pythagoras-would-puke</guid>
		<description><![CDATA[This week a colleague pointed out this comment from an Ask Reddit: danceswithwolves 62 points 4 days ago* [-] I went to a job interview and the interviewer told me I would get the job (my first full time job ever) if I could answer this math question which is supposedly a middle school grade [...]]]></description>
			<content:encoded><![CDATA[<p>This week a colleague pointed out this comment from <a href="http://www.reddit.com/r/AskReddit/comments/bb43z/whats_the_most_shockingly_basic_math_question_you/">an Ask Reddit</a>:</p>
<div style="background:lightgrey;font-family:sans;margin: 1em;padding:1em">
<strong>danceswithwolves</strong> 62 points 4 days ago* [-]<br/><br />
I went to a job interview and the interviewer told me I would get the job (my first full time job ever) if I could answer this math question which is supposedly a middle school grade question. I had no clue how to solve it so I&#8217;m jobless now and suffering. To this day I never figured out the answer. <a href="http://img691.imageshack.us/img691/2192/mathv.png">http://img691.imageshack.us/img691/2192/mathv.png</a> (The lines are curved if that makes a difference)
</div>
<p>Basically the problem asks you to find the area of a sort-of-square, inside a square of side 4.   Each corner of the outer-square is joined to the adjacent one by an arc of angle pi/2 radians (90 degrees) of radius 4.   The sort-of-inner-square is outlined by the four crossing arcs.  See the <a href="http://img691.imageshack.us/img691/2192/mathv.png">link above</a>, or the picture below for a diagram.</p>
<p>Normally when faced with puzzles of this sort I try and pretend I&#8217;m too busy to figure them out.   Partly because I&#8217;m belligerent, bordering rude, and partly because I know I&#8217;m a bit dumb.   If my life depended on getting the answer I&#8217;d be dead already.   So it was a strange thing indeed that I found myself working on and actually enjoying this particular puzzle.   I did, of course, out of all of my colleagues get to the answer last.  </p>
<p>It then occurred to me that I could regain some cool by trying to draw a better diagram (than the one in the comment) using the Common Lisp vector drawing package <a href="http://www.xach.com/lisp/vecto/">Vecto</a>.   It was actually a lot easier than I thought it was going to be and came out quite nice, the result is here: <img id="image153" style="float:right" src="http://www.hackinghat.com/wp-content/uploads/2010/03/shape1.png" alt="Area of shape" /> the code is <a href="http://github.com/hackinghat/misc/blob/master/common-lisp/area-of-shape.lisp">here</a>.   </p>
<p>After arriving at our respective numerical answers some bright-spark said that it should be possible to verify our results using <a href="http://en.wikipedia.org/wiki/Monte_Carlo_method">Monte-Carlo</a> methods.   I thought about this for a bit and then concluded that it would actually be quite easy to do, especially in Lisp where functions are first class objects.   You can see the code <a href="http://github.com/hackinghat/misc/blob/master/common-lisp/area-of-shape-mc.lisp">here</a>.   </p>
<p>As fun as it was to find the answer by Monte-Carlo it was a bit of a waste of time since I could have just as easily subdivided the area into a grid of tiny squares and then counted the ones that were inside and outside the shape.     This would have been simpler to code and it would have been more accurate too.</p>
<p>Pythagoras would probably have known that of course.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackinghat.com/index.php/lisp/pythagoras-would-puke/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Awesomely awesome in its awesomeness</title>
		<link>http://www.hackinghat.com/index.php/emacs/awesomely-awesome-in-its-awesomeness</link>
		<comments>http://www.hackinghat.com/index.php/emacs/awesomely-awesome-in-its-awesomeness#comments</comments>
		<pubDate>Tue, 09 Feb 2010 11:15:20 +0000</pubDate>
		<dc:creator>Steve Knight</dc:creator>
				<category><![CDATA[emacs]]></category>

		<guid isPermaLink="false">http://www.hackinghat.com/index.php/uncategorized/awesomely-awesome-in-its-awesomeness</guid>
		<description><![CDATA[Sometimes some things are just so good that you have to shout about the fact that it&#8217;s good even though it does no-one any good. Emacs is such a thing. It occurred to me today that I&#8217;d dearly love to write more for this blog, and one way that I might be able to do [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes some things are just so good that you have to shout about the fact that it&#8217;s good even though it does no-one any good.</p>
<p>Emacs is such a thing.  It occurred to me today that I&#8217;d dearly love to write more for this blog, and one way that I might be able to do that would be if I could edit my posts in Emacs.   One web-search later I&#8217;m <a href="http://emacs.wordpress.com/">reading about how trivial it is</a> using <a href="http://www.emacswiki.org/emacs-en/weblogger.el">weblogger</a> and <a href="http://www.emacswiki.org/emacs-en/xml-rpc.el">xml-rpc</a>.</p>
<p>60 seconds after that I&#8217;m up and running, and writing this post.  I didn&#8217;t even have to restart Emacs.   <strong>That&#8217;s how awesome it is.</strong></p>
<p>And to think that I used to berate people who used Emacs because I only used <a href="http://en.wikipedia.org/wiki/Vi">vi</a>.  </p>
<p>So now I&#8217;m praying to the <a href="http://en.wikipedia.org/wiki/Richard_Stallman">saviour</a> for redemption.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackinghat.com/index.php/emacs/awesomely-awesome-in-its-awesomeness/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Mersenne Twister in Clojure</title>
		<link>http://www.hackinghat.com/index.php/lisp/mersenne-twister-in-clojure</link>
		<comments>http://www.hackinghat.com/index.php/lisp/mersenne-twister-in-clojure#comments</comments>
		<pubDate>Mon, 24 Aug 2009 15:29:28 +0000</pubDate>
		<dc:creator>Steve Knight</dc:creator>
				<category><![CDATA[lisp]]></category>

		<guid isPermaLink="false">http://www.hackinghat.com/index.php/uncategorized/mersenne-twister-in-clojure</guid>
		<description><![CDATA[The Mersenne Twiser is a random number generator that has a lot of applications, particularly in finance. After discovering that there was no Clojure implementation at Wikipedia I decided to give it a try as my first attempt at something useful in Clojure. As it turns out it&#8217;s problably not a good candidate to be [...]]]></description>
			<content:encoded><![CDATA[<p>The Mersenne Twiser is a random number generator that has a lot of applications, particularly in finance.   After discovering that there was no Clojure implementation at <a href="http://en.wikipedia.org/wiki/Mersenne_twister">Wikipedia</a> I decided to give it a try as my first attempt at something useful in Clojure.   As it turns out it&#8217;s problably not a good candidate to be implemented in a functional language because the whole thing requires modifying a mutable array for every call to (genrand).     </p>
<p>I&#8217;m not very excited by the solution because it doesn&#8217;t seem very lispy.   It much more resembles the reference implementation, and gives the same results as it for all the tests I tried.</p>
<p>I&#8217;m sure it could be better though.</p>
<p>If it&#8217;s of any use to anyone you can find it<a href="http://github.com/hackinghat/common-library/blob/f50ac4aba31fd161e1e6fd3e27ad265f50dc733a/clojure/mersenne_twister.clj"> here</a>.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackinghat.com/index.php/lisp/mersenne-twister-in-clojure/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>REPL to the rescue!</title>
		<link>http://www.hackinghat.com/index.php/lisp/repl-to-the-rescue</link>
		<comments>http://www.hackinghat.com/index.php/lisp/repl-to-the-rescue#comments</comments>
		<pubDate>Thu, 20 Aug 2009 21:36:20 +0000</pubDate>
		<dc:creator>Steve Knight</dc:creator>
				<category><![CDATA[lisp]]></category>

		<guid isPermaLink="false">http://www.hackinghat.com/index.php/lisp/repl-to-the-rescue</guid>
		<description><![CDATA[Yesterday I watched Dan Weinreb talking about ITA, Lisp, and well, really, really complex stuff The kind-of conclusion that he drew on the future of Lisp is that Clojure is the next Common Lisp. I&#8217;ve been dodging Clojure for about a year now. I bought the book from The Pragmatic Programmers when the book was [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I watched <a href="http://www.youtube.com/watch?v=xquJvmHF3S8">Dan Weinreb</a> talking about ITA, Lisp, and well, really, really complex stuff <img src='http://www.hackinghat.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>The kind-of conclusion that he drew on the future of Lisp is that <a href="http://clojure.org">Clojure</a> is the next Common Lisp.   I&#8217;ve been dodging Clojure for about a year now.   I bought the book from <a href="http://www.pragprog.com/titles/shcloj/programming-clojure">The Pragmatic Programmers</a> when the book was in <em>beta</em> and eagerly followed the examples and decided it had promise.    But, I figured that if I <em>needed</em> to really learn it (and by that I mean use it, not just talk about using it) it would have to come to me.   It would do this, I reasoned, by being hard-to-ignore.</p>
<p>Well there seem to be more than enough smart-folks on-board now (like: <a href="www.lispcast.com">Eric Normand</a>, <a href="http://bc.tech.coop/blog/">Bill Clementson</a>  to name only two) so I guess I better not miss the party.   Not because I&#8217;m smart but because if I don&#8217;t make too much noise I can blend in and no one will notice I&#8217;m there.</p>
<p>So I came to the conclusion that I should try and use it for ad-hoc things that might cross-my-dome and are hard(er) to solve in non-functional languages.</p>
<p>For instance, this very afternoon I wanted to know how many possible hand distributions by suit there are in the card game <a href="http://en.wikipedia.org/wiki/Contract_bridge">Bridge</a>.   So four possible distributions might be:</p>
<ul>
<li>All 13 hearts</li>
<li>12 hearts, one diamond</li>
<li>11 hearts, one diamond, one club</li>
<li>10 hearts, one diamond, one club, one spade</li>
</ul>
<p>The question is how many total distributions are possible?</p>
<p>I banged my head on the table-hard trying to figure out the answer to this.   At first it seemed like a simple counting problem, but if it is I&#8217;m too simple to see it.    Then I wondered if it could be an <a href="http://www.mathpages.com/HOME/kmath091.htm">additive patitioning problem</a>, but ordering is important so I don&#8217;t think it is.   It didn&#8217;t feel <a href="http://en.wikipedia.org/wiki/NP-complete">NP complete</a>.   I know one thing though, at this late hour it might as well be.</p>
<pre><code>1:57 bridge=&gt; (count (for [spades (range 0 14)
                                hearts (range 0 14)
                                diamonds (range 0 14)
                                clubs (range 0 14)
                                :when (= 13 (+ spades hearts diamonds clubs))]
                                [spades hearts diamonds clubs]))
560</code></pre>
<p>Functional programming rocks.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackinghat.com/index.php/lisp/repl-to-the-rescue/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Virtually pure, in a way only C++ can be</title>
		<link>http://www.hackinghat.com/index.php/c/virtually-pure-in-a-way-only-c-can-be</link>
		<comments>http://www.hackinghat.com/index.php/c/virtually-pure-in-a-way-only-c-can-be#comments</comments>
		<pubDate>Mon, 20 Jul 2009 12:13:48 +0000</pubDate>
		<dc:creator>Steve Knight</dc:creator>
				<category><![CDATA[c++]]></category>
		<category><![CDATA[interviews]]></category>

		<guid isPermaLink="false">http://www.hackinghat.com/index.php/c/virtually-pure-in-a-way-only-c-can-be</guid>
		<description><![CDATA[What? C++ was a boon for technical interviewers. There was a time when C++ was about the only viable object-oriented programming language in a business environment. In those days you pretty much had to know C/C++ to be an OO programmer. So you could test someone&#8217;s depth of C++ knowledge (and thus their overall suitability) [...]]]></description>
			<content:encoded><![CDATA[<h2>What?</h2>
<p>C++ was a boon for technical interviewers.   There was a time when C++ was about the only viable object-oriented programming language in a business environment.   In those days you pretty much had to know C/C++ to be an OO programmer.   So you could test someone&#8217;s depth of C++ knowledge (and thus their overall suitability) by just asking about all the messed up edge cases that make C++ really tricky to use.</p>
<p>Today we&#8217;ll be observing a hypothetical candidate Phil.   Phil has been a C++ programmer for 5 years (2 of which were at University) and has rated himself 9/10 on the C++ confidence scale that I asked him about at the beginning.   I&#8217;ve been talking to Phil about the various &#8216;overloaded&#8217; meanings of the keyword <strong>virtual</strong> in C++.  Phil has correctly answered what a virtual method was, but couldn&#8217;t get the one about the virtual base class.   </p>
<p>Even so, Phil is now into his stride and feeling pretty good about himself, so then I lay it on him:</p>
<blockquote><p>Phil, is it possible to ever call a pure virtual method?</p></blockquote>
<p>To which Phil  (and about 95% of other candidates) would heartily reply &#8220;No&#8221;.    And of course Phil would be wrong.  But that&#8217;s ok.   First I want Phil to tell me why a pure virtual method can not be called <strong>usually</strong>.   This  demonstrates a basic understanding, and then I would try and probe to see if Phil can guess the correct answer as to how a pure virtual method can be called and then the reason it&#8217;s possible.    </p>
<h2>So, what?</h2>
<p>If Phil didn&#8217;t get the answer straight away then Phil probably won&#8217;t get it at all.  This is not terribly surprising I suppose, and it&#8217;s not a reason to reject Phil.   The sad truth about being a C++ programmer is that you really need to know how C++ works, at some level, to be able to use it effectively.   I&#8217;m here to find out what Phil&#8217;s level really is.  The more oppurtunities I give him to tell me about those edge cases the better I can assess how good a (C++) programmer he is.</p>
<h2>How?</h2>
<p>So here is a code sample that under a couple of compilers (VC++ 2005 &#8211; Express, and GCC 3.4.4) produces the (un?)desired behaviour.   I wanted to try it under some more recent C++ compiler invocations but don&#8217;t have them available on the machine that I&#8217;m writing this.   Anyway, these two compilers seem fairly representative of what a lot of people are currently using.  </p>
<pre><code>
<font color="cyan">#include&lt;stdio.h&gt;</font>

<font color="yellow">class</font> A {
<font color="yellow">protected</font>:
	<font color="yellow">virtual void</font> Reset() = 0;
	<font color="yellow">void</font> Init()	{
		Reset();
	}
<font color="yellow">public</font>:
	A()	{
		Init();
	}
};

<font color="yellow">class</font> B : A {
<font color="yellow">protected</font>:
	<font color="yellow">void</font> Reset() 	{
		printf("Reset");
	}
<font color="yellow">public</font>:
	B() {
	}
};

<font color="yellow">int</font> main(<font color="yellow">int</font> argc, <font color="yellow">char</font>** argv)
{
	B b;
}</pre>
<p></code></p>
<p>And here is the result:</p>
<pre><code>$ g++ pvirt.cpp
$ ./a.out
pure virtual method called
      9 [sig] a.out 180 _cygtls::handle_exceptions: Error while dumping state (p
robably corrupted stack)
Segmentation fault (core dumped)</code></pre>
<p>So for those, like Phil, not fully aware of the vaguaries of calling pure virtual methods here is the explanation.  </p>
<blockquote><p>Pure virtual methods can be called from <strong>base-class constructors that attempt to indirectly call pure virtual methods.</strong></p></blockquote>
<p>The reason it's a problem is that C++ constructs objects inside-out, so base classes get built before derived classes.   If you then try and call a virtual method on a partially constructed object you will end up calling the base class copy of the virtual method.  This is because at the time you are in the base-class constructor the object is of type base-class and not of type derived-class.    Calling pure virtual methods from constructors sounds like a really bad idea then, and it is.   But there might be legitimate reasons to want to do it, which I'll come to in a minute.</p>
<p>Note that if you try and call the virtual method directly from the constructor, like so:</p>
<pre><code><font color="yellow">class</font> A
{
<font color="yellow">protected</font>:
	<font color="yellow">virtual void</font> Reset() = 0;
<font color="yellow">public</font>:
	A()	{
		Reset();
	}
};</code></pre>
<p>You will probably get a linker error because <strong>Reset()</strong> is pure and has no implementation, as you'd expect.  </p>
<h2>Why?</h2>
<p>So why might you want to do this at all then?   Well say you have a class hierarchy of objects that connect to a database for example.</p>
<p>So you dutifully create a <strong>Database</strong> base class with <strong>MySQLDatabase</strong> and <strong>OracleDatabase</strong> derived classes.    Now when you disconnect an already established connection you need to reset all the structures as if they had just been created.   The object is then ready to be reconnected.    So you create a virtual <strong>Reset()</strong> method in the base class that does this.   At the base level though this class should do nothing and you decide that your derived classes must define such a method so you very cleverly make it pure. You could then (but you probably wouldn't) write code like this:</p>
<pre><code>
<font color="yellow">void</font> Database::Disconnect() {
	Reset();
}</code></pre>
<p>And have the derived class handle the gorey details of the reset.   You want the same behaviour when you create a new object so you simply add the <strong>Reset()</strong> function to the <strong>Init()</strong> function that you created to deal with the fact that you had to write so many different types of copy constructor in C++!  Hey presto pure virtual method called.   </p>
<p>Note that if you had decided not to make the method pure and just put in an empty stub then this empty stub would be called instead (and not the derived version).   In some ways this is a harder error to track because there is no error just a silent refusal by the object system to call the derived version because it can't, because it doesn't exist yet.</p>
<p>The correct way to deal with this, unsurprisingly, is to not call <strong>Reset()</strong> from the base-class <strong>Init()</strong> function but from the derived constructor directly.</p>
<h2>Phil</h2>
<p>Thanks for coming Phil, we'll be in touch.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackinghat.com/index.php/c/virtually-pure-in-a-way-only-c-can-be/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The First Casualty</title>
		<link>http://www.hackinghat.com/index.php/internet/the-first-casualty</link>
		<comments>http://www.hackinghat.com/index.php/internet/the-first-casualty#comments</comments>
		<pubDate>Tue, 16 Jun 2009 21:30:01 +0000</pubDate>
		<dc:creator>Steve Knight</dc:creator>
				<category><![CDATA[internet]]></category>

		<guid isPermaLink="false">http://www.hackinghat.com/index.php/internet/the-first-casualty</guid>
		<description><![CDATA[I had to read this paragraph from the BBC about the recent Iranian elections twice to make sure I understood it correctly: The official said the state department contacted Twitter over the weekend to urge it to delay a planned upgrade that could have cut daytime service to Iranians. So, let me get this straight. [...]]]></description>
			<content:encoded><![CDATA[<p>I had to read this paragraph from the BBC about the <a href="http://news.bbc.co.uk/2/hi/middle_east/8103577.stm">recent Iranian elections</a> twice to make sure I understood it correctly:</p>
<blockquote><p>The official said the state department contacted Twitter over the weekend to urge it to delay a planned upgrade that could have cut daytime service to Iranians.</p></blockquote>
<p>So, let me get this straight.   The US state department monitors Twitter traffic and is reviewing what Iranians are commenting on the topic of their government.    The department wanted to prevent an upgrade to Twitter that might stop the flow of revolutionist propaganda getting back out on the street and starting a larger movement.    </p>
<p>Don&#8217;t get me wrong, it&#8217;s a good call.   It just caught me a little by surprise.</p>
<p>I guess I shouldn&#8217;t actually be surprised by this but it marks a very definite transition (to me at least) for Twitter.   Folks at Twitter have probably been feeling it for years though.   The point is though that it&#8217;s no longer a fun toy.    </p>
<p>Twitter is now, amongst other things, a marketing place for the public, politicians, and companies.   But for government departments it&#8217;s also a socio-political barometer.  </p>
<p>Innocence lost.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackinghat.com/index.php/internet/the-first-casualty/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

