Scala: Implicit Classes

Implicit classes in Scala are an underused and oft misused feature. I don’t feel that they are explained very well and have a bit too much mystery for the beginner to grasp. I was playing around and thought it might help to show the explicit syntax, to more easily explain the “magic” to someone.

In a nutshell:

import scala.language.postfixOps

object Factorial {

implicit class IntDecorator(val n: Int) {
def ! = 1 to n product
}

def factorial(n: Int) = n! // implicit

def factorial2(n: Int) = IntDecorator(n).! // explicit

}

Bareback Academy

I recently went on a domain name shopping spree. My favorite, my flagship, is Incompetent Ninja.

IncompetentNinja

It wasn’t intentional, but the irony was so self evident. I had plans for the domain, but I think I just might leave the error page for my vanity. Fuck it.

So, I have moved on to Bareback Academy. This thing I want to do right. I’ve made it a project to attend to when I’m bored out of my mind.

I have end-to-end IPv6 from my desktop to my data center. I upgraded my home network. The HTTP servers are fronted by CloudFlare with mandatory SSL and, of course, IPv6 + SPDY + DoS protection.

RegexToolbox

RegexToolbox

I wrote a widget a few years back. Apple removed my hosted file storage, so it’s become unavailable and I moved it to GitHub. It’s always been free, despite the donation link that’s earned me $6.00 over several years.

It’s not great nor amazing, it’s just a simple tool that got me through a lot of work. Many complaints came from this widget, telling me that I should kill myself. Now the haters can send me pull requests.

xrange, the PHP extension that I "maintain"

A few years ago, after being bewildered by infinite loop problems and convoluted paging algorithms, I wondered why the PHP community hadn’t adopted iterator based looping and sunset the three-statement for-loop like Python.

Some of the resistance I encountered were arguments regarding memory efficency & performance using the range() function. These were the days where PHP lacked generators and yield statements.

In Python’s image, I built xrange(). I wrote an article, Killing The For Loop, about it in PHP|Architect magazine (when still in print) and published my extension into the PECL respository.

In combination with PHP’s SPL, you can do some quite amazing things. For those that don’t know, SPL is the PHP “equivalent” of C++’s Standard Template Library. It offers generic algorithmic data structures, iterators and such. Following is a contrived example, using one of my own package supplied filters.

<?php
// vim: set ts=4 sw=4 noet:
assert(extension_loaded('xrange'));

// display all odd numbers 1 .. 20
print_r(array_values(
iterator_to_array(
new OddFilterIterator(xrange(1, 20))
)
));

The real power comes from chaining, slicing, appending and filtering iterators together. Creating large batch sets of numbers together without any memory overhead.

Last I heard, it’s still a standard package in the SUSE Linux repositories. You can still use it, include the source I published in the article, or adopt the modern generator syntax… which does not work with SPL natively in this way.

Anyone want to take this over??

Almost two years later, the Boost C++ Library project finally merges my patch

Almost two years ago, I was using Boost’s PropertyTree library as a JSON serializer. It had a bug that included extra whitespace in the output, when disabling the “pretty” option. I submitted a patch.

I implemented it locally, to reduce bandwidth for a game title I was working on. The bandwidth savings add up, given the frequency of data refresh. It’s a simple fix.

diff -dur boost.old/property_tree/detail/json_parser_write.hpp boost.new/property_tree/detail/json_parser_write.hpp
--- boost.old/property_tree/detail/json_parser_write.hpp 2012-07-26 19:42:10.000000000 -0700
+++ boost.new/property_tree/detail/json_parser_write.hpp 2012-07-26 19:43:34.000000000 -0700
@@ -93,7 +93,8 @@
stream << Ch(',');
if (pretty) stream << Ch('\n');
}
- stream << Str(4 * indent, Ch(' ')) << Ch(']');
+ if (pretty) stream << Str(4 * indent, Ch(' '));
+ stream << Ch(']');

}
else

It was finally merged to master five months ago.

I you work in C++ and haven’t tasted the Boost libraries, I’d highly encourage you to do so. Modern C++ doesn’t resemble old school C in the slightest. Some of my most favorite libraries in the world have been included in the C++11 standard.

This was done, and made possible, by Adobe’s FlasCC initiative. KIXEYE ported all of their ActionScript to C++ to run both on the client and server with a shared code base, during my multiplayer implementation project, that I lead. The game still runs in the Flash Player… with more explosions.