Przejdź do głównej zawartości


Wyświetlanie postów z marzec, 2013

SuperFib - an example of using lexical closure in Python

This story begins with simple implementation of a function that calculates Fibonacci numbers.

def fib(n):
    return 1 if n == 1 or n == 2 else fib(n-1) + fib(n-2)

Assuming that we are using 1-based indices, we can run a couple of tests and see that it works. Of course, fib(35) takes some time to calculate - about 15 seconds on my laptop.

Now, if you don't know what lexical closure is, I recommend reading about it on Wikipedia) before going further. Let's try to cache the results, but also be better than using conventional memoization - not only will we cache the final result of each call, but also intermediate results.

def fib():
_dict = dict()
    def inner(n):
        nonlocal _dict
        if n == 1 or n == 2:
            return 1
                return _dict[n]
            except KeyError:
                _dict[n] = inner(n-1) + inner(n-2)
                return _dict[n]
    return inner

f = fib()
f(1000) returns instantly, with:

Unit Testing code with IO file operations (in Python)

We may often come across a piece of code that was written without Unit Tests at all. In addition, the piece of code may be dealing with IO like file writing and reading, which makes it more difficult to Unit Test it when we are trying to refactor and modify. Let's suppose the code in question looks like this:

def writeInitialsToFile(filename, name, surname):
    initials = name[0] + '.' + surname[0] + '.'
    with open(filename, 'w') as file:

def readInitials(filename):
    initials = None
    with open(filename, 'r') as file:
        initials = file.readline()
    return initials

A straightforward and bad idea would be to write a couple of Unit Tests that make use of a real file and simply test the reading and writing. Is therea a better way to test this code?

First of all, we need a way to replace the real file with something else. For both reading and writing we will now have a couple of functions, one that expects a stream fo…

Long if-else statement without if (in Python)

Let's suppose we find ourselves in a situation where we have no other option than to write a lengthy if-else statement (definitely not something that would make cleaner by polimorphism).

def inevitable_long_if (n):
    if n == 0:
    elif n == 1:
    elif n == 2:

Can we re-write it so that we do not use IFs?

import collections
import functools

d = collections.defaultdict(lambda : functools.partial(print, 'Manu-manu'))
d[0] = functools.partial(print, 'Zero')
d[1] = functools.partial(print, 'One')
d[2] = functools.partial(print, 'Two')

Now, let's call some to see how it works:

d[0]()  ==>  Zero
d[1]()  ==>  One
d[2]()  ==>  Two
d[7]()  ==> Manu-manu

The first version is 9 lines long, the second one (without imports) is 4 lines long. To me, the second version is also much more readable, but you decide.

Scrum? Yes, I'll have one, please!

There is one of those soft skills training like "Conflict management" or "Team building". The trainer is a woman with psychological background. After the usual part that is delivered during every soft skills training (and, surprisingly, this part is almost always the same on every training) there is a break. The trainer asks people what they do in their job and it may well be that she is really interested in what people say, or maybe she's just pretending. Regardless, she goes like this:

- Could you tell me more about this Scrum thing you mentioned? How do you do it exactly?

One of the trainees feels he's the one who must speak up:

- Well, we meet every day at 9:00 and we talk briefly what we did yesterday and what we have to do today and whether anyone has any obstacles. This is called "stand-up". Also, the whole project is divided into a number of short periods called iterations and there is the planning at the beginning of each and (...)

The tr…

Pure functions in Object Oriented World

Let's suppose we have a class called Time with method convertToGMT(...). The method has one parameter called time, it converts the parameter to the GMT and returns the result.

For some reason (and please let's don't try to question this reason for the sake of this short exercise) the actual time zone of the caller is an instance variable of the Time object. It may be set when the object is constructed or re-set afterwards.

convertToGMT simply checks the time zone by accessing the instance variable timezone, performs calculation and based on that returns the GMT result.

When writing a couple of unit tests to test how convertToGMT works for different time zones, we would have to repeatedly:
set the time zone by calling the presumed Time.setTimeZone(...) methodcall convertToGMT and check the result We would do that for each time zone - time-to-be-converted pair we want to test. Clearly, this function is not a pure function, since its result depends on the state of the object. …

Clojure is great!

I've been playing with Clojure for a couple of months now and I want to share with you some thoughts why I think it is a great language.

Clojure is a Lisp

Many wise people have written great things on Lisp, so I'm not even trying to present arguments for it; just try out these:
http://www.paulgraham.com integrates easily with Java libs

Everybody knows it. Nothing interesting to add here.

Clojure implements modern concurrency techniques

Clojure implements STM( It features Atoms (abstractions for managing state synchronously), Agents (abstractions for managing state asynchronously, by applying function to agent's state), and lock-free handling of scenarios that would have to be handled with mutexes in other programming languages.

Don't be deceived that Clojure is "nice, because it revitalised Lisp or JVM". The key point is that it features modern approach to concurren…

Code Kata: Eating habits and Markov chains

The Wikipedia entry:
contains nice and clear explanation of the idea of Markov chains. Based on the paragraph about eating habits, we can form an interesting coding exercise:
A person eats lunch every dayWe form a set of rules similar to the one presented in Wikipedia - one part of the exercise is how to represent these rules in a program. What would be the convenient way to represent this internally and how would you translate a possible textual representation (such as one entered from standard input) into the internal format? Would the two format be quite similar or quite different from each other?We run a simulation for a number of days - say, 365 days - and we get statistical distribution of the number of each type of meals that would be eaten Does it seem like a long program to write? My initial solution in Python was 23 lines long and then refined solution took just 14 lines.