What really frustrates me with go is that it doesn't have a mature debugger so I have to resort to inserting print statements everywhere.
I Challenge You to Debug These 7 Lines of Code Under 9 Minutes
11–20 of 37 posts
Re: I Challenge You to Debug These 7 Lines of Code Under 9 Minutes
#12It's a common JavaScript interview question to ask about capturing variables in a closure. What really frustrates me with go is that it doesn't have a mature debugger so I have to resort to inserting print statements everywhere.
Re: I Challenge You to Debug These 7 Lines of Code Under 9 Minutes
#13Re: I Challenge You to Debug These 7 Lines of Code Under 9 Minutes
#14Re: I Challenge You to Debug These 7 Lines of Code Under 9 Minutes
#15It's a common JavaScript interview question to ask about capturing variables in a closure. What really frustrates me with go is that it doesn't have a mature debugger so I have to resort to inserting print statements everywhere.
Re: I Challenge You to Debug These 7 Lines of Code Under 9 Minutes
#16Haha racket can here is how.
#lang racket
(define (foo a [methods empty])
(let ([methods
(append methods
(for/list ([i (range 3)])
(lambda (x) (+ x i))))])
(for/sum ([m methods])
(m a))))
(foo 0) ;; Should be 3
(foo 1) ;; Should be 6
(foo 2) ;; Should be 9
(foo 2 (list (lambda (x) x))) ;; Should be 11
(foo 0)
(foo 1)
(foo 2)
;; Now the wrong way
;; The lambda capturing wrong
(define (foo a [methods empty])
(define i 0)
(let ([methods
(append methods
(for/list ([z (range 3)])
;; modifying the variable i
(set! i z)
(lambda (x) (+ x i))))])
(for/sum ([m methods])
(m a))))
;; this one doesn't do the default parameter wrong, because
;; methods and methods-default share an imutable list and we
;; modify only methods so beside "empty" they won't share the same
;; reference anymore, to get it wrong we should use a mutable list
;; (doesn't exist in racket).
(define foo
(let ([methods-default empty])
(lambda (a [methods methods-default])
;; (printf "~a ~a\n" (length methods) (length methods-default))
(define i 0)
(for ([z (range 3)])
(set! i z)
(set! methods (cons (lambda (x) (+ x i))
methods)))
(for/sum ([m (append methods methods-default)])
(m a)))))
;; So i use a box to make it mutable.
(define foo
(let ([methods-default (box empty)])
(lambda (a [methods methods-default])
;; (printf "~a ~a\n" (length (unbox methods)) (length (unbox methods-default)))
(define i 0)
(for ([z (range 3)])
(set! i z)
;; methods and methods-default share the same reference of box.
(set-box! methods (cons (lambda (x) (+ x i))
(unbox methods))))
(for/sum ([m (unbox methods)])
(m a)))))
The moral of the story, language design matter, immutable by default is good, lexical scoping is good, binding over variable is good, ruby, python, php and javascript (the languages the author mention) are bad in that regards.One thing php, ruby, python (but not javascript) has good over racket their identatation tend to be flat and racket (let form, no return statement) tend to go all the way to the right.
Re: I Challenge You to Debug These 7 Lines of Code Under 9 Minutes
#17If you want to talk about statistical significance, don't say "not completely". It either is or it isn't. If you think the p-value is important (and I'm not saying it is, but don't bother mentioning it if you don't think it is), then you can't claim a 35% speedup. If you're reporting results in the context of significance, and you decided that your results were not significant, then the 35% speedup is meaningless.
Re: I Challenge You to Debug These 7 Lines of Code Under 9 Minutes
#18Is this a designed feature or something that just happened? I really think that this behaviour is erroneous and unexpected. With python (and many other languages) I find myself getting bitten by behaviour like this every now and then. I use scheme for almost all my personal projects, and I can only remember getting bitten twice. Once was unexpected, and the other was laziness on my part.
Basically it just boils down to reference vs value. Both of the errors in this code occur because python uses a reference instead of a value – I agree that they can be annoying and somewhat unexpected, but they make sense once you realize what's behind them. In the method=[] part, the error occurs because python creates a single list, and passes that list by reference (as all lists are passed) into the function. In t…
However the default parameter issue looks like a python specific issue which is really strangely designed. I would normally say [] is a literal for an empty list which creates a new instance of an empty list every time it's used. So when it's used as a default parameter a fresh empty list should be created all times.
Is [] is global reference to a single mutable list which is only initially empty? That would make it pretty useless, since everyone could mutate it. If it's a single reference then that should at least be an immutable list which throws some exception when someone messes around with it.
Re: I Challenge You to Debug These 7 Lines of Code Under 9 Minutes
#19Great intro to PDB, but I have to be critical of: "The second group took 9 minutes on average, and the first one 14 minutes! Those are not completely statistically significant, but that’s still a 35% speedup on an 8-line-long program." If you want to talk about statistical significance, don't say "not completely". It either is or it isn't. If you think the p-value is important (and I'm not saying it is, but don't bot…
Re: I Challenge You to Debug These 7 Lines of Code Under 9 Minutes
#20"If I lied, you are allowed to insult me abundantly through your favorite channel." Haha racket can here is how. #lang racket (define (foo a [methods empty]) (let ([methods (append methods (for/list ([i (range 3)]) (lambda (x) (+ x i))))]) (for/sum ([m methods]) (m a)))) (foo 0) ;; Should be 3 (foo 1) ;; Should be 6 (foo 2) ;; Should be 9 (foo 2 (list (lambda (x) x))) ;; Should be 11 (foo 0) (foo 1) (foo 2) ;; Now th…