Solving a math puzzle with Python’s generator expressions

Recently James Grime of numberphile-fame posted a puzzle on YouTube. The video is right here but the question is (if you don’t care to watch): “How many ways are there to completely fill a Noughts and Crosses (tic-tac-toe) board, with four noughts and five crosses? Not including rotations and reflections.”

So I am not much of a mathematician and I also did not really care for fopping about much so I wrote a small program in python to find the solution. And I took the chance to make a post about generator expressions in python as well.

A generator expression can take two general forms:

def gen(count):
  c = 10
  while c > 0:
    yield c
    c = c - 1

Here the yield keyword is what it’s all about. This will generate an iterable object that returns the yielded items. The cool thing is, that computation of this method is suspended until the next item is needed.

If you already have an iterable you can create a new generator expression from it:

g = (x for x in gen(10))

Here you can filter or map the value of x as you will see. And again, this is only a generator. No computation happens unless you actually want to print any values or you manifest them into a list.

you will find the source code of my solution below the fold

Weiterlesen

Using ExtJs’ bind to partially apply a function

Because of my work I dabbled a bit in ExtJs and I wanted to show you something that I need quite regularly. It has become common practice in JavaScript to pass around function object like no man’s business. Which is quite alright. But sometimes you prepare a function for another library. Usually this looks something like this:

var somefunction = function (arg1, arg2, arg3, arg4, arg5) {
  console.log(arg1, arg2, arg3, arg4, arg5);
};

var caller = function (fObject) {
  return fObject(1,2);
};

caller(somefunction);
// 1 2 undefined undefined undefined

But you can’t pass any argument to somefunction (because JavaScript is not Haskell and you cannot build partially applied thunks). So how do we partially apply some values? ExtJs gives us quite a powerful tool:

caller(Ext.bind(somefunction, undefined /* this is the bound scope which, in this case, is not needed */, [3,4,5], true));
// 1 2 3 4 5

Usually, you would bind the scope to “this” but in this rather trivial case, we don’t need it. The “true” at the end is to tell Ext to append the new parameters (3, 4 and 5) rather than overwriting the existing ones. But we can put them any place we want.

caller(Ext.bind(somefunction, undefined, [3,4,5], 1));
// 1 3 4 5 2

We also can, which is rather cool, apply them in instead and therefore “cancel out” other paramters by leaving the last bind-parameter as undefined:

caller(Ext.bind(somefunction, undefined, [3,4,5]));
// 3 4 5 undefined undefined

You can, of course, save your partially applied function objects:

var partiallyAppliedFunction = Ext.bind(somefunction, undefined, [3,4,5]);
partiallyAppliedFunction();
// 3 4 5 undefined undefined

But remember: The function is saved as reference at binding time (and not by name). Therefore changing the original function won’t change anything:

somefunction = function () { console.log('I no longer do as I am told.') };
partiallyAppliedFunction();
// 3 4 5 undefined undefined

You can also construct function objects from anonymous functions/closures that way, for whatever reason you would need that:

var SomeBoundAnonymousFunction = Ext.bind(function (a) { console.log(a); }, undefined, [3,4,5]);
SomeBoundAnonymousFunction();
// 3

cool, huh?!

Cobalt-Turnier

Am Samstag findet bei mir ein großes (8 Spieler/4Teams) Cobalt-Turnier statt. Das Ganze wird dann ab 17 Uhr mit Webcam live auf http://de.twitch.tv/maweki gestreamt. Wir freuen uns auf reges Zusehen.

 

Constructors in Python: overwriting __new__ and the singleton pattern

About all object-oriented languages allow you to overwrite the constructor of a particular self-defined type. In Java (and C#) this looks somewhat like that:

public class A(object) {
  constructor A() {
    // Do Something
  }
}

If you think the parameters somehow merit the abortion of the object creation, you can only throw an exception. You only can do that. An object of (in this case) type A is always returned. You can’t even return a different object or an object of a subclass of A. This convention led to the factory pattern in which a static method is called to decide which specific constructor of which specific class to run. Since the method is declared as returning the supertype, it can return any subptype as well. But the factory-pattern can sometimes get out of hand.

If you get started with python you commonly see __init__(self [, args]) as the method you need to overwrite in order to get a constructor.

class A(object):
  def __init__(self):
    pass

And it is true that this is exactly what is written above.
Note: in python, methods are objects (and could be swapped any time). Therefore the own instance cannot be bound outside the method scope and it is always passed by the calling instance as the first parameter. Therefore any non-static method of a class starts with “self” (or “s” or whatever you want to call it) as its first parameter.

So __init__ is, as is the constructor in C# or Java, only called when the instance has already been created. So why is it called a constructor then? Historical reasons, for one. But with statically typed languages you can overload the constructor and build your instance from different parameters. A dynamically typed language like python has no method overloading. It is still good for initializing properties of the particular instance.

But there is another cool method in python which you can overwrite: __new__(C [,args]) in which the class to create an instance of is passed in C and the rest of the arguments in args. And now you can build something like this:

class A(object):
  pass

class B(object):
  def __new__(C):
    return A()

In statically typed languages it is almost always true that A() returns an object of type A. But in python, this does not need to be true. You can put your factory right there as the constructor of the superclass and it looks really neat.

Weiterlesen

Twitter ist… php-Bashing

https://twitter.com/gr4y/status/296543206361948161

Unnamed Jump and Run – Ein Autorensysteme-Projekt von iScraM und Nulzagu

Manche Menschen haben das Glück, nicht mit Adobe Director arbeiten zu müssen. Nicht so iScraM und Nulzagu. Ich bewerte ihre Leistung auf einer Skala von 1 bis nicht-schlecht.

Blackhole Defense – Ein Autorensysteme-Projekt

Lucky Lucas und Ich hatten dieses Semester das unglaubliche Glück, mit Adobe Director programmieren zu dürfen. Das ist unser Ergebnis: Blackhole Defense

Ja. Die Soundeffekte habe ich selbst “eingespielt”.