PHP’s array_walk_recursive

The description says “If funcname needs to be working with the actual values of the array, specify the first parameter of funcname as a reference.” This isn’t necessarily helpful as the function you’re calling might be built in (e.g. trim or strip_tags). One option would be to create a version of these like so.


function trim_by_reference(&$string) {
    $string = trim($string);
}

The downside to this approach is that you need to create a wrapper function for each function you might want to call. Instead, we can use PHP 5.3′s inline function syntax to create a new version of array_walk_recursive.


/**
 * This function acts exactly like array_walk_recursive, except
 * that it pretends that the function its calling replaces the
 * value with its result.
 *
 * @param $array The first value of the array will be passed
 *               into $function as the primary argument
 * @param $function The function to be called on each element
 *               in the array, recursively
 * @param $parameters An optional array of the additional
 *                parameters to be appeneded to the function
 *
 * Example to alter $array to get a short slice of each value
 *    array_walk_recursive_by_reference(
 *        $array, "substr", array("1","3")
 *    );
 */
function array_walk_recursive_by_reference(
        &$array, $function, $parameters = array()) {
    $reference_function = function(&$value, $key, $data) {
        $parameters = array_merge(array($value), $data[1]);
        $value = call_user_func_array($data[0], $parameters);
    };
    array_walk_recursive(
        $array,
        $reference_function,
        array($function, $parameters)
    );
}

The advantage here is that we only explicitly define one wrapper function instead of potentially dozens.

Portable cheesecake

Following up on the pie in a jar notion, I’ve extended the same idea to cheesecakes. The following recipe is from La Dolce Vegan with minor modifications for convenience.

Filling

  • 300g silken tofu
  • 2 cups tofutti “cream cheese”
  • ¾ cup sugar
  • 1 tbsp vanilla extract (I also added one vanilla bean)
  • 1 tbsp lemon juice (I think I put in 1½)
  • 1 tsp lemon rind, finely grated
  • ½ tsp salt
  • ¼ flour

Glaze

  • 1 cup jam
  • 1 tbsp corn starch
  • ¼ cup water

Method

  1. Using a premade graham cracker crust (much faster), ground it up and split it between 8 jars.
  2. Blend the filling in a food processor (wets first, drys second) and pour into the jars on top of the crust.
  3. Cover and leave overnight in the fridge.
  4. The next day, bring the glaze to a boil and simmer for 5 minutes, stirring constantly (make sure to mix the water and cornstarch first to avoid clumps).
  5. Split the glaze between the 8 jars and refrigerate again for an hour.

Pie in a jar

Just about any pie recipe can be easily converted into jar pie. It’s really just a matter of form factor. Prepare a bunch, leave them in the freezer, thaw before use and bake at 375°F for 45 minutes. Couldn’t be simpler.

Alternate ideas: layer on multiple pie flavours, or partition them horizontally with the aid of construction paper. The photo below is cherry on the left, blueberry on the right.

2pie1jar

Finding columns in a database

If you need to determine which columns in a database contain a certain type of string, the pseudocode is fairly simple: list all tables in a database, list their columns that are text-oriented, filter those that match some given format. For large tables, this script won’t bother looking at too much.


############################################
# Imports
import MySQLdb
import sys
############################################
# Initialize the database connection
database = MySQLdb.connect(
  host = "localhost",
  user = "username",
  passwd = "password",
  db = "database")
cursor = database.cursor();
############################################
# Find all matching columns
cursor.execute("SHOW TABLES")
tables = [table[0] for table in cursor.fetchall()]
for table in tables:
  cursor.execute("DESCRIBE %s" % table)
  columns = [column for column in cursor.fetchall()]
  for column in columns:
    if column[1].count("text") or column[1].count("char"):
      cursor.execute("SELECT `%s` FROM \
        (SELECT `%s` FROM `%s` LIMIT 20000) AS subtable \
        WHERE `%s` LIKE %s LIMIT 1" \
        % (column[0], column[0], table, column[0], '"%foo%"'))
      results = cursor.fetchall()
      if results != ():
        print "%s.%s -- %s" % (table, column[0], results)
############################################
# Close the database connection
database.close()

Chocolate truffles

  • ½ “butter” (Earth Balance works better than margarine here as consistency is important)
  • 1 cup cocoa powder
  • ½ tsp vanilla extract
  • 1 tbsp maple syrup
  • 1½ cups icing sugar

Cream the “butter” and mix the rest in slowly, one at a time. When you’re done, you can coat each with melted chocolate (as pictured above), roll them in cinnamon, top them with an almond perhaps. These keep fairly well in the fridge when they’re done, although I’m no chocolatier, so venture with caution.

Increased streetview coverage

Back in 2009, Google’s coverage of Canadian streets was limited to major cities.

Seems as though there was an update in the meantime.

Macaroni casserole

The following details Greg Opperman‘s recipe for vegan macaroni and “cheese”. Likely one of the tastiest of dishes I’ve ever had. It’s a touch elaborate, but well worth the effort.

Veggie Beef

  • 1 cup TVP
  • 2 cups water
  • 2 tbsp vegetable oil
  • 2 tbsp mild chili powder
  • 1 tbsp cumin
  • 1 tsb soy sauce
  • 1 veggie broth cube
  • 1 tbsp Sriracha
  • ¼ cup chopped sage leaves
  1. Combine ingredients in a saucepan.
  2. Boil then simmer.
  3. Adjust flavours as necessary.

Main sauce

  • ½ cup flour
  • ½ cup vegetable oil
  • 4 cups unflavoured “milk” (soy, almond or rice)
  • 1½ tbsp tumeric
  • 1½ tbsp madras curry
  • 1 tbsp garam masala
  • 2 tbsp soy sauce
  • 1 veggie broth cube
  • 1 tbsp yellow mustard
  • 1 inch fresh ginger
  • 4 cloves garlic
  • 1 batch veggie beef (as per above)
  • 1 cup nutritional yeast flakes
  • 8 ounces peas
  • 8 ounces corn
  • 1 pound rotini pasta (al dente)
  1. Make a roux with the oil, flour and milk.
  2. Add the spices and bring to a bubble.
  3. Simmer with the remaining ingredients.

Topping

  • ¼ cup sage leaves
  • 1 cup panko breadcrumbs
  • 8 ounces margarine
  1. Melt the margarine.
  2. Add the sage.
  3. Add the breadcrumbs and combine.

Combine

  1. Preheat the oven to 400°F.
  2. Pour the main sauce into the casserole dish, followed by the topping.
  3. Bake for 30 minutes (you’ll want the topping to be crispy, which may require broiling).

Invasive browser sniffing

Take the a:visited pseudo-class and use it to determine if the client’s browser has visited a site. Do this en masse with a subset of Alexa’s top 1m websites and you can build up a fairly detailed profile of a visiting brower’s history.

var sites = [
  'http://google.com',
  'http://facebook.com'
];
$(function(){
  for (var i in sites) {
    $("#test").html("
      <li><a href='"+sites[i]+"'>"+sites[i]+"</a> </li>
    ");
    if ($("#test a").css('color') == "rgb(0, 204, 0)")
      $("#test li").appendTo("#visited");
    else
      $("#test li").appendTo("#not-visited");
    }
});

The ethics of this technique are questionable, especially if you retain this data and associate it with an email address or a registered user account. The only way I can see to protect against it is to disable JavaScript on all but the few sites you trust.

You could augment it to only send a sampling of 50 sites, respond with an ajax post, then determine which 50 to check next (e.g. if a user has visited a site specific to tech news, poll your next 50 on the most popular tech news sites). This concept is used at What The Internet Knows About You.

Fondue and fudge

This recipe works well both as fondue (while hot) and a soft fudge (while cold).

  • ¼ cup margarine
  • 225 grams dark chocolate
  • ½ tbsp flour
  • ½ cup corn syrup
  • ½ tsp vanilla
  • 2 tbsp soy milk
  1. Melt the chocolate and liquids in a double boiler.
  2. Sift in the flour to thicken the mixture up.

Misinterpreting problem spaces

Polls and elections aren’t the same type of thing. The former asks for descriptive data, the latter for prescriptive preferences. To illustrate the hazards of this common misunderstanding, let’s go over some other ways data can partitioned and the problems that arise from confusing them.

Ordered data vs unordered data

Ordered data has an innate linear ordering, whereas unordered data doesn’t.

Ordered
How many apples can you eat in one sitting? (0, 1, 2…)
Unordered
What was the last type of apple you ate? (Granny smith, red delicious…)

When unordered data is confused as ordered data, a relationship is erroneously implied between the items.

unordered-as-ordered

When ordered data is confused as unordered, data trends are buried.

ordered-as-unordered

Ordered discrete vs ordered continuous

With discrete data, there exist two values for which there is no valid value in between, whereas continuous data can always be broken down into more detail.

Discrete
How many thermostats do you have in your house? (0, 1, 2…)
Continuous
What is the average temperature in your house? (22.1°C, 20.3°C…)

When discrete data is confused as continuous, inappropriate conclusions can be reached (chances are you have 2.3 thermostats in your house).

discrete-as-continuous

When continuous data is confused as discrete, granularity is lost. We can’t tell from the graph below whether there’s a large portion of 0.0km entries, if only odd decimal entries exist (0.1, 0.3, 0.5…), or any other detail hidden by these brackets.

continuous-as-discrete

Simple data vs compound data

Compound data have multiple values that bear some relation to each other.

Simple
How big is your television diagonally (24″, 32″, 34″…)
Compound
What are the dimensions of your television (20″x18″, 24″x20″…)

Compound data can be misrepresented as multiple simple data axes by ignoring the coupling between the two values, whereby the conditional probabilities are lost. Worse, it can be restricted down to a single simple data axis, whereby relevant data is entirely discarded (e.g. do sampled televisions only exist at certain aspect ratios?).

compound-as-simple

Descriptive data vs prescriptive preferences

Descriptive
What did you have for dinner last night?
Prescriptive
What should we have for dinner tonight?

The difference is subtle, but it’s there. With descriptive data, respondents have no incentive to tactically misrepresent their opinions. With prescriptive preferences, respondents (aka voters) are aware that their response will influence a future that likely affects them and will vote accordingly.

When prescriptive preferences are confused as descriptive data, a few things can happen:

  • Continuous data can be filtered down into discrete data. Instead of allowing degrees of preference, ballots are restricted to binary expressions of support.
  • Compound data can be filtered down into simple data. Although multiple options exist, ballots restrict the voter to supporting a single candidate.

Both confusions are present when Plurality voting is used in lieu of a more expressive ballot, where in voters could express more than just a single binary preference for a single option. Maybe this comes from ignorance of alternative voting methods, technical restrictions from the online voting application used, or seeing everything as a nail when you’re holding a hammer; Take your pick.

When Plurality voting is used to obtain prescriptive preferences, the same type of analytical fallacy is being made as is detailed in the above examples, all of which lead to a poor understanding of the underlying reality the data represents.