tag:blogger.com,1999:blog-3327089668500969982024-03-13T19:19:52.199-07:00Meme Urgency Broadcast Networkoperatorhttp://www.blogger.com/profile/13927814241268465006noreply@blogger.comBlogger9125tag:blogger.com,1999:blog-332708966850096998.post-88823818137294730112012-12-01T12:21:00.001-08:002012-12-01T12:27:10.349-08:00invoking methods on php's stdClass<span style="font-family: Georgia,"Times New Roman",serif;">I figured out a way to add methods and call them on the stdClass object, using anonymous functions:</span><br />
<span style="font-family: "Courier New",Courier,monospace;"><br />$obj = new stdClass();<br />$anon = function ($value) { return "My value is $value." ; } ;<br /><br />$obj->value = "a";<br />$obj->method = $anon;<br /><br />$method = $obj->method;<br />echo $method($obj->value) . "\n";</span>operatorhttp://www.blogger.com/profile/13927814241268465006noreply@blogger.com0tag:blogger.com,1999:blog-332708966850096998.post-80903343389989977472011-04-05T20:18:00.000-07:002011-04-06T07:10:03.325-07:00Migrating an Adjacency List<span style="font-style:italic;">Adjacency lists </span>and <span style="font-style:italic;">modified preorder trees</span> are two ways of storing nested hierarchies in a relational database. For some good background on these structures, see <a href="http://dev.mysql.com/tech-resources/articles/hierarchical-http://www.blogger.com/img/blank.gifdata.html">here</a>.<br /><br />So the adjacency list is probably one that's intuitive and easy to come up with if you are to to the field. However, once you learn the benefits of the preorder tree, you'll probably want to migrate. But the migration is not so easy as a few queries -- you'll need an algorithm to do so, and a recursive one at that.<br /><br />Here's a couple stored procedures for MySQL that will migrate data from an adjacency list to a modified preorder traversal tree:<br /><br /><br />First I'll set up an adjacency list (language family data from wikipedia):<br /><br /> <code><pre> CREATE TABLE IF NOT EXISTS `language_family_adj_list` (<br /> `language_id` int(11) NOT NULL auto_increment,<br /> `language` varchar(20) NOT NULL,<br /> `parent_id` int(11) default NULL,<br /> PRIMARY KEY (`language_id`)<br /> ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=41 ;<br /><br /><br /> INSERT INTO `language_family_adj_list` (`language_id`, `language`, `parent_id`) VALUES<br /> (1, 'Finno-Ugric', NULL),<br /> (2, 'Hungarian', 1),<br /> (3, 'Khanty', 1),<br /> (4, 'Mansi', 1),<br /> (5, 'Permic', 1),<br /> (6, 'Mari', 1),<br /> (7, 'Mordvinic', 1),<br /> (8, 'Sami', 1),<br /> (9, 'Baltic-Finnic', 1),<br /> (10, 'Komi', 5),<br /> (11, 'Komi-Permyak', 5),<br /> (12, 'Udmurt', 5),<br /> (13, 'Erzya', 7),<br /> (14, 'Moksha', 7),<br /> (15, 'Western Sami', 8),<br /> (16, 'Eastern Sami', 8),<br /> (17, 'Southern Sami', 15),<br /> (18, 'Umi Sami', 15),<br /> (19, 'Lule Sami', 15),<br /> (20, 'Pite Sami', 15),<br /> (22, 'Northern Sami', 15),<br /> (23, 'Kemi Sami', 16),<br /> (24, 'Inari Sami', 16),<br /> (25, 'Akkala Sami', 16),<br /> (26, 'Kildin Sami', 16),<br /> (27, 'Skolt Sami', 16),<br /> (28, 'Ter Sami', 16),<br /> (29, 'Estonian', 9),<br /> (30, 'Finnish', 9),<br /> (31, 'Ingrian', 9),<br /> (32, 'Karelian', 9),<br /> (33, 'Livonian', 9),<br /> (34, 'Veps', 9),<br /> (35, 'Votic', 9),<br /> (36, 'South Estonian', 29),<br /> (37, 'Voro', 36),<br /> (38, 'Karelian Proper', 32),<br /> (39, 'Lude', 32),<br /> (40, 'Olonets Karelian', 32);</code></pre><br /><br />Here's a query to demonstrate:<br /> <code><pre><br /> mysql> SELECT t1.language AS lev1, t2.language as lev2, t3.language as lev3, t4.language as lev4, t5.language AS lev5<br /> -> FROM language_family_adj_list AS t1<br /> -> LEFT JOIN language_family_adj_list AS t2 ON t2.parent_id = t1.language_id<br /> -> LEFT JOIN language_family_adj_list AS t3 ON t3.parent_id = t2.language_id<br /> -> LEFT JOIN language_family_adj_list AS t4 ON t4.parent_id = t3.language_id<br /> -> LEFT JOIN language_family_adj_list AS t5 ON t5.parent_id = t4.language_id<br /> -> WHERE t1.parent_id IS NULL<br /> -> ORDER BY t1.language, t2.language, t3.language, t4.language, t5.language;<br /> +-------------+---------------+--------------+------------------+------+<br /> | lev1 | lev2 | lev3 | lev4 | lev5 |<br /> +-------------+---------------+--------------+------------------+------+<br /> | Finno-Ugric | Baltic-Finnic | Estonian | South Estonian | Voro |<br /> | Finno-Ugric | Baltic-Finnic | Finnish | NULL | NULL |<br /> | Finno-Ugric | Baltic-Finnic | Ingrian | NULL | NULL |<br /> | Finno-Ugric | Baltic-Finnic | Karelian | Karelian Proper | NULL |<br /> | Finno-Ugric | Baltic-Finnic | Karelian | Lude | NULL |<br /> | Finno-Ugric | Baltic-Finnic | Karelian | Olonets Karelian | NULL |<br /> | Finno-Ugric | Baltic-Finnic | Livonian | NULL | NULL |<br /> | Finno-Ugric | Baltic-Finnic | Veps | NULL | NULL |<br /> | Finno-Ugric | Baltic-Finnic | Votic | NULL | NULL |<br /> | Finno-Ugric | Hungarian | NULL | NULL | NULL |<br /> | Finno-Ugric | Khanty | NULL | NULL | NULL |<br /> | Finno-Ugric | Mansi | NULL | NULL | NULL |<br /> | Finno-Ugric | Mari | NULL | NULL | NULL |<br /> | Finno-Ugric | Mordvinic | Erzya | NULL | NULL |<br /> | Finno-Ugric | Mordvinic | Moksha | NULL | NULL |<br /> | Finno-Ugric | Permic | Komi | NULL | NULL |<br /> | Finno-Ugric | Permic | Komi-Permyak | NULL | NULL |<br /> | Finno-Ugric | Permic | Udmurt | NULL | NULL |<br /> | Finno-Ugric | Sami | Eastern Sami | Akkala Sami | NULL |<br /> | Finno-Ugric | Sami | Eastern Sami | Inari Sami | NULL |<br /> | Finno-Ugric | Sami | Eastern Sami | Kemi Sami | NULL |<br /> | Finno-Ugric | Sami | Eastern Sami | Kildin Sami | NULL |<br /> | Finno-Ugric | Sami | Eastern Sami | Skolt Sami | NULL |<br /> | Finno-Ugric | Sami | Eastern Sami | Ter Sami | NULL |<br /> | Finno-Ugric | Sami | Western Sami | Lule Sami | NULL |<br /> | Finno-Ugric | Sami | Western Sami | Northern Sami | NULL |<br /> | Finno-Ugric | Sami | Western Sami | Pite Sami | NULL |<br /> | Finno-Ugric | Sami | Western Sami | Southern Sami | NULL |<br /> | Finno-Ugric | Sami | Western Sami | Umi Sami | NULL |<br /> +-------------+---------------+--------------+------------------+------+<br /> 29 rows in set (0.00 sec)<br /></code></pre><br /><br />So here's the modified preorder traversal tree table schema:<br /><br /> <code><pre><br /> CREATE TABLE language_family_mptt (<br /> language VARCHAR(30) NOT NULL,<br /> lft INT NOT NULL,<br /> rgt INT NOT NULL<br /> ) COLLATE utf8;<br /></code></pre><br /><br />And then here's the recursive stored procedure to migrate the data:<br /><br /> <code><pre><br /> TRUNCATE TABLE language_family_mptt;<br /> SET max_sp_recursion_depth = 255;<br /> DROP PROCEDURE IF EXISTS insert_branches;<br /> DROP PROCEDURE IF EXISTS start_tree;<br /> DELIMITER ~~<br /><br /> CREATE PROCEDURE start_tree()<br /> BEGIN<br /> DECLARE language_field VARCHAR(100);<br /> DECLARE done INT DEFAULT 0;<br /> DECLARE insert_id INT;<br /> DECLARE source_id INT;<br /> <br /> DECLARE cursor1 CURSOR FOR SELECT language, language_id FROM language_family_adj_list WHERE parent_id IS NULL ORDER BY language;<br /> <br /> DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;<br /> <br /> OPEN cursor1;<br /> read_loop: LOOP<br /> <br /> SET @my_left = 1;<br /> <br /> FETCH cursor1 INTO language_field, source_id;<br /> INSERT INTO language_family_mptt ( language, lft ) VALUES ( language_field, 1 );<br /> <br /> CALL insert_branches( source_id );<br /> <br /> UPDATE language_family_mptt SET rgt = @my_left + 1 WHERE lft = 1 AND rgt = 0;<br /> <br /> IF done THEN<br /> LEAVE read_loop;<br /> END IF;<br /> <br /> END LOOP;<br /> CLOSE cursor1;<br /><br /> END; ~~<br /><br /> CREATE PROCEDURE insert_branches( IN source_parent_id INT )<br /> BEGIN<br /> DECLARE done INT DEFAULT 0;<br /> DECLARE next_source_parent_id INT DEFAULT NULL;<br /> DECLARE orig_left INT DEFAULT NULL;<br /> DECLARE language_field VARCHAR(100); <br /> DECLARE cursor1 CURSOR FOR SELECT language_id, language FROM language_family_adj_list WHERE parent_id = source_parent_id ORDER BY language;<br /> DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;<br /> <br /> OPEN cursor1;<br /> <br /> read_loop: LOOP<br /> <br /> FETCH cursor1 INTO next_source_parent_id, language_field;<br /> <br /> IF done THEN<br /> LEAVE read_loop;<br /> END IF;<br /> <br /> SET @my_left = @my_left + 1;<br /> <br /> INSERT INTO language_family_mptt ( language, lft ) VALUES ( language_field, @my_left );<br /> <br /> SET orig_left = @my_left;<br /> <br /> CALL insert_branches( next_source_parent_id );<br /> <br /> UPDATE language_family_mptt SET rgt = @my_left + 1 WHERE lft = orig_left AND rgt = 0 ;<br /> <br /> SET @my_left = @my_left + 1;<br /> <br /> END LOOP;<br /> CLOSE cursor1;<br /> END; ~~<br /><br /> DELIMITER ;<br /></code></pre><br /><br />And here's the results:<br /><br /> <code><pre><br /> mysql> SELECT CONCAT( REPEAT( ' ', (COUNT(parent.language) - 1) ), node.language) AS name FROM language_family_mptt AS node, language_family_mptt AS parent WHERE node.lft BETWEEN parent.lft AND parent.rgt GROUP BY node.language ORDER BY node.lft;<br /> +---------------------------------+<br /> | name |<br /> +---------------------------------+<br /> | Finno-Ugric |<br /> | Baltic-Finnic |<br /> | Estonian |<br /> | South Estonian |<br /> | Voro |<br /> | Finnish |<br /> | Ingrian |<br /> | Karelian |<br /> | Karelian Proper |<br /> | Lude |<br /> | Olonets Karelian |<br /> | Livonian |<br /> | Veps |<br /> | Votic |<br /> | Hungarian |<br /> | Khanty |<br /> | Mansi |<br /> | Mari |<br /> | Mordvinic |<br /> | Erzya |<br /> | Moksha |<br /> | Permic |<br /> | Komi |<br /> | Komi-Permyak |<br /> | Udmurt |<br /> | Sami |<br /> | Eastern Sami |<br /> | Akkala Sami |<br /> | Inari Sami |<br /> | Kemi Sami |<br /> | Kildin Sami |<br /> | Skolt Sami |<br /> | Ter Sami |<br /> | Western Sami |<br /> | Lule Sami |<br /> | Northern Sami |<br /> | Pite Sami |<br /> | Southern Sami |<br /> | Umi Sami |<br /> +---------------------------------+<br /> 39 rows in set (0.00 sec)<br /></code></pre><br /><br />:Doperatorhttp://www.blogger.com/profile/13927814241268465006noreply@blogger.com0tag:blogger.com,1999:blog-332708966850096998.post-56464183406750481472010-10-02T10:33:00.003-07:002010-10-02T10:57:01.322-07:00Jpeg and WebP -- what's the diff?I used Paint.NET's XOR blending to create a diff between the sample Jpeg and WebP images that google posted on their blog. Here's the result:<br /><br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_s_5GFl7OT-Y/TKdvp9o-BnI/AAAAAAAAAHk/ovB4e5dLY4M/s1600/webp_diff.png"><img style="display: block; margin: 0px auto 10px; text-align: center; cursor: pointer; width: 400px; height: 200px;" src="http://3.bp.blogspot.com/_s_5GFl7OT-Y/TKdvp9o-BnI/AAAAAAAAAHk/ovB4e5dLY4M/s400/webp_diff.png" alt="" id="BLOGGER_PHOTO_ID_5523506234704922226" border="0" /></a>Click to see the full size image.<br /><br />The colors you see on the diff image is where the two images had disagreements about what color the pixel was. Where it's white, the two images had the same color for that part.operatorhttp://www.blogger.com/profile/13927814241268465006noreply@blogger.com0tag:blogger.com,1999:blog-332708966850096998.post-7444112275777914132009-08-27T18:09:00.000-07:002009-08-27T19:04:03.261-07:00TranslationMy translation of Johann Gottfried Herder' classic poem:<span style="text-decoration: underline;"><span style="font-weight: bold;"><br /><br /></span></span>Our life is but a dream, a dream<br />Here in this Earthly sphere<br />Like shadows on the wavecrest<br />we rise and disappear.<br /><br />We mark our plodding journey<br />in Time and Space,<br />unknowingly<br />in the midst of eternity.operatorhttp://www.blogger.com/profile/13927814241268465006noreply@blogger.com0tag:blogger.com,1999:blog-332708966850096998.post-49423269895988102862009-02-09T17:58:00.000-08:002009-02-09T18:00:54.822-08:00Outer joins have a (+)?Oracle Joins have a weird syntax for outer joins. The table that shows null values has a (+) appended to it, IIRC. I don't know if this is ANSI syntax or Oracle's own. I suspect it's Oracles; whatever the case, I don't like it. It breaks the standard of SQL syntax being English words ( FROM, WHERE, LIKE ), instead of weird operator symbols.operatorhttp://www.blogger.com/profile/13927814241268465006noreply@blogger.com1tag:blogger.com,1999:blog-332708966850096998.post-60981189034743448322008-09-24T19:51:00.000-07:002008-09-24T19:52:07.895-07:00What is dark matter?Dark Matter is not an aether. It's a very simple proposition: <br /><br />All dark matter does is weigh. It doesn't reflect light; it doesn't interact with any other particles. All it does is exert gravitational attraction. Now, think about that for a moment. Suppose such a stuff as dark matter, which only exerts gravitational attraction, but has no other observable phenomena, actually existed. How would we come to know about it? What phenomena would we be observing that would indicate the existance of such a stuff? Only one: mass -- and nothing else.<br /><br />All that we know about the universe is that there is missing mass. We've measured all the mass from everything that can be observed and we've come up short. We've accounted for every 'thing' we can observe and it doesn't weigh enough. So therefore, there is something out there that weighs, but doesn't generate any other observable phenomena -- it doesn't emit light nor does it crash into anything. It just weighs. That's all it is -- weight alone.operatorhttp://www.blogger.com/profile/13927814241268465006noreply@blogger.com1tag:blogger.com,1999:blog-332708966850096998.post-31732694088477268862008-06-13T19:51:00.000-07:002008-07-02T18:35:02.775-07:00Are we alone in the Universe? Yes, we areImagine a UFO lands in a football stadium. A humanoid creature walks out onto the field. A television reported breathlessly states, "Finally, proof that we are not alone in the universe!"<br /><br />Microphones are raised to the creature. He begins, "Sadly, we are alone in the universe. Only humans and aliens exist -- just us two, and nobody else. We are alone."<br /><br />What does it mean to be alone? Are 4 billion humans not enough for you? How many conscious beings does there need to be for us not to be alone? Enough so that the annihilation of a couple of suns or planets does not wipe out conscious life everywhere?operatorhttp://www.blogger.com/profile/13927814241268465006noreply@blogger.com1tag:blogger.com,1999:blog-332708966850096998.post-84864747552346373632008-06-13T19:45:00.001-07:002008-06-22T11:06:00.818-07:00Religion is not inherently violent.It's probably true that most people have been killed in the name of religion, but that's not the real reason they were killed. Religion was just the cover story to motivate people to go out and kill. The really underlying cause of all of these deaths is the political power of the state.<br /><br />It's true that there are violent and deadly religions ( such as Kali's thugee devotees, or child sacrifice to the gods ), but religion isn't <span style="font-style: italic;">necessarily</span> the cause of war and violence. Power and politics, almost by definition, are. You find plenty of examples of totally peaceful, pacifist religions ( just as a few examples, the Mennonite and Amish churches ). The early Christians were pacifist to the point of being regularly martyred. Then, when Roman Christians started gaining positions of power in the Roman government, and Christianity eventually became the official state religion, <span style="font-style: italic;">that's</span> when you see Christians persecuting others. It wasn't religion or Christianity that caused the violence -- the violence only came about after power entered the picture. On the other hand, you never find a peaceful government or country. It's a contradiction in terms. The state ultimately enforces its rule through violent coercion. So, religions may or may not be violent; but governments, by definition, are. In the case of Islam, Mohammed was also a political leader ( he was basically a tribal chief, responsible for the defense of his tribe ) as well as a prophet.<br /><br />So yes, people have been killed in the name of religion, a lot. But I argue that more people have actually been killed by governments.<br /><br />But, some might ask, if religion is a cover story that governments use to kill people, why not go ahead and suppress religion, to prevent governments from using this powerful cover story? It's because you're not really addressing the real cause of the violence. If you take away one cover story -- a religion -- government will simply come up with another cover story -- another religion, or a mystical super-state, like Hitler's 3rd Reich, or Stalin's communist revolution.<br /><br />If you're serious about addressing war and violence, religion is not the culprit. If it's just another reason to bash religion, well go ahead; religious people certainly deserve the criticism of going along with violent, evil state powers.operatorhttp://www.blogger.com/profile/13927814241268465006noreply@blogger.com2tag:blogger.com,1999:blog-332708966850096998.post-69197752880483711622008-06-13T16:19:00.000-07:002008-06-13T19:47:37.869-07:00A penny costs more than a penny to makeA penny costs one-and-a-half-cents to make. What a perfect example of government waste in spending. Why, we'll surely go broke just minting money, before we even have a chance to use it!<br /><br />Well, it really doesn't matter if a penny costs 5 cents, 10 cents, or a dollar. A penny's job is to transmit value, and after repeated uses, you can see that it easily pays for itself.<br /><br />Say you buy a car for $10,000. You drive it to work the next day. Congratulations! You just spent $10,000 to drive to work. What a waste of money.<br /><br />But hold on. Tomorrow, you drive to work again in that same car. Now, you've spent $10,000 to drive to work twice, or $5,000 per trip. In fact, if you drive to work and back every day for a year in your new car, it will have cost you $20 per trip* for the car. Now that's starting to sound more reasonable, eh?<br /><br />So imagine how many times a penny changes hands in its lifetime. If you have a two pennies in your pocket, chances are one of them is from the 80s or the 90s. You, like most people, do not hoard coins. That penny has probably changed hands thousands of times. If a penny has changed hands 28,743 times, it's done $287.43 worth of money transfers. If this little money transfer device cost even a dollar to make, I'd say it was a pretty good deal, wouldn't you?<br /><br />* $10,000 / ( 2 trips per day * 50 work weeks * 5 work days in a week )operatorhttp://www.blogger.com/profile/13927814241268465006noreply@blogger.com1