1,326 karma · joined Jul 6, 2009 (17y ago) · 248 submissions
comment 13y ago · link · parent
So if you reference the symbol :foo, it will transparently get pinned in memory somewhere. Referencing :foo again will not allocate an additional object - the interpreter will just give you the existing symbol.
This is usually not an issue, because an application typically operates on a small, fixed set of known symbols. But if an attacker can generate arbitrary symbols (e.g. if you call .to_sym on user input), he can exhaust memory.
comment 14y ago · link · parent
First, the term "tail recursion" is overly narrow. This transformation should apply to all tail calls, not just recursive ones. This is useful, for example, in families of mutually recursive functions.
Second, this transformation is not really an optimization, as it changes the behavior of some programs (by preventing stack overflow or memory allocation errors). It's a documented feature of languages (or specific implementations of languages) that users rely on to write programs in a particular style.
I prefer the term "tail call elimination" to describe these transformations.
comment 14y ago · link · parent
But right and wrong guesses are not equally valuable in hangman. A wrong guess will eliminate all words of a given length containing the given letter. But a correct guess gives you more: you get the location (or locations) of the letter in the word as a bonus. This will reduce your remaining search space drastically.
Thus, you should optimize for the success of your next guess.
comment 14y ago · link · parent
When you need to wrap up a common block of code, you use a function. This lets you call the same block in a number of different contexts. When you need to fix the context in place and let the block in the middle vary, you write a function that takes another function as a parameter. An example in JavaScript (might be a little off):
var withFile = function (path, action) {
var f;
try {
f = File.open(path);
action(f);
} finally {
File.close(f);
}
};
withFile('foo.txt', function (f) { ... });
This can be generalized (to some extent and depending on language) so you don't need to write the same structures every time: var context = function (prolog, epilog) {
return function (action, ...args) {
var o;
try {
o = prolog.apply(args);
action(o);
} finally {
epilog(o);
}
};
};
var withFile = context(File.open, File.close);
This example uses JavaScript' hypothetical future "rest" parameters, but there are other ways to manage the same goal.(Other comments have mentioned the author conflates context management with applying reversible functions. I've only dealt with one of those things here, but similar techniques apply in solving both problems.)
comment 14y ago · link · parent
Also, consider the stated goal of the project. They wanted to write a throwaway compiler so that they could bootstrap a Pascal version. In this case, it's a bad choice to use a language (Fortran) that's so different from Pascal. On the other hand, assembly is very flexible, so you can use whatever idioms you think will apply in the Pascal code you later intend to write. This will make the translation much simpler.
comment 14y ago · link · parent
Don't discard unsexy solutions out of hand; choose the right tool for the job.
comment 15y ago · link · parent
Not really a C language trick, but useful nonetheless.
comment 15y ago · link · parent
Some languages (I'm thinking of ML and Haskell) require you to opt into nullability with an option type (e.g. http://www.standardml.org/Basis/option.html).
What's worse is that many mainstream languages do not allow you to opt out of nullability. I.e. most or all types are always nullable.
232.
4 points · by sdevlin · 15y ago · 5 comments
comment 15y ago · link · parent
Also, this feels like piling on, but I think the original (intentionally erroneous) example would print '5' repeatedly rather than '4'. var i ends up with the value 5.
comment 15y ago · link · parent
This is why we often give novices rules of thumb like "don't use macros" or "don't use goto." I've seen some people criticize this practice, but I think it's actually a good thing; when you're just starting out, you don't know when these things are really called for. Working under some constraints simplifies things.
With experience, you learn the pain points well enough to outgrow those rules and use macros/goto/whatever when called for.
235.
Namespacing in JavaScript(javascriptweblog.wordpress.com ↗)
3 points · by sdevlin · 15y ago · discuss
comment 16y ago · link · parent
We could simply add a getTemplate() method, but then we'd be adding methods to the public interface solely to allow testing, which is not a good approach.
It seems like his solution does this anyway by exposing the supplant function. Despite the underscore, it's not private; it's that brand of smile-and-a-wink private popular in python. I'm not a big fan of that. I like to keep the interface simple for consumers of the class, and even if I know to ignore underscored members, it's still noise.That said, I don't have a better solution. Maybe the trade-off is worth it for well-tested software. I suppose you could structure your code so that objects can respond to some debug or test flag that forces them to expose their internals, but that might be a lot of work for little benefit. At the end of the day, maybe it's best to call this "good enough" and get back to work.
comment 16y ago · link · parent
Microsoft writes nice tools, and they all work well together. It's a truly integrated environment up and down the stack. And for the simple cases, it's great. I hit F5, and my app runs, right out of the box.
But I find the tools difficult to extend, when it comes to it. They are mostly black boxes. This means I'm dependent on MS to add features before I can use them easily. It's possible to write plugins for VS, etc., but I don't get the sense that most people do that. I know I don't. It seems difficult and time-consuming; I'd rather just live with less than ideal behavior.
But I've been trying to branch out into other stuff in the past year or so and dabble in different things (app engine, some rails, some C code). What I've noticed is that low level tools like make are cool for a few reasons.
First, regardless of its syntax, make is pretty damn simple for the simple cases. Even as a novice, its easy for me to tweak a makefile to suit my needs. I think this simplicity is valuable. By the same token, the generated code for rails is all pretty easy to follow and tinker with. That's not always the case when you pull back the curtain from Visual Studio.
Second, make (and others) are orthogonal to the other tools I'm working with. It doesn't matter what editor or framework or languages I'm using. It's pretty easy to whip up a makefile to compile or deploy a dev build or whatever. This is true because steps in a makefile are expressed in terms of terminal commands. Simple and powerful.
Third, make is ubiquitous. I think it's pretty cool that I can use the same makefile to deploy my app engine site from Ubuntu or OSX.
I guess what I'm trying to say is that MS's powerful tool set feels oddly constraining to me. I feel much more in control with a set of simple and independent tools, even though I'm much less experienced with them.
Just my two cents.
comment 16y ago · link · parent
Given that, it's not a stretch to imagine that syntax could be useful in other scenarios.
comment 16y ago · link · parent