The return of DDR Wednesday

Haven't done one of these in a while, but I'm planning to hit up SVGL this Wednesday evening starting around 7-7:30pm. Any of the old crew want to make a party of it? xtalcy/i-gene, I'm talking to you!

Might even play a round of minigolf!

WTF spam

What the heck is up with all the spam on LJ recently? If they don't solve this problem soon I think it's time to finally delete my account.

Am I the only person getting all this garbage?


I'm going to be in Seattle for PAX, September 3rd to 5th. If you will be around, and want to hang out, leave a comment. I'd love to meet up with people.
  • Current Mood
    excited excited

Framing 3d objects

Today I ran into some code in Spore that has to do with framing pictures of creations. The problem is this: given some object created by the user, come up with a camera which takes a picture of that object.

The code worked by placing the object in front of a camera, then incrementally moving the camera around until the object fits. This is obviously inefficient. So today I wanted to figure out how to frame objects efficientlyCollapse )

Applicative Functors

This is a long response to a post about encoding musical patterns in Haskell.

data Pattern a = Pattern {at :: Integer -> [a], period :: Integer}

The problem was in how to make this object into a Monad; I made an argument that it was just an applicative functor instead.

The "essence" of an applicative functor is that it is just like a monad, except that the result of a computation can't be used to change the "shape" of the entire computation. For example, using [] (which is both a Monad and an Applicative instance):

(+) <$> [1,2] <*> [3,4] = concat [[1+3, 1+4], [2+3,2+4]]
[1,2] >>= \x -> if even x then [] else [x+3,x+4] = concat [[1+3,1+4], []]

Notice in the second example that the inner lists have a different shape; whether or not the result of the first 'computation' [1,2] is even changes the shape of the result for that computation. While it's easy to make this work for patterns, building a (Pattern (Pattern a)), you can't write the equivalent of "concat" unless you know the period of the resulting pattern. Without that complication, you could say type Pattern a = ReaderT Integer [] a which is just a fancy way to say type Pattern a = Integer -> [a] that automatically gives you a Monad instance.

It's easy to make Pattern into an applicative functor; it's just the composition of the applicative functors for Reader and [], with a little extra work to manage the period:

instance Functor Pattern where
  fmap f (Pattern xs p) = Pattern (fmap (fmap f) xs) p

The outer fmap is using instance Functor (a ->) where fmap = (.), the inner is using instance Functor [] where fmap = map

instance Applicative Pattern where
  pure x = Pattern (pure (pure x)) 1
  Pattern fs pf <*> Pattern xs px = Pattern (liftA2 (<*>) fs xs) (lcm pf px)

However, Pattern *is* a monad, it's just that "concat" is ugly; we have to figure out the period of the resulting pattern, which, surprisingly, is possible: you just take the lcm of all the patterns we could return!

joinPattern :: Pattern (Pattern a) -> Pattern a
joinPattern (Pattern ps p) = Pattern f newPeriod where
  newPeriod = foldl' findPeriod p $ map ps [1..p]
  findPeriod p = foldl' lcm p . map period
  f n = concatMap (\pat -> at pat n) (ps n)

instance Monad Pattern where
  return = pure
  m >>= f = joinPattern (fmap f m)

Militarized police are a bad tactic

Anyone who has known me for any period of time knows that I'm about as anti-drug-use as you can get. I don't drink or smoke, let alone make use of any other substances.

But I couldn't stop reading the Washington Post article Deadly Force. It describes a police raid on the home of the mayor of a small town. His two dogs were killed, his house trashed, and his live-in mother-in-law was held on the floor at gunpoint.

And this is not an isolated incident. The drug war has gone too far.

When someone brings weapons to bear, it raises the stakes on any encounter. Are people supposed to comply with an armed attacker just because they claim to be a police officer? That gives an obvious strategy to the bad guys: claim to be a police officer.

When our country was founded, we decided that we would rather let some bad guys go free than force innocents to submit to this trauma. These rights are enshrined in our Constitution as the Fourth Amendment: "The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no Warrants shall issue, but upon probable cause, supported by Oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized."

But these rights continue to be eroded. Just recently, the Supreme Court again narrowed the Exclusionary Rule, the main protection that the Fourth Amendment gives citizens. The idea behind the Exclusionary Rule is that the only way to compel law enforcement to follow the rules is to let bad guys go free, if law enforcement doesn't follow the rules. To do so, we make evidence obtained via illegal searches inadmissible in court. But the rule has been chipped away at so many times by people who don't understand that if we don't let some bad guys go free, then we will harm good guys in the process.

How can the 4th Amendment stop unlawful searches, if law enforcement can use evidence from those very same searches to arrest and convict people? Arrests and convictions are the measures that are used to determine how good of a job they are doing, so of course they will bend the rules to the point of breaking!

Instead of being modeled after the military, law enforcement should be about reaching out and building communities. We need to have people feel safe in their homes, and feel that the police are there for their protection. Instead, the United States has close to the highest per-capita prison population in the world. Do you really think that the citizens of the U.S. are worse people than elsewhere in the world? It's no wonder people don't trust the police.

The "us vs. them" military mentality in law enforcement damages our society. Lives are cut short or ruined. People give sound advice to never talk to the police. We have the despicable rise of the "stop snitchin'" movement. This isn't the direction we want our society to go!