Better HN
topnewbestaskshowjobs
Log inSign up

sdevlin

1,326 karma · joined Jul 6, 2009 (17y ago) · 248 submissions

Submissions

comment 13y ago · link · parent

Depending on how they're hashed, that may or may not be an effective countermeasure.

comment 13y ago · link · parent

Symbols in Ruby are basically interned strings.

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 13y ago · link · parent

It's probably worth noting that Contre Jour was originally an iOS game. The HTML5 version is a port of the original. (Not to take anything away from it!)

comment 14y ago · link · parent

Nice article.

This makes me wonder if Visual Studio's F# mode could help detect common errors, e.g. highlight recursive calls not made in tail position.

On the other hand, this could get annoying as such calls are often not a real problem in practice.

comment 14y ago · link · parent

Good point, I'm not very familiar with Scala. I think Clojure may also have this limitation.

comment 14y ago · link · parent

Decent explanation, but I have a couple quibbles.

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

Ha, I'm wrong. I thought you could set the referer with setRequestHeader on an XHR. Mea maxima culpa.

comment 14y ago · link · parent

The referer header can easily be forged. The whole point of a CSRF attack is to turn a user's credentials against him.

comment 14y ago · link · parent

It's not that the referer header is not "enough". "Enough" implies that it falls somewhere on the scale of trustworthiness.

It's user input. Don't trust user input.

comment 14y ago · link · parent

Your intuition is telling you that you should shoot for 50% so that you can divide the search space in half regardless of the outcome. A right or wrong answer will eliminate half of the possible remaining words.

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

Yeah, you may be right that the full generality of the pattern is not appreciated.

For what it's worth, I think most commonly used languages can implement some form of the context function in my example.

comment 14y ago · link · parent

People have posted a lot of examples in a lot of different programming languages. What they show is that all you really need to build this idiom is first class functions (or some approximation thereof). (It also shows that the title is not correct; many, many people are familiar with this idiom.)

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

C#'s 'using' statements allow this kind of pattern for types implementing IDisposable. It looks something like this (might be a little off):

    using (var file = File.Open("foo.txt"))
    {
        ...
    }

comment 14y ago · link · parent

I think so. Wirth called out Fortran's lack of pointers, records, and recursion. You have (or can fake) all of these things in assembly. Recursion is particularly important for building a compiler, since much of it involves manipulating trees.

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

So they had three tools to choose among. One was broken, one was wildly unsuitable, and one was unpopular. They chose the second for (apparently) purely cultural reasons. I think it's not so surprising that they ran into problems.

Don't discard unsexy solutions out of hand; choose the right tool for the job.

comment 14y ago · link · parent

These bindings are only for M-x. This is still useful (M-x is really common), but it's not a full replacement for all meta bindings.

comment 15y ago · link · parent

This is something that's nice about ML-family languages. Data is not "nullable" by default. If you want to introduce that possibility, you need to wrap it in some form of "option" type - and you're forced to deal with the consequences of that explicitly.

comment 15y ago · link · parent

There is also the top level reversed() function that takes some sequence and returns a reversed iterator.

comment 15y ago · link · parent

Since some people are posting macros (including some intentionally malicious ones), it is handy to know the -E compiler flag for debugging purposes. This will run only the preprocessor on the given files and print the result to stdout. (N.b. -E works at least on gcc and clang; the flag may be different elsewhere.)

Not really a C language trick, but useful nonetheless.

comment 15y ago · link · parent

It's not really having null that's bad. What's bad is making types nullable by default. More often than not, null is not a valid value for a variable (in my experience - YMMV). This is a common source of errors.

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.

comment 15y ago · link · parent

Thanks for the feedback, guys. I'm going to go with gharbad's suggestion and do plain text combined with a PDF via LaTeX.

232.

Ask HN: Plain Text Resumes?

4 points · by sdevlin · 15y ago · 5 comments

comment 15y ago · link · parent

I think you're correct. His interior function is still just another closure around the original var i.

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

It seems like you could say that about any sufficiently powerful construct. Sharper knives require better judgment to be used safely.

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

This comment would be more helpful if you gave him some specific criticisms. What did you have to think about? How could he fix it?

Kilimanjaro, it seemed pretty straightforward to me.

comment 16y ago · link · parent

I write C# code in Visual Studio every day at work, and I've got some thoughts.

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

Yeah, you can simulate that stuff with lambdas. For that matter, you can simulate function calls by following conventions in assembly language. That's what I meant by "awkward/difficult". Surely you'd concede that you're happy to have syntactic support for if-statements and lambdas?

Given that, it's not a stretch to imagine that syntax could be useful in other scenarios.

comment 16y ago · link · parent

Lisp dialects afford you more powerful tools for abstraction. Whereas most languages provide abstraction at the function level, Lisp allows for syntactic abstraction via macros. Think of constructs like the if-statement that only evaluates one of its consequent and alternative, or short-circuiting and- and or-expressions. These things are impossible (or awkward/difficult) in most languages, but Lisp makes them relatively simple.
1
56789