# Nerd wannabe

lazy weblog

## Old: RAII Obliterator Pattern

I implemnted an obliterator for C#. It works around this problem:


foreach (var item in list)
if (item == "two")
list.Remove(item);



System.InvalidOperationException: Collection was modified;enumeration operation may not execute.

The new version of the code is one line longer:


using (var obliterator = Obliterator.From(list))
foreach (var item in list)
if (item == "two")
obliterator.Remove(item);


The Obliterator class :


/* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* http://sam.zoy.org/wtfpl/COPYING for more details. */

using System;
using System.Collections.Generic;

namespace Obliterator
{
public class Obliterator<T> : IDisposable
{
ICollection<T> collection;
List<T> toRemove;

public Obliterator(ICollection<T> collection)
{
this.collection = collection;
toRemove = new List<T>();
}

public void Remove (T item)
{
}

public void Dispose()
{
foreach (var item in toRemove) collection.Remove(item);
}

}

public static class Obliterator
{
public static Obliterator<T> From <T> (ICollection<T> collection)
{
return new Obliterator<T> (collection);
}
}
}



August 22, 2009 at 2:13 pm

Posted in laugh

## spam, rant, or code? (all verbs)

(for those who need help deciding)

1. Put a link as IM ‘status message’ to my first drawing.
2. Then write a rant about OOP.
3. Then announce my first software release.
4. ??
5. Profit!?

February 5, 2008 at 4:05 pm

Posted in laugh

## Think Big [drawing]

You know there is a reason behind people still using e-mail (instead of instant messaging), and other people try pushing REST on us?

Well, e-mail and URLs work even on a connection which has a Big Lag. Or latency. You can write an e-mail now and check for the answer an hour later.

And RESTful sites have the advantage of being cacheable (for read only, of course). So you can run httrack on them and browse them later, or better, put an HTTP cache server in your LAN.

In other words: did you know that the Moon is 384 thousands km away from us? That’s about 1.2 seconds for the light to travel..The distance of Mars from our planet has a minimum 55 millions of kilometers and a maximum of 376 millions.

Thats between 3 and 20 minutes “one way lag” ! Now let me tell you something while I clicked that AJAX combobox…

None of the Science-Fiction books I read featured “out in space” Internet .

Think Big Lag!

January 11, 2008 at 4:46 pm

Posted in laugh, strip

## How does one split 3 bits between two persons?

You noticed it: you had problem with you dial-up, you fiddled with your modem settings, you thought you knew something about how computers work, then it struck you: stop bits: 1, 1.5 or 2

How does one define half a bit?

First, let’s say how do we define a bit: binary digit. In the binary system, only 0 and 1 are digits. So one bit is either 0 or 1… we’re not getting anywhere, aren’t we?

Let’s go further and dig the Shannon definition: the information gathered from tossing a coin, each side having equal probabilities to show up.

So one bit of information is gathered from a coin with equal probabilities for the sides. How would a coin that emits a half bit look like?

What happens if the sides don’t have equal probabilities? Let’s say the probabilities are p1 and p2

$bits = -p_1log_2(p_1)-p_2log_2(p_2)$

For $p_1=p_2=\frac{1}{2}, log_2(\frac{1}{2}) = -1$ therefore the number of bits is $-\frac{1}{2}\cdot(-1)-\frac{1}{2}\cdot(-1)=\frac{1}{2}+\frac{1}{2}=1$ bit. Exactly one bit.

Question is, what are the probabilities the coin’s sides should have to transmit a full half-a-bit?

Given that the two probabilities must sum exactly 1, therefore are $x$ and $1-x$ respectively (just like $\frac{1}{2}+\frac{1}{2}=1$), the problem of how does one split one bit in two becomes

If $-xlog_2(x)-(1-x)log_2(1-x)=0.5$ bits, what’s the value of $x$?

Using an online newton approximation and guessing 0.25 as the ‘seed’ the solution becomes 0.11002786443836 or roughly 0.11.

So, each time a modem wants to send 1.5 bits, it sends a bit and it tosses a special coin that has 0.11 probability to fall on one side and 0.89 probability to fall on the other side.

Now that’s some light shed on modem’s speed bottleneck!