apophenia comments on Open Thread: July 2010, Part 2 - Less Wrong

6 Post author: Alicorn 09 July 2010 06:54AM

You are viewing a comment permalink. View the original post to see all comments and the full post content.

Comments (770)

You are viewing a single comment's thread. Show more comments above.

Comment author: apophenia 10 July 2010 10:18:52PM 2 points [-]

There are really people who don't get pointers.

Comment author: Morendil 10 July 2010 10:30:20PM 2 points [-]

One of the epiphanies of my programming career was when I grokked function pointers. For a while prior to that I really struggled to even make sense of that idea, but when it clicked it was beautiful. (By analogy I can sort of understand what it might be like not to understand pointers themselves.)

Then I hit on the idea of embedding a function pointer in a data structure, so that I could change the function pointed to depending on some environmental parameters. Usually, of course, the first parameter of that function was the data structure itself...

Comment author: apophenia 10 July 2010 10:59:46PM 1 point [-]

Usually, of course, the first parameter of that function was the data structure itself...

Cute. Sad, but that's already more powerful than straight OO. Python and Ruby support adding/rebinding methods at runtime (one reason duck typing is more popular these days). You might want to look at functional programming if you haven't yet, since you've no doubt progressed since your epiphany. I've heard nice things about statically typed languages such as Haskell and O'Caml, and my personal favorite is Scheme.

Comment author: sketerpot 11 July 2010 05:50:09PM 2 points [-]

Oddly enough, I think Morendil would get a real kick out of JavaScript. So much in JS involves passing functions around, usually carrying around some variables from their enclosing scope. That's how the OO works; it's how you make callbacks seem natural; it even lets you define new control-flow structures like jQuery's each() function, which lets you pass in a function which iterates over every element in a collection.

The clearest, most concise book on this is Doug Crockford's Javascript: The Good Parts. Highly recommended.

Comment author: apophenia 13 July 2010 04:09:58AM *  1 point [-]

The technical term for this is a closure. A closure is a first-class* function with some associated state. For example, in Scheme, here is a function which returns counters, each with its own internal ticker:

(define (make-counter)

(let ([internal-variable 0])

 (lambda ()
(begin (set! internal-variable (+ internal-variable 1))
internal-variable))))

To create a counter, you'd do something like

(define counter1 (make-counter 0))

(define counter2 (make-counter 0))

Then, to get values from the counter, you could call something like

(display (counter1))

(display (counter2))

(display (counter2))

(display (counter2))

(display (counter1))

(display (counter2)) which prints: 1, 1, 2, 3, 2, 4.

Here is the same example in Python, since that's what most people seem to be posting in:

def make_counter():

def counter(internal_variable=[0]):

 internal_variable[0] += 1
return internal_variable[0]

return counter

counter1 = make_counter()

counter2 = make_counter()

print(counter1())

print(counter2())

print(counter2())

print(counter2())

print(counter1())

print(counter2())

*That is, a function which you can pass around like a value.

Comment author: sketerpot 13 July 2010 05:40:21AM 0 points [-]

While we're sharing fun information, I'd like to point out a little-used feature of Markdown syntax: if you put four spaces before a line, it's treated as code. Behold:

(define (make-counter)
(let ([internal-variable 0])
(lambda ()
(begin (set! internal-variable (+ internal-variable 1))
internal-variable))))

Also, the emacs rectangle editing functions are good for this. C-x r t is a godsend.