Thursday, April 5, 2012

Just wrote an auto-memoizer for single-valued functions as an answer to a StackOverflow question:
function memoize(func, max) {
    max = max || 5000;
    return (function() {
        var cache = {};
        var remaining = max;
        function fn(n) {
            return (cache[n] || (remaining-- >0 ? (cache[n]=func(n)) : func(n)));
        }
        return fn;
    }());
}

function fact(n) {
    return n<2 ? 1: n*fact(n-1);
}

var memfact = memoize(fact,170);
Based on an implementation of factorial function by user xPheRe:
var factorial = (function() {
    var cache = {},
        fn = function(n) {
            if (n === 0) {
                return 1;
            } else if (cache[n]) {
                return cache[n];
            }
            return cache[n] = n * fn(n -1);
        };
    return fn;
}());
Things I like:
  • It memoizes anything
  • The cache limit prevents stupid things like memoizing where there are floats
  • It's faster than the if/else original
I could add in a trigger which replaced the function with a straight call after too many misses, so that even wrongly-memoized functions weren't slowed down after a number of calls.

Monday, March 12, 2012

Rituals, expectations, culture

On reading Ken's post on rituals:

I've been thinking about this recently. I think a culture defines a set of public and private expectations of its members. People that do not conform to these expectations are cold-shouldered. We usually call it being rude, or antisocial. In one society, one finishes everything on the plate to show it was good. In another, we leave a little to show we the host was generous. There is probably a historic reason for each, and doing the wrong one will make us seem rude.

The key is that a culture needs to be largely predictable for it to function. I need to be able to expect to go to dinner without taking a plate, or to know to take my plate. The more uncertain the behaviour of everyone else, the more precautions and preparations I have to make.

In a village, social cohesion is important, and there are not that many people, so I can and should greet everyone I pass. But in a city, I cannot afford to greet everyone I pass, and the culture must permit this. So we have cities where no-one makes eye contact, despite inhabiting each other's personal space.

A different slant, however, is provided by the religious or moral code. Often, particularly for the believers, these are not merely rituals but ways to honour their diety; the ritual represents their own hierarchy. She chose to observe her ritual instead of socialising, which some will respect and others will see as an affront.

Thursday, December 8, 2011

The future: megacity margin-dwelling artistry

Malthus saw two equations: the exponential increase in population resulting from the individual action to have more than 2 children per couple, and the sub-exponential rise in resources with which to house, clothe and feed these people. He could see that the first will continue until something limits it, either continuously (for much of human history you needed n children to have 2 survive) or in mass events (war, disease, famine).

Over the last 60 years, the West has suffered no war on its soil, no famine it couldn't buy its way out of, no disease it couldn't contain. It has assisted similar evasion of Malthus in the rest of the world, and the world population has responded by exploding.

We are faced, today, by a series of Malthusian equations, regarding the future of civilisation:

  1. Outside a few countries in the West (notably Germany, France) the population is continuing to rise. In developing countries it is continuing to rise exponentially as the traditional expectation of large families is not met by the same threat of disease and famine it had been for so long. Until people get used to choosing to have smaller families and a better quality of life, this will continue at a breathtaking pace.
  2. There has been a migration from the country to the city for a century or more. As the city offers more modern lifestyles and has lower incidence of famine and disease, the population growth of existing city dwellers is also increasing. This means that the future will be increasingly lived in cities, to the exclusion of all else. Over time, life has been less about the land and more about people; technology, education, consumption.
  3. People are living longer. Not only does this contribute to the rising population, but it also makes more demands on their ability to save for their old age. 
  4. Technology is systematically removing the human component of necessary production. Food, clothes, transport are all global goods, produced wherever is cheapest. But the competitor with no bottom line, no minimum wage, is the machine. Even Chinese and Indian labour will be more expensive than the machine eventually. This means that increasingly people's jobs depend on the unnecessary; on books and holidays and music. We have a fear of recession precisely because in a recession people stop spending and the economy depends on their spending, their consumption.
As population increases beyond our means to produce more, there will inevitably be a food crisis. It may be slow in coming, with people learning to live on less as they see the prices rise, or it may be fast, initiated by a widespread bad harvest in a particular year. If it is the former, and we are ready, we may survive the beginning of it. If it is the latter, there will be famine. There are many ways we can ease the crisis in the near-term; vegetarianism, GM crops, desert irrigation, food supplements. If the crisis is slow, we will use all of these and others, albeit only when finances dictate we must.

With technology reducing the dependence of farming and production on people, and the population rising, an ever-diminishing number of people are needed for production. Since the capacity to farm is limited by the amount of available land, rather than our ability to farm it, the exponentially-rising population cannot result in an exponentially-rising number of farmers.

This means that with a diminishing amount of food available, and an ever-decreasing proportion of the population in necessary work, the rest of us will need something to do. A population needs entertainment, society, events, and so on. This is the expanding industry of the future; art. The creation and sharing of meaningful artefacts of culture, things that bind a group together - film, music, news. All the material things that have possessed the world of the present and the past; farming, manufacture, the management of scarce resources, will be history. The scarcity may be so severe that we cease trying to manage it.

If people are doing unnecessary work which largely benefits only themselves and their group, where will they get the money to buy food? I think the only answer is that the government or some other overarching corporation will give it out. State benefits will be the norm, not the exception, and they will be meagre by today's standards. Imagine that instead of handing out money for food, the city government provided it free. It will have already become the government's job to find enough food for everyone; we already see countries like China buying up land in Africa for food - eventually this is their primary remit. Money and capitalism will still exist, but they will be largely irrelevant to the mass; after all, you don't need money for food.

So we see enormous populations in cities living on low margins; minimising their consumption, minimising their need for scarce resources. Over a few generations of any lifestyle, a group absorbs the techniques and goals of living it into their very psyche; we see it in the philosophy of martial arts, in the way people feel that there is a natural order, their order. We see free speech and democracy as right, as good. But they are constructs we created to handle threats to our existence in the past - tyrants, dictators, corruption. I think we will see a commitment to living on narrow margins enter the collective psyche. People will not be fat even though food is free in this future, because of the social stigma associated with such excessive consumption. You may think that the rational actor will win, that people will overeat anyway. But the rational actor would steal, corrupt and connive in today's world with its low policing and yet people don't because we consider it beneath ourselves to do so. Plus, the food won't be so fattening.

The final piece in the jigsaw is perhaps the most unbelievable, if you have managed to stifle your disbelief so far. In a world where art is the way of life, and money merely a choice between living in one area with its balance of comfort and food and another area, people will not pay each other for things, they will give. Once the meaningless necessities are dealt with - purchasing food, clothing and so on - what is left is the meaningful. Gifts are the ultimate currency of meaning, and one people are only too ready to employ (why else do gift vouchers exist?). In a world of scarcity, to save up your resource, your time, and produce an artefact which you give to another is a supreme sacrifice, and thus a meaningful gift.

Your great-grandchild won't buy a ring for his spouse, he will craft one, and after a special dinner at the local eatery where he called in his favours with the owner to get the tastiest morsels, they will go home to their 2 room apartment and think about when in their life they will have their one child, proud to be living the right way. What will they think of you?


Sunday, November 27, 2011

Scrobble my mood

I'm back on Last.fm after a long stint on Spotify. I have paid for the premium services of both in the past, but both are disappointing in one area: knowing what to play next. Spotify makes you choose your own tracks (the Radio functionality is useless), which is simple but very limited. I barely discover new music on Spotify, it's usually just from searching for things I've heard elsewhere. Occasionally I will discover something new by listening to compilation albums which contain tracks I've searched for, but that's about the limit of it.

Last.fm, by contrast, makes it almost impossible not to discover new stuff unless you only listen to your Library or Loved Tracks all the time. But the piece of functionality that should do exactly what I want (click-free music that I want to listen to for hours) is the Recommended Radio, and it's not great. Which is surprising, because it knows everything I have listened to for the last few years, as I've scrobbled even my Spotify listening.

So what's the problem? Inappropriate tracks. I don't mean offensive, I mean not matching my mood. Some artist radio stations are good matches for my mood and don't end up playing me rubbish; Rob Dougan is one, but mostly because it plays me lots of Rob Dougan, who is just excellent, particularly at work.

My mood is very variable, and the music has to fit it or I'll just switch it off. At the same time, I usually want the music to just play without me needing to babysit it. On Spotify, I tend to play specific playlists built up over weeks or months of selections and deletions. On Last, playlists are nearly pointless (you can't just play a playlist until it has a very large number of tracks in it, so you never actually build them up), so I end up playing tag or artist radio.

I've never worked out, however, whether playing the Uptempo tag radio will be the same for me as for someone else - does it take my tastes into account?

Idea: Do mood-matching to link my scrobbles
If I listen to a load of tracks in one go, and I skip some of them before they scrobble, then I'm probably in a particular mood, especially if some of those I skip are loved tracks; it's not that I don't like the track, I just don't want to listen to it now. Thus, we have more meta about this collection of tracks; they are probably mood-related. So instead of making me work out playlists for myself, or cast myself to the winds of tag or artist radio, Last could do mood-specific recommendation.

The power of the Web 2+ world is to do algorithmic cleverness in order to deliver some very human requirement automatically. Google goes beyond mere links now, and personalises searches, fixing spelling mistakes and suggesting maps. Social networks have to handle my social segregation between close friends, acquaintances, family and work, without telling work I'm pregnant, or the matriarchy about my relationship status before I'm ready. Thus Last has done well to facilitate self-measurement (scrobbling), but they have some way to go to give me a set-and-forget radio station that works for me.

So for now, I stick to pop artist radio... the mainstream is at least inoffensive to the ear for the most part, and fairly peppy.

Tuesday, November 8, 2011

Re: The Great Apple Apartheid

My response to Alan, who wrote about Apple ignoring the needs of the poor or ill-connected when needing a fast internet connection for a software update to get iTunes functioning on his new machine.

---

The demographic that can afford to buy Apple products is the demographic that has a fast internet connection. Apple does not target the poor, at all. Have you ever seen an Apple product priced to compete with cheap products? The only one I can think of is the shuffle, which is a lead-in for getting people to spend money on iTunes.

Alan, you speak of poverty, but people in actual poverty do not buy Apple products. They do not fit; you need to buy the product, and then pay for all the expensive stuff that goes with it – the fast internet connection, the expensive upgrades, the pay-for iTunes downloads. If you live on $100 a month, when are you going to buy into that kind of expenditure? Even the western poor on state benefits are likely to struggle. No, you buy a cheap PC, second or third hand, ideally with some linux flavour on it which will cost nothing to update bar the net connection, and which permits you to share music for free. Illegally if necessary. If I have a choice between a 5-year old run-of-the-mill car or a 12 year old Merc, I would be foolish to buy the Merc because it will cost far more to fix, far more to fuel etc. Particularly if no-one else around me owns a Merc.

Apple do not target the poor, because they are poor. They do not have the money that Apple wants, nor do they have the cachet that Apple makes use of. Its products are aspirational, status items. I suspect that any community whose internet connection is slow and/or expensive will share music via mp3s on usb sticks, not by paying a track at a time on iTunes. Just like we used to, when we had slow and expensive connections.

Apple is an immediate choice in your world, so your problem is the fragility of it. In the world of the poor, Apple is Prada, Gucci, Alienware, Steinway. Hey, Prada have a fashion event on, and the dress code is strings of pearls.

Monday, November 7, 2011

Flow

I had flow again, just for a few seconds, improvising on the violin.

Felt like a kind of bliss/fire up my spine.

Wednesday, October 26, 2011

Re: A tale told by an idiot

In response to A tale told by an idiot on Ken's blog, in which he bemoans the reality-denying conspiracy theory of the film Anonymous about Shakespeare's works being written by someone else.

---

To reuse the bard’s form: “A tale told by an idiot is still a tale”

Films are about meaning and narrative, and are in fact usually a way for people to ascribe meaning and overarching narrative to a world full of random chance and no objective meaning. So film is actually about the meta, the meaning, not reality. Just like sci-fi is about humanity, not about science.

So, just as scientists have to do when watching most disaster movies, you must realise that we must leave disbelief at the doors of the theatre. Leave reality behind, and enter fairyland, enter the magical world where everything has a reason.

It probably will do well at the box office, because it has a strong enough premise for you to rail against. Most people won’t really care whether that premise is objectively true or not, because they already have too much objective reality to deal with. All they want is a story. That’s why films do well about dinosaurs roaming the earth, or aliens, or supernatural things.

There is usually a loose connection to reality in order to set the scene, to begin from a common narrative understanding; that there is a bard, that he is famous for writing some of the greatest works in literature. From there, the tale can construct its own world. Ignore the assertions of the nutters, and immerse yourself in the story.