5 ms·
> Before we move on, I want to point out that “being able to write code from left to right without backtracking” is a completely bonkers thing to optimize a pro
by hnlmorg 3y ago
> Before we move on, I want to point out that “being able to write code from left to right without backtracking” is a completely bonkers thing to optimize a programming language for. This should not be anywhere in the top hundred priorities for any “serious programming language”!
There are plenty of serious languages that are written this way. Most noticeably are shell scripting languages but I’ve seen stack based and functional languages that are written like this too.
- bmitc 3y agoI don't really even understand what is meant by "being able to write code from left to right without backtracking”, much less why that is bonkers. Scrolling down and seeing the code examples, the language even seems quite conventional, so I'm even more confused.
- KeplerBoy 3y agoIsn't that a whole lot more "being able to write code without making a lot of dumb mistakes", which is a feature of the programer. So it boils down to getting gud and practice?
- Cthulhu_ 3y agoThat's what it sounds like to me. Thinking about a line of code before writing it down, or knowing what the code will do instead of trial and error (which... is what I often do, lol)
- ThreeToZero 3y agoThe post gives an example of how python doesn't have this feature in the section [https://blog.vero.site/post/noulith#coding-with-and-without-infix-functions https://blog.vero.site/post/noulith#coding-with-and-without-...] Python fails this criteria because if you type as you think through the process, you have to move the cursor to the beginning to prefix the 'map' around the input. For example this series of transforming the input: puzzle_input.split("\n\n") map(ints, puzzle_input.split("\n\n")) map(sum, map(ints, puzzle_input.split("\n\n"))) max(map(sum, map(ints, puzzle_input.split("\n\n")))) ---------------------- Compare to this postfix syntax where you can write this incrementally as you think through the operations: puzzle_input split "\n\n" map ints map sum then max
- bmitc 3y agoThanks for pointing out that specific example and comparison. The solution seems like it's just a less readable pipeline. puzzle_input split "\n\n" map ints map sum then max; puzzle_input split "\n\n" map ints map sum then sort then (_[-3:]) then sum;
- dekhn 3y agoafter a while, my brain reorganized and i think of the map() first, before thinking about the content of the map, basically to avoid the keystrokes required to go to beginning of line, then back to where I was typing.
- hnlmorg 3y agoNo, what the author is discussing is a syntax thing. Lets take a hypothetical C-like language: result = C(B(A())) In this your result is on the left hand side. The first function to be executed is A and then B and lastly C, which also reads right to left. This is a pretty common way to write code, it's by no means unique to C-like languages. So you'd have gotten so good at reading code like this that you probably don't even realise you're reading right to left. Now lets look at a POSIX-like shell language: result = $(A | B | C) Here the result is still on the left hand side but now you're reading the functions from left to right (ie pipes) I'm not the article author by my own programming language takes things a step further from conventional shells and you can do the following: A -> B -> C -> set result Here it reads fully left to right. There is zero confusion about which order to read this. ---------------- Going back to the more general point about reading left to right, it's worth noting that even math operators can be extended this way. For example with polish notation (https://en.wikipedia.org/wiki/Polish_notation https://en.wikipedia.org/wiki/Polish_notation) your operators precede your values. Effectively turning those symbols into function names: + 2 5 ...would return 7 Though personally I prefer the more traditional format of operators sitting between their values (2+5), but that's purely because that is what I'm used to.
- xiaq 3y agoYou can write A | B | C | result=$(cat) in shell. Edit: this only works if the shell runs the last command in the current environment (as opposed to a subshell), and only ksh and zsh seem to do that...
- deleted 3y ago[deleted]
- _0ffh 3y agoOr with uniform function call syntax result = A().B().C()
- deleted 3y ago[deleted]
- Strilanc 3y agoHere's an example. In SQL the "select" clause comes at the start. In C# LINQ queries, "select" comes at the end. A major resulting difference is autocomplete works in the latter case but not the former. A more pervasive example is how OOP languages do x.f(...) instead of f(x, ...). This also helps with autocomplete. Also it results in calls chaining like x.y(...).z(...) instead of nesting like z(y(x(...), ...), ...). These kinds of things have noticeable effects on how easy it is to discover relevant methods and how fast they are to type.
- frou_dh 3y agoTalking of shell, the preference for left-to-right is often used to justify "useless use of cat", i.e. `cat file | command` instead of `command <file`, but it can just be written `<file command` instead.
- robertlagrant 3y agoI'm surprised that file> command isn't the preferred idiom. Does that mean something else?
- hnlmorg 3y agoYes, that would write to a file called "command". So the correct way to write that would be: command > file https://www.gnu.org/software/bash/manual/html_node/Redirections.html https://www.gnu.org/software/bash/manual/html_node/Redirecti...
- smrq 3y agoThat would execute the command `file` and put the output into the file `command`.
- robertlagrant 3y agoOh, now I re-read it - of course! Hah.
- sgarland 3y agoIs `<file` shorthand for `cat file`, or is that only true when used in a sub-shell?
- mpweiher 3y agoYeah, Smalltalk also works this way...mostly. 3 negated + 2 negated. Alas, as with the post, this also breaks down when you have more than 2 arguments (or in Smalltalk parlance, 1 argument in addition to the message receiver), as those are handled by keyword arguments and you can't tell where the keywords for one message stop and the ones for the next one start. Let's say we have some nested arrays, which are accessed with at: in Smalltalk: array at:4 at:2 at:1. Alas, that doesn't get interpreted as 3 messages, but as the single message at:at:at:. As it kind of has to be as there is no way to disambiguate. Surprisingly, Smalltalk does have a way to chain messages and thus separate the keywords, the semicolon: array at:4; at:2; at:1. Alas, this sends the subsequent messages to the original receiver, so it is equivalent to: array at:4. array at:2. array at:1. (And so this example doesn't actually make sense, it's just a syntax example). So what you have to do is add parens: ((array at:4) at:2) at:1. Hmm, not nice. For Objective-S (https://objective.st https://objective.st), I introduced the pipe for message chaining: array at:4 | at:2 | at:1. One way of looking at this is as a syntactic device that allows left-to-right typing without backtracking, which it is. And that is both nice to write and quite readable, IMNSHO. A second way of looking at it is as a version of the pipe/filter architectural style, with each message expression being a filter, the results from the filter on the left piped into the filter on the right as the receiver. This is a little bit like |> in some FP languages. But really only a little bit, because in Objective-S this is not the whole story, but just a way of integrating messaging into the way the pipe/filter architectural style is supported at the language level.
- firejake308 3y agoThe syntax reads similar to R with pipes