strictures and structures

if only we stopped trying to be happy, we could have a pretty good time

a terribly hacktastic implementation of Conway’s Game of Life

I had to take a Ruby training course as part of my job recently. The instructor asked us to write Conway’s Game of Life without any conditional statements–so no “if”, no “unless”, no ternary statements, etc.

Once upon a time I would have thought she was on crack and a mean vicious lady and refused to play along, but I’ve grown up a little bit, and to my surprise, I was the only one in the class to come up with a solution.

This isn’t the full implementation, but the core of the game is calculating the state a cell will be in on the next tick. Here it is:

=begin
  The general gist is that you iterate through all the neighbors, gather the liveness states
  of each of them, and convert the array of liveness states to a string. E.g. the array of 
  states [true, false, true] would end up becoming "true_true_false" (since we sort the array).

  Ruby has the ability to pass the name of a function to a method called "send." This is useful
  if your method calls are so dynamic you have no idea what you're going to be calling, as 
  we are in this case. So we write one method for each condition: one live neighbor, two live 
  neighbors, three, etc. Using the set of states, we generate the correct method to invoke without
  ever having to resort to the use of conditional statements.

  The cases, taken from http://coderetreat.org/gol:
    Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
    Any live cell with more than three live neighbours dies, as if by overcrowding.
    Any live cell with two or three live neighbours lives on to the next generation.
    Any dead cell with exactly three live neighbours becomes a live cell.

  The one case I haven't handled is the case where two dead cells is not brought back to life, 
  but you could handle it using the strategy I've outlined.
=end

def true_true_false_false_false_false_false_false(board, cell)
  board[cell] = true
end

def true_false_false_false_false_false_false_false(board, cell)
  board[cell] = false
end

# The overcrowding case.
def true_true_true_true_false_false_false_false(board, cell)
  board[cell] = false
end

# I won't write all of them because that's boring.

def check_neighbor_status(board, cell)
  neighbors = []
  neighbors += [-1, 0, 1].map { |x| [cell[0] + x, cell[1] + 1] }
  neighbors += [-1, 0, 1].map { |x| [cell[0] + x, cell[1] - 1] }
  neighbors << [cell[0] - 1, cell[1]]
  neighbors << [cell[0] + 1, cell[1]]
  neighbors.uniq!
  # produces something like: 
  #   ['alive', 'alive', 'dead'...]
  neighbors.map { |n| board[n].to_s }.sort.reverse.join("_")
end

board = {}

board[[1,1]] = false
board[[0,1]] = false
board[[0,0]] = false
board[[2,0]] = false
board[[0,2]] = true
board[[1,0]] = false
board[[1,2]] = false
board[[2,2]] = false
board[[2,1]] = true


puts "Starting value of cell: #{board[[1,1]].inspect}"
# Generating the method name by checking the state of the neighbors.
method = check_neighbor_status(board, [1, 1])
# Using Ruby's metaprogramming abilities to invoke this method.
send(method.to_sym, board, [1, 1])

# Et voila, the state changed!
puts board[[1,1]].inspect

I was talking to a fellow dancer at an alternative blues event, of all places, and he pointed out that using a hash where the strings were the keys and the methods were values would have worked. Excellent point. The key insight is that you can use hashing to approximate conditional behavior.

Mainly, I’m posting this because did I mention I was the only person to get the solution, out of a room of about twenty professional computery types, and one of those people was my superhumanly competent coworker Lisa and I even surprised the instructor, and I *cannot* stop gloating?

I only look like a bad person, because I speak the truth. If you were me, you wouldn’t be able to stop gloating either.

on biking

It’s lonely on the 1. I wish I could say it’s beautiful, and I was counting on its beauty to cheer me up, but the clouds hanging over the sea are like so much blue dryer lint, and the sea itself has the dull gleam of crumpled foil in a trash can. The wind is blowing me back, a strong wind the likes of which I have not seen for five years. I remember Mike Munk telling me five years ago that he’d rather ride up a hill than fight the wind any day, because you can at least see what you’re riding against. I have to agree.

I’m tired, and I have no one to talk to but myself, so this is how I pass the time. I invent as many ways as I can to describe the pain of pedaling seventy miles.

  • The air feels like broken glass in my lungs.
  • The air feels like dry hay in my lungs.
  • My bones are turning into milk.
  • My bones are turning into chalk.
  • My lower back hurts so much that it feels like a crab is trying to pinch it free from my spine.
  • I am so hungry that it feels like I’ve swallowed a bobcat.
  • I am so hungry it feels like two stones scraping against each other.
  • I am so hungry I can feel my stomach digesting itself.
  • My eyes are drying out like peeled grapes.
  • I feel like tissue paper disintegrating in water.

I’ve done this to myself before. Not as often as the people I regard as being truly hardcore cyclists, but often enough. This is absurd because I don’t like cycling. My two favorite hobbies in all the world are reading really stupid books, and social dance. If a demon showed up and forced me to give up a hobby or he’d kill a litter of kittens, I would give up cycling first, no question. I’d even keep perfume collecting over cycling, and I haven’t bought a new scent in years.

So why am I here?

For the aftermath. For the victory. For the knowledge that when put to the test, I do not fail.

This is a fundamentally childish impulse, but childish in the best sense of the word–in the sense that the young have a need to learn, and explore, and grow, to discover themselves and the world, that someone more settled might not have. [1] The older and more experienced you are, the weaker your drive is to discover things–because you know them already.

So this is why I am here.

Because, ultimately, I want to find my limits. And I haven’t yet.

[1] http://www.theatlantic.com/features/archive/2014/03/hey-parents-leave-those-kids-alone/358631/

the virtues of overachieving

I don’t like people. I never have, and I never will, but even so, I’m forced to admit that I’ve met a remarkable number who are interesting, even amazing. It’s almost disgusting how you can take a bag of awards, accomplishments, skills, and hobbies, pick out a few at random, and bam! You’ve just made another Stanford student. Blue-haired product designer who welds things and does ballet? Bam, she exists, I have met her. A Googler who writes trophy-winning AIs in his spare time? Bam, he exists too. I know a lot of partnered dancers who excel at dance and some other esoteric field, mostly because that’s my hobby, dance, but I’ve met a harpist who does wushu. There are plenty of other combos out there.

I was hanging out with two of those annoyingly accomplished Stanford students a few years ago, and since they were old enough to be thinking about leaving the warm cocoon of Stanford’s campus, talk turned to the job market. They were mechanical engineers, not programmers, so their job security was somewhat uncertain. I said, tentatively, “Well, what about not doing engineering?” Andy they replied in nigh unison, “I couldn’t bear the thought of wasting my education.”

This story, mundane as it is, haunts me. Why? I’ll be the first to admit that Stanford is too expensive for the kind of useless, traditional liberal arts education that I value so highly. If you buy it, you have to use it.

What bothers me, I think, and I don’t believe my friends meant to imply this at all, of course, but what bothers me is that it seemed like another outbreak of a strong sentiment that it’s not enough to just be a good person. To live well, to do no harm, to help others–lauded, but hardly prioritized, and never given the same status as “achieving one’s potential,” whatever that means.

Though I must admit, I am lazy. I am a Type B, all the way. Meeting ambitious, accomplished people tires me out. The pressure to achieve, to be interesting, to be unique, suffocates me sometimes. It all seems so draining, and it is so, so very unnecessary. We don’t live in a kind or just world, and living up to one’s potential as a decent citizen of the world will do a lot more good than being a champion fencer who built a bipedal robot for his PhD thesis.

William Carlos Williams update #3

This is just to say

I have eaten
the pluots
that were

screw it

pluots
are my
favorite

I will
not
apologize

Dad pronounces judgment

Dad: You don’t have a truly scientific personality, Jane. You’re my daughter, and I’ve seen how you behave. You really like petting cats and dogs. If you were a real scientist, you would want to dissect them instead.

random thoughts on pigeons at lunch

1. There are fat pigeons and skinny pigeons.

2. There is a distinct correlation between being a fat pigeon, and a greasy pigeon.

3. Does being fat make it harder to fly?

4. If so, why haven’t birds of prey eaten all the fat pigeons? I know they’re out there. I saw one lunching on a sparrow in Yerba Buena.

5. What makes some pigeons fat, and others not? I still see svelte, well-groomed pigeons. There is no such thing as an educated, upper-middle class health conscious pigeon, though, so what could possibly be keeping them skinny? Genetics? 

6. Do some pigeons just not like pizza and Doritos?

7. Maybe the fat pigeons push the skinny ones out of the way when there’s food around? I’ve been observing this flock for ten minutes though, and the fat pigeons appear to be as peace-loving as any other pigeon.

8. Are birds of prey health conscious? Do they eat fat pigeons and then think, no, gross, why did I do this to myself? This would be pretty easy to do an experiment on. Possibly unethical, but pretty easy as far as these things go.

9. Are greasy feathers the avian equivalent of acne?

10. Would I like the taste of the svelte pigeon, or the fat one with a jiggly neck more? I suspect the one with the jiggly neck.

11. If a pigeon ate a pizza, would it taste like a pizza?

12. A peregrine falcon needs to eat approximately 2.5 ounces of food per day. A single pigeon weighs about 10 ounces. If a peregrine falcon eats a generous serving of one pigeon per day, then that means it needs to eat 365 pigeons a year. Pigeons lay one to six broods per year, at a rate of about two eggs per brood. Let’s assume a mated pair of pigeons produces just three pigeons that survive to adulthood every year. So, to support a single peregrine falcon, we need 365/3, or approximately 122 breeding pairs per year. That is, for each bird of prey in San Francisco, we need a supporting cast of 240 pigeons. (Assuming no pigeons die of old age, illness, housecats, etc.) I guess this explains why I still see so many of those flying rat bastards around.

dad’s perception of himself

Jane: Dad, what is your blood type?

Dad: I am O negative. I can donate to anyone. That means I am a hero.

Mom: When I went to the ER and I needed a blood transfusion, you didn’t give me anything!

Dad: Ahem, I am a reluctant hero.

—–

He actually said that last part in English. He is much more familiar with literary tropes than I had thought.

‘Twas the night before Demo Day

‘Twas the night before Demo Day, and all through the house,
Came a furious clicking, of keyboard, and mouse.
A worried non-technical founder did pace,
His PowerPoint done, he felt out of place.
The hacker was hacking, but to no avail,
He made sounds that were half growl, half wail.
The night felt so bleak, but there soon came a crash,
Down through the chimney fell a fat stack of cash.
It was followed thereafter by two shiny black shoes,
Clearly those of a VC, but which one–whose?
The figure emerged, the founders sucked a breath in–
For who should it be, but Marc Andreessen?
How commanding he looked! Yet how genial and merry!
He strode to the demo, and typed in a query.
“A new dating app?! Sweet! Let’s chat!
“Let’s sign you on now! Immediately! Stat!”
He signed all the papers, and spun on his heel,
And went back up the chimney as if drawn by a reel.
The founders logged on to tweet with delight,
“We crushed it, you suckers. Have a good night.”

rites of passage in a typical Chinese family

Jane: Dad, why is your nickname for me “puppy”? Why don’t you ever use my actual name?

Dad: Traditionally, in China, you start out by calling a child an animal. It is only when they prove that they can survive that you give them a real name.

Twenty-six years, and he’s still calling me “puppy.” No wonder he made his plans for retirement without counting on me for support.

what my parents think of amy chua

Jane: Mom, would you take your stories about raising me and Zhao, publish them in a book, and then sell them to all the white people?

Mom: No.

——

Jane: Hey Dad, what do you think of that tiger mom?

Dad: If she thinks Chinese culture is so superior, why is she in America?

Follow

Get every new post delivered to your Inbox.

Join 4,895 other followers