Wednesday, July 11, 2012

PHP and consistency? No way

Today I was debugging my PHP code and trying to figure out why my references (quite nasty feature of PHP but sometimes required) do not work only to learn that my bug was in array_key_exists function call.

So, try to guess what are the arguments for array_key_exists. If you think it is

array_key_exists($array, $key)

you are quite wrong, just read the documentation:

array_key_exists ( mixed $key , array $search )

You may say, ok, this is just a convention. Well, not exactly, looking at the manual, one can summarize the functions as following:

array array_change_key_case ( array $input [, int $case = CASE_LOWER ] )
array array_chunk ( array $input , int $size [, bool $preserve_keys = false ] )

vs

array array_combine ( array $keys , array $values )
array array_fill_keys ( array $keys , mixed $value )
mixed array_search ( mixed $needle , array $haystack [, bool $strict = false ] )
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )


But the best part is this (can you spot the black sheep?):
array array_filter ( array $input [, callable $callback = "" ] )
array array_map ( callable $callback , array $arr1 [, array $... ] )
mixed array_reduce (array $input, callable $function [, mixed $initial = NULL ] )

In summary, PHP requires a manual, unless you are good at guessing/coin flipping.

Bonus from the manual:

string implode ( string $glue , array $pieces )
string implode ( array $pieces )


Join array elements with a glue string. Noteimplode() can, for historical reasons, accept its parameters in either order. For consistency with explode(), however, it may be less confusing to use the documented order of arguments.

Sunday, July 8, 2012

Grand St Bernard pass on a bike




On Saturday Maciej and I went to a quite challenging bike trip from Orsieres (cca 900m) to St Bernard pass (2470m). It was very good trip. We started at Orsieres and used forest roads along the river all the way up to Bourg St Pierre (except for a special "bonus" section right before Pont de la Tsi where we needed to carry our bikes 200m uphill on a hiking trail with traverses and wooden steps).




From there, we continued to dam of Lac des Toules, where we climbed up and started second bonus part -- trail along Lac des Toules is just a big hiking trail and we were glad that most of the part was downhill. After the dam, we crossed the river and tried to go to Bourg St Bernard. Except ... it wasn't easy. There were two possibilities: a) cross another stream on an iron bar probably 30cm wide which would be fine on foot but a crazy idea on bikes b) cross the stream on a very inconvenient place c) take stairs to the roof of the tunnel. We took last option in the hope that it was designed as a hiking trail -- a big mistake. The roof wasn't particularly good for biking (or hiking for that matter). So we went down on the other side of the tunnel but our problem remained -- there was a road but there was also a big stream through it. After enumerating possible choices, we went for the easiest -- cross the stream with naked foot. And it was really cool (or I should say really cold -- water was probably from melting snow). This was however final obstacle and we reached Bourg St Bernard (1900m) where the majority of cars go to the St Bernard tunnel and thus there is less traffic on the old road. There we had a lunch and started the last part of our trip -- final (and quite hard) 600m of ascent on an asphalt road.


But it was worth the sweat. The views from St Bernard pass were amazing. After we went a few hundreds of meters in Italy, returned back, shopped for souvenirs and visited church with a display of old relics, it was time to go down. This time, we chose the main road all the way down.


Hungry for more photos? Here they are.

Vim, your gpx swiss knife

Did you ever have a gps file which was consistently moved from the map by few meters?
It happens mainly if you are travelling fast and the gps tries to catch up after tunnels or other losses of a signal. But now there is universal solution to it. And it is in VIM!

Just get the offset (in lat/lng), open file in vim, select problematic region (it helps if the file is not in one line as some programs prefer) and run following mega-command:

:'<,'>s/lon="\([0-9.]*\)"/
\='lon="'.printf('%.10f',str2float(submatch(1))+0.002).'"'/

To explain few things:
:'<,'> matches on selected lines
s/xxx/yyy/ replaces regrexp with yyy
\(xxx\) is a submatch
[0-9.]* matches floating point value
\= tells vim to use expressions instead of strings while replacement
'xxx' is a string expression
. concatenates string expressions
str2float(submatch(1)) gets a floating point value of the matched latitude/longitude

Tuesday, July 3, 2012

Ppershing:Sustenpass 0:1

Welcome to the match between Ppershing and Sustenpass. The bicycle is starting at home field near Realp and PPershing is pushing fast towards Andermatt. There, he kicks in the direction of Göschenen and bike is going fast downhill. The bike is near Göschenen and the player is kicking ... nooo, in the wrong direction. The bike is slowly climbing towards Göschneralp, through high grass. Bike and the player are in the off-road position. Finally, the touchdown in Göschnealp and half-time break. After the game is restarted, bike is again kicked towards Göschenen and from there to Wassen. And here Sustenpass starts actively playing. Sustenpass is defending by off-road and gradient tactic. Ppershing is clearly out of breath and he is not able to make any long kicks. In fact, several times he is just pushing the bike by hands. And the game is near the end -- the reserved time is almost over. It is unlikely that Ppershing will score in this game. In order to avoid even bigger defeat, Ppershing ends at 1600 metres out of 2200 and returns to home. The result of game is 0:1 but PPershing says he will try it again and will be better prepared.

Photos from the match

He's half-dead, Jim

Computers never fail to impress me. At least mine computers. They always have some new way of failing. For example, yesterday my old laptop (really a piece of crap now, but I use it as my desktop) started running only on the battery. Hmm, wait, why is my laptop running on the battery, especially when it is plugged into the power? This happened a few times ago because of the overheating. However, right after the start there is a little change of overheating. So what is the problem? Finally my power source broken down? Or maybe n-times patched connector? Well, I just shot down the computer and ... surprise ... the battery is charging. What the heck? I turn on the computer and ... AC power led just dies. Veeery strange. Anyway, it seems that my power source is half-broken and not able to produce enough voltage/current. Therefore, the laptop decides to go with the battery which is after 5 years almost dead too. A very unlucky combo...

So, my third option -- go without battery. It seems that this is the most promising solution -- when laptop dehibernates, it seems to be running without any problems. Except for the fact that it dies with high probability (which can be reduced by plugging out usb devices) during the dehibernation process.