2014/4/15

Laugh-Out-Loud Cats #2493

I miss Honolulu.

Laugh-Out-Loud Cats #2493

I miss Honolulu.

2014/3/23

maruhi:

the moon chase me 
どこまでもついてくるまるいつき

To photograph the moon is man’s greatest desire, but it remains perpetually unfulfilled. The moon is too beautiful to be captured in film or on screen, and no scientist can explain why.

maruhi:

the moon chase me

どこまでもついてくるまるいつき

To photograph the moon is man’s greatest desire, but it remains perpetually unfulfilled. The moon is too beautiful to be captured in film or on screen, and no scientist can explain why.

2014/3/11

Cat and Girl and the Search for the Total Abnegation of Self
Laugh-Out-Loud Cats #2479

A repeat, really, but more relevant now than ever.

Laugh-Out-Loud Cats #2479

A repeat, really, but more relevant now than ever.

2014/1/10

Laugh-Out-Loud Cats #2401

The foxes have holes, and the birds of the air nests; but the Son of man hath not where to lay his head.

— Matthew 8:20 & Luke 9:58
Jesus was the original hobo???

Laugh-Out-Loud Cats #2401

The foxes have holes, and the birds of the air nests; but the Son of man hath not where to lay his head.

— Matthew 8:20 & Luke 9:58

Jesus was the original hobo???

Anyone paying even cursory attention to U.S. foreign policy in recent decades will recognize that Washington’s response to Egypt and Syria is part of a much bigger story. The story is this: America’s national-security elites act on the assumption that every nook and cranny of the globe is of great strategic significance and that there are threats to U.S. interests everywhere. Not surprisingly, they live in a constant state of fear.

John Mearsheimer - America Unhinged (Brad Pitt was right: the things you own end up owning you.)

2014/1/05

On using Go channels like Python generators

I really like Go, and I really like Python, so it’s natural that I apply patterns I’ve learned in Python to Go… even when it’s maybe not appropriate.

For example, in Python, it’s common to use generators to process a stream of data. Take this simple (unrealistic, inefficient) odd number generator:

>>> def odds(from_, to):
...     for n in range(from_, to):
...         if n % 2:
...             yield n
... 
>>> print(*odds(0, 10), sep=", ")
1, 3, 5, 7, 9

How would you do something like that in Go? The natural approach is to use channels. Go’s channels are a language primitive that allows you to send values from one light-weight green-ish thread (“go-routine”) to another. Many other languages would make these into methods in a standard library threading package, but since they’re language primitives in Go, they get used more often, even in simple programs.

Here is an incorrect first attempt at simulating generators using channels:

package main

import "fmt"

func Odds(from, to int, c chan int) {
    if from%2 == 0 {
        from += 1
    }
    for i := from; i < to; i += 2 {
        c <- i
    }
}

func main() {
    c := make(chan int)
    go Odds(0, 10, c)
    for v := range c {
        fmt.Println(v)
    }
}

This program ends with fatal error: all goroutines are asleep - deadlock! because the loop in for v := range c never quits. It never quits because the channel is never closed. An even worse version of this would be one where go Odds(0, 10, c) was just Odds(0, 10, c). That ends with a deadlock before even printing anything.

What is a non-terrible version of this code? We should move more of the responsibility into the Odds function. The Odds function should be in charge of creating the channel, spawning a go-routine, and closing the channel when the go-routine is done, so that the caller can be freed of that responsibility.

Here’s what that looks like:

package main

import "fmt"

func Odds(from, to int) <-chan int {
    c := make(chan int)
    go func() {
        defer close(c)
        if from%2 == 0 {
            from += 1
        }
        for i := from; i < to; i += 2 {
            c <- i
        }
    }()
    return c
}

func main() {
    for v := range Odds(0, 10) {
        fmt.Println(v)
    }
}

There are a couple of thing to notice here. First, the caller doesn’t need to use the go statement because the Odds function now has an anonymous function inside of it that it uses to spawn a go-routine. (This use of an anonymous function looks a bit like Javascript to me.)

Second, the caller doesn’t need to create its own channel to give to our pseudo-generator. Not only that, since the type of the channel is <-chan int, it’s perfectly clear that the channel is only for receiving ints, not for sending, and this is enforced at compile time.

This leads to the most important point: that it doesn’t deadlock at the end! That’s because the channel gets closed. Generally speaking, in Go the one doing the sending on a channel should also be the one to do the closing of the channel when it’s done sending. (Double closing a channel causes a runtime panic.) Since the Odds function returns a receive-only channel, the compiler actually enforces that the close cannot be done in the main function.

But notice how it gets closed, with defer close(c). In Python, the with statement is used for resource management things like remembering to close files and sockets. In Go, defer is used to ensure that a certain command happens whenever a certain function ends (whether in a normal return or an exception-like panic). As a convention, it’s a good idea to defer the close of something as soon as it’s applicable.

So far so good, but what about this scenario:

func main() {
    for v := range Odds(0, 10) {
        fmt.Println(v)
        if v > 5 {
            fmt.Println("Tired of counting all day.")
            return
        }
    }
}

The channel returned by Odds wasn’t exhausted, so it was never closed. Since the function returning was main, this isn’t a big deal in this case, but this is actually a somewhat subtle bug that can affect otherwise correct-looking code. It’s a memory leak, or more specifically a go-routine leak. Python and Go are both garbage collected, but there’s a subtle difference here. In Python if I write gen = odds(0, 10) and then next(gen) the resulting generator will be cleaned up shortly after gen goes out of scope. Similarly, in Go the channel created by c := Odds(0, 10) will be garbage collected once all references to it go away. Go-routines, however, only go “out of scope” when they return or panic, since it can have side-effects independent of whether anything in the main thread has a reference to it. (E.g.)

The standard way to prevent go-routine leaks is through the use of “quit channels.” However, this looks like it’s just going to re-introduce a lot of the complexity that we got rid from the first/incorrect example. We need to make up a new channel to pass around, figure out what value to send as a quit signal, etc.

A quit channel is more complicated than just letting a go-routine leak or run to exhaustion, but there are a few tricks we can take advantage of to make it less annoying:

package main

import "fmt"

func Odds(from, to int, quit <-chan struct{}) <-chan int {
    c := make(chan int)
    go func() {
        defer close(c)
        if from%2 == 0 {
            from += 1
        }
        for i := from; i < to; i += 2 {
            select {
            case <-quit:
                return
            case c <- i:
            }
        }
    }()
    return c
}

func main() {
    quit := make(chan struct{})
    defer close(quit)
    for v := range Odds(0, 10, quit) {
        fmt.Println(v)
        if v > 5 {
            fmt.Println("Tired of counting all day.")
            return
        }
    }
}

First of all, what is <-chan struct{}? type struct {} is a type for hanging methods off of with no associated data. chan struct{} is a channel that passes no data. You could also write chan bool but that has two potential values, true or false. struct{} lets your readers know, “There’s no data being passed here.” The only reason for the channel to exist is to synchronize things. No other information is being sent. The arrow in front of <-chan struct{} further specifies that the caller is the one who will be in charge of the quit channel, not the function.

Next, select (not to be confused with switch, which also has cases) is a statement in Go that lets you send or receive from multiple channels at once. If multiple channels are ready simultaneously, the runtime will pseudo-randomly select just one to use, otherwise the first channel to be ready will fire. In this case, it means “either send the value of i on the output channel or return if the quit channel has something.”

When you close a channel, from then on anyone listening to that channel receives the blank value for that type (0 for int, "" for string, etc.) Instead of using close, you could also send an empty struct:

func main() {
    quit := make(chan struct{})
    for v := range Odds(0, 10, quit) {
        fmt.Println(v)
        if v > 5 {
            fmt.Println("Tired of counting all day.")
            quit <- struct{}{}
        }
    }
}

close(quit) is less ugly than quit <- struct{}{}, but the real reason I think it’s better to use close gets at my next couple of points.

Why didn’t I put the make for the quit channel into Odds like I did for the chan int in the second example? It certainly could be done, but fundamentally a caller knows better than the function itself when the go-routine should quit, so the caller should have the responsibility. This lets us do shortcuts like this:

func main() {
    for v := range Odds(0, 10, nil) {
        fmt.Println(v)
    }
}

Since the caller knew the channel would be drained to exhaustion, it didn’t need to make a quit channel at all. In Go, nil channel never sends, but there’s nothing illegal or panic-inducing about listening to a nil channel, so Odds works just fine.

Furthermore, we can reuse the same quit channel for multiple functions. If we were sending the quit signal with quit <- struct{}{}, we’d have to send one message for each go-routine that reuses the same quit channel and needs to be closed. However because closed channels are always “sending” their blank value, we can stop all of the go-routines that are listening to the same quit channel by just doing close(quit) once.

Put it all together and this approach allows for more advanced (and Python generator-like) usages of channels. The second Project Euler problem is “By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.” Here is an over-engineered solution using Go’s channels:

package main

import "fmt"

func sendOrFail(n int64, out chan<- int64, quit <-chan struct{}) bool {
    select {
    case out <- n:
        return true
    case <-quit:
        return false
    }
}

func FibGen(quit <-chan struct{}) <-chan int64 {
    out := make(chan int64)
    go func() {
        defer close(out)
        var last, current int64 = 1, 2

        if ok := sendOrFail(last, out, quit); !ok {
            return
        }
        for {
            if ok := sendOrFail(current, out, quit); !ok {
                return
            }
            last, current = current, last+current
        }
    }()
    return out
}

func Evens(in <-chan int64, quit <-chan struct{}) <-chan int64 {
    out := make(chan int64)
    go func() {
        defer close(out)
        for n := range in {
            if n%2 == 0 {
                if ok := sendOrFail(n, out, quit); !ok {
                    return
                }
            }
        }
    }()
    return out
}

func TakeUntil(max int64, in <-chan int64, quit <-chan struct{}) <-chan int64 {
    out := make(chan int64)
    go func() {
        defer close(out)
        for n := range in {
            if n > max {
                return
            }
            if ok := sendOrFail(n, out, quit); !ok {
                return
            }
        }
    }()
    return out
}

func main() {
    var total int64

    quit := make(chan struct{})
    defer close(quit)
    for n := range Evens(TakeUntil(4000000, FibGen(quit), quit), quit) {
        total += n
    }
    fmt.Println("Total:", total)
}

Compare to similar Python code:

>>> def fibgen():
...     last, current = 1, 2
...     yield last
...     while True:
...         yield current
...         last, current = current, last + current
... 
>>> def take_until(max, gen):
...    for n in gen:
...       if n > max:
...           return
...       yield n
... 
>>> def evens(gen):
...    for n in gen:
...       if n%2 == 0:
...           yield n
... 
>>> sum(evens(take_until(4000000, fibgen())))
4613732

The Go version isn’t as simple as a basic approach using Python generators. It’s 73 lines versus only 20 lines of Python. (15 lines just for all of those closing }.) Still, Go has certain advantages, like being concurrent and type safe. In real code, you could imagine making and processing multiple network calls concurrently using techniques like this. The lowest level generator opens a socket to get some data from somewhere on the internet, then it pipes it results out to a chain of listeners that transform the data. In a case like that, Go’s easy concurrency could be an important advantage over Python.

2013/10/28

【昭和】昔は当たり前だった、あんなことや、こんなこと・・・。 [The Shōwa Era: In the Olden Days, it was just like this and that…]

So, a major genre of writing on the internet is nostalgia. Some people blame this on Gen Y, but those people apparently never watched The Wonder Years or Happy Days. What’s interesting to me is that this webpage is a list of nostalgic things for Japanese people, and most of these are completely baffling for Westerners. Here’s an abridged loose translation:

  • At hotels, you had to pay to watch TV, even broadcast channels.

  • For tests, you used cheap, coarse paper.

  • You had to keep score at bowling by hand.

Same.

  • Girls went to school with triple braided hair.

  • There were mom-and-pop video stores.

Same.

  • If you went out to the countryside, even if you found a public bathroom, there was a fifty-fifty chance you’d need to use toilet paper you brought yourself.

  • TV broadcasts of movies were shown with an explainer.

  • Moms wore Japanese-style aprons.

  • There were doilies on door knobs, tissue boxes, and telephones.

  • Sometimes TV channels would just announce, “We’ll be back shortly.”

  • Around New Years, stores would be closed for three days, so if you forgot to go shopping beforehand, you were out of luck.

  • Male-female pairs were referred to with the French word avec rather than the English couple.

  • You would hold a cassette recorder up to the TV to record your favorite songs.

Same.

  • Kids in photos would wear baseball caps and shorts while making a peace sign.

  • School lunch trays were made of anodized aluminum.

  • If a baseball game went too long, the video recorder would fail.

  • You could smoke on trains.

This was still true when I lived in Japan in 2006.

  • You could ride on a motorcycle without a helmet or in a car without a seatbelt.

  • People had wooden statues of a bear catching a fish in a river.

  • At a public bath, you’d hear kids yell over the divider, “Mom lend me some shampoo” or “Dad can I have the soap” and go naked from the male side to female side or vice versa.

  • Kids wore “duck shoes.”

  • Whale meat was a meal for ordinary people.

This is behind a lot of the push to put whale meat in public schools in Japan. It’s basically proletariat nostalgia.

And finally this ad was sure to be in the back of magazines. (Click the link even, or especially, if you don’t read Japanese.)

Via Araki Yūko

Nedroid Picture Diary - Sweet Tooth

2013/10/25

As a characterization of U.S. policy at any time in memory, isolationism is a fiction. Never really a tendency, it qualifies at most as a moment, referring to that period in the 1930s when large numbers of Americans balked at the prospect of entering another European war, the previous one having fallen well short of its “War To End All Wars” advance billing.

In fact, from the day of its founding down to the present, the United States has never turned its back on the world. Isolationism owes its storied history to its value as a rhetorical device, deployed to discredit anyone opposing an action or commitment (usually involving military forces) that others happen to favor.

Andrew J. Bacevich - 70 Years of “New Isolationism”

(I say, embrace the slur: I’m an isolationist and I’m proud.)

xkcd - Halting Problem

A computer is a machine that solves problems. On a long enough timeline, all problems are temporary.

xkcd - Halting Problem

A computer is a machine that solves problems. On a long enough timeline, all problems are temporary.

2013/10/20

kerokerobonito:

Kero Kero Bonito, Homework (2013).

Dig that sound.

Also check out their album, Intro Bonito. I like how the vocalist, Sarah, smoothly mixes Japanese raps and English singing. She sounds a bit like HalCali in Japanese, and it’s quite often funny (she compares pregnancy to Alien, that sort of thing). It’s a little too precisely targeted at the “people named Carl Johnson” demographic, but don’t let that turn you off.

Incidental Comics - The Ghosts of Creativity

2013/9/07

MTV 81 - Buffalo Daughter video interviews

Interesting interviews in support of their new album. See also part two, which has a discussion of the role of fūdo in rock music.

A Softer World - 981

Yan Hui said, ‘I am making progress.’ 
Confucius replied, ‘What do you mean?’ 
‘I have forgotten all about humaneness and righteousness.’ 
‘Very well; but that is not enough.’  
Another day, Hui again saw Confucius, and said, ‘I am making progress.’ 
‘What do you mean?’ 
‘I have forgotten all about rituals and music.’ 
‘Very well, but that is not enough.’ 
A third day, Hui again saw the Master, and said, ‘I am making progress.’ 
‘What do you mean?’ 
‘I sit and forget everything.’ 
Confucius changed countenance, and said, ‘What do you mean by saying that you sit and forget everything?’ 
Yan Hui replied, ‘My connection with the body and its parts is dissolved; my perceptive organs are discarded. Thus leaving my material form, and bidding farewell to my knowledge, I am become one with the Great Throughway. This I call sit it and forget it.’  
Confucius said, ‘If you’re one with it, then you are free from all likings; so transformed, you are become impermanent. You have, indeed, become superior to me! I must ask leave to follow in your steps.’

– Zhuangzi 6

A Softer World - 981

Yan Hui said, ‘I am making progress.’
Confucius replied, ‘What do you mean?’
‘I have forgotten all about humaneness and righteousness.’
‘Very well; but that is not enough.’

Another day, Hui again saw Confucius, and said, ‘I am making progress.’
‘What do you mean?’
‘I have forgotten all about rituals and music.’
‘Very well, but that is not enough.’

A third day, Hui again saw the Master, and said, ‘I am making progress.’
‘What do you mean?’
‘I sit and forget everything.’

Confucius changed countenance, and said, ‘What do you mean by saying that you sit and forget everything?’

Yan Hui replied, ‘My connection with the body and its parts is dissolved; my perceptive organs are discarded. Thus leaving my material form, and bidding farewell to my knowledge, I am become one with the Great Throughway. This I call sit it and forget it.’

Confucius said, ‘If you’re one with it, then you are free from all likings; so transformed, you are become impermanent. You have, indeed, become superior to me! I must ask leave to follow in your steps.’

Zhuangzi 6