Left to Right Programming: Programs Should Be Valid as They Are Typed

https://news.ycombinator.com/rss Hits: 10
Summary

2025-08-17 Left to Right Programming Programs Should Be Valid as They Are Typed I don’t like Python’s list comprehensions: text = "apple banana cherry\ndog emu fox" words_on_lines = [line.split() for line in text.splitlines()] Don’t get me wrong, declarative programming is good. However, this syntax has poor ergonomics. Your editor can’t help you out as you write it. To see what I mean, lets walk through typing this code. words_on_lines = [l Ideally, your editor would be to autocomplete line here. Your editor can’t do this because line hasn’t been declared yet. words_on_lines = [line.sp Here, our editor knows we want to access some property of line, but since it doesn’t know the type of line, it can’t make any useful suggestions. Should our editor flag line as a non-existent variable? For all it knows, we might have meant to refer to some existing lime variable. words_on_lines = [line.split() for line in Okay, now we know that line is the variable we’re iterating over. Is split() a method that exists for line? Who knows! words_on_lines = [line.split() for line in text.splitlines()] Ah! now we know the type of line and can validate the call to split(). Notice that since text had already been declared, our editor is able to autocomplete splitlines(). This sucked! If we didn’t know what the split() function was called and wanted some help from our editor, we’d have to write words_on_lines = [_ for line in text.splitlines()] and go back to the _ to get autocomplete on line.sp You deserve better than this. To see what I mean, lets look at a Rust example that does it better. †[The most elegant solution here is Haskell’s map words $ lines text but that breaks all the principles I’m arguing for.] let text = "apple banana cherry\ndog emu fox"; let words_on_lines = text.lines().map(|line| line.split_whitespace()); If you aren’t familiar with Rust syntax, |argument| result is an anonymous function equivilent to function myfunction(argument) { return result; } Here, your progra...

First seen: 2025-08-18 17:42

Last seen: 2025-08-19 03:48