# BRB – just a mess around

Spent one hour this weekend for this small messaround. Got a silly time killer. Don’t hate me:

http://www.zabolotny.com/brb.html

I stumbled upon the idea for this utter silliness a while ago, but cannot find (well, i was not looking hard enough i guess). So decided to code it myself (which means the code is waaay better).

Ok, see you.

# The Stanley Parable

Oh i have written code today, alright. Whole day. Cracked the nut just two minutes before six. What a joy, when you do something that actually works, despite all odds and… inconveniences (i don’t want to rattle again about how “good” open source products sometimes are, there are too much complains in this blog already, and besides, i could just go and fix it instead of mumbling, it is free after all).

So instead, i would rather tell about the game. It’s called The Stanley Parable. And it is simply brilliant. The problem i face is that this is all i can tell you about it. Go play.

# Recursive C #2

All day today i’ve been thinking how to implement `reverse()` function from yesterday post using no helper code. Still no result though. Except the realization, that this whole helper is very slow. Let’s make a test: reverse 103 character string 106 times, shall we?

```time ./test2

real	0m26.846s
user	0m26.810s
sys	0m0.016s
```

Wow, that’s a lot. The reason for that is obviously the fact, that we call `strlen()` each time `_rev()` is called and `strlen()` has linear running time, so we are just adding that running time ever `_rev()` invocation without any gain because string length do not change during `reverse()` call. Not cool. We can add another constant parameter to carry initial string length:

```static void _rev(char s[], int i, const int l) {
int k = l - 1 - i;

if (i > k) return;

char c = s[i];
s[i] = s[k];
s[k] = c;

_rev(s, ++i, l);
}
```

And then:

```void reverse(char s[]) {
_rev(s, 0, strlen(s));
}
```

Quite simple. Here’s how it performs:

```time ./test1

real	0m2.830s
user	0m2.825s
sys	0m0.002s
```

Fastaaaa! Mind you, that’s recursive solution which has function call overhead (tail recursion, anyone?) and is just an exercise.

So yeah. Optimize-shmoptimize.

Just started the preprocessor chapters. That should be even funnier, because of all the bugs possibilities. Really looking forward to it.

That’s it for today.

# Recursive C

Continuing with C. Last chapter i’ve finished is Recursion. There is an exercise which is to implement recursive function for reversing string in place. Here’s my solution:

```void reverse(char s[]) {
static int i;
int k = strlen(s) - 1 - i;

if (i > k) {
i = 0;
return;
}

char c = s[i];
s[i] = s[k];
s[k] = c;

i++;
reverse(s);
}
```

Which is a bit… cheating. And that’s due to the fact, that function is not supposed to keep any kind of state. Look at `static int i;` which i use to keep track of which position is currently exchanging. This is not very good. Is there a better way? If i would be using Scala, i could just create nested function. In C i have to use `static` function, which is essentially private.

Here is goes:

```void reverse(char s[]) {
_rev(s, 0);
}

static void _rev(char s[], int i) {
int k = strlen(s) - 1 - i;

if (i > k) return;

char c = s[i];
s[i] = s[k];
s[k] = c;

_rev(s, ++i);
}
```

Funny enough changing `++i` in `_rev()`‘s last line, to `i++` will lead to Segfault because recursion never ends. Why not stack overflow? Here is why.

That concludes recursion for now. Source file here: reverse.c.

That’s it for today.

# Playing with Unity

Today i was playing with Unity. This is a game engine, which is simple to use and it can run on almost any platform (yeah, even WP8 and Wii). Sure enough this comes with it’s own price, both dollar and performance, though i don’t have any hard numbers on latter. But just the ability do “write once, run anywhere” is head-lightening. Especially because Windows Phone.

Just for the sake of learning something new i have followed a simple tutorial on Unity site on how to build collecting game. You control the ball with arrow keys and collect spinning boxes. Got them all – you win. It’s rather primitive, i mean, i can’t even play with friends or take payments from those players who have stuck. Anyway, here is the link to the game itself: Rollerball (needs UnityPlayer). Just don’t get too addicted.

Code is on the github. It uses C# since that what they have used in tutorial, but Javascript will do too (i guess they can be mixed in one project, however i don’t see why anyone would do that).

Building games is really hard task, even having tools like Unity, which does tons of work for you. Design, arts and testing will take years, unless you are doing time killer game, in which case it will take only months.

That’s it for today.

# One day one code #17

Here we go again, skipped 5 days. Had a very nice trip to California: San Francisco, Mountain View, Santa Cruz and Carmel. I’ll have to write a post with photos later.

Today is ODOC time. I am done with the reverse Polish calculator from the last update. Here is it: calc.c. It wasn’t too hard. The main problem is lack of free time, frankly. As soon as you understand how things should work, it’s easy to extend existing solution.

Historical note: Reverse Polish notation

Switched from Vim to MacVim, which has all the same great features plus:

• point and click;
• mouse scroll;
• “usual” copy-paste shortcuts.

That’s it for today.

# One day one code #16

Time machine update: i’ve managed to get almost all my data back. Right now all 45Gb of music is downloading. Also in this time of cloud based services, like password managers and Dropbox, there is not many things you can actually loose. So computers are pretty much disposable (like Apple wants you to think about iPhone), it’s so easy to upgrade and safe. Almost always.

Back to the main topic of this blog. Coding. Bunch of stuff going on with Sonata. I am still pretty serious about writing a post about that later someday. On the other hand C learning is not going too fast. But i have managed to make progress on the calculator. Added modulus operation, negative numbers support. They also have added stack operations as a part of this exercise, so get(), clear(), swap() etc. are done too. The code is not thread safe by the way. Which is something i recon is hard to do (and is out of scope of current chapter). I will post the code in this blog when all things are done.

Tomorrow i am visiting San Francisco. So most probably will not have too much time to spend on coding. So i will post code-related photos.

# Insert title here

You know how you supposed to make backups to keep all your data safe and warm? If you don’t – trust me, you have to do them. But here is a catch (how come that there is always a catch in this world?) – even if you do make reserve copy (or even several of them) – does not necessarily mean you are on the safe side. How is that? Very simple: software is broken. Which brings me to this comprehensive post from Scott Hanselman. Go on, read it and don’t tell me he is not right.

I’ve been using Macs for at least five years now. And rarely had any problems. Until now. Bought a new Mac (retina if you ask me) and certainly wanted to get all my stuff quickly from Time Machine. Except that Migration Assistant did not find any valid data on the storage. Weird. Tried several things here and there which did not lead to anything… I don’t say there is some critical stuff, but photos and documents are nice to have. Also rsa keys. And Kerbal Space Program saves.

After some digging found James Pond site, stuffed with Time Machine troubleshooting information. According to him, what happened is backup process failed to finish for any reason (although i do remember waiting for it to stop) and creating invalid records. Every other (finished) copy of data should be fine, just the last one is corrupted. So here i am, copying my home folder from network storage (which is my TM disk) manually. It will take just three hours or nine. And it better work.

Will see tomorrow.

On the code side: been playing with PHP stuff all day at work. Got a good piece done, but it doesn’t work because of some crazy issue with Document mapping in Symfony CMF. That stuff is seriously decoupled. Have to spend lifetime to get it to behave. I will write a separate post about it someday maybe to help guys like myself (or future me) understand it better. But like i said yesterday – best docs for open source is the code itself.

That’s it for today.

# One day one code #13-14

Yep, i skipped a day. But because I was writing code, a lot. But mostly PHP. A lot of PHP and YAML. Not much time to learn C though.

So, the summary:
– DI is a bitch – sometimes it’s really hard to get it right;
– open source documentation is a bitch – have to read code, that’s the best documentation;
– i do complain too much.

Next step – get that calculator done. At least the is no DI containers.

That’s it for today.

# One day – one code #12

Can you believe it? It’s been almost two weeks. I am starting to feel like i am writing “learn something in 21 days” book. Maybe some day…

Right. Today i was doing some C stuff again, since this is the main goal of all this nonsense. By the way – there is only half of the book left to read and i don’t know what’s going to happen next. Guess that would be perfect time to use the knowledge from the book.

Right, so i am in a middle of functions chapter and i have to extend the calculator which is given as an example. The biggest challenge will be to add functions and variables. Because this requires syntax convention, and, well, implementation. Or maybe it’s easier: just detect the non-digit characters and check if there an opening parenthesis for function… i’ll think of something, but tomorrow. Today i just fixed atof() function to handle scientific notation.

Some time ago i’ve bought Steve Wozniak’s autobiography “iWoz”, which is a big fun to read, so i’ve spent several hours reading it today instead of coding. You can find it on Amazon used for about \$10 and i would recommend reading it (i find it a lot more interesting read than “Steve Jobs” book).

That’s it for today.