Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> For example, if we wanted to get the first three files from ls I’d do something like `ls | head -n 3`. In Powershell, it’s `$(dir)[0..2]` since the dir command is returning an array which I can index into.

Hmm, yes, but it's not a lazy array/list, so it's not online, is it. If you have a few million files in a directory, that's not going to work. To be fair, neither is `ls | head -n3` -- you have to use `ls -f | ...` to avoid ls(1) sorting the listing first, but if you do, this will be online. For ls(1) it's not really an issue, but in general you want your shell to be online.



>Hmm, yes, but it's not a lazy array/list, so it's not online, is it. If you have a few million files in a directory, that's not going to work.

It is lazy. The author slightly misunderstood / oversimplified what's going on. The commandlet does not build an entire array in memory and then write it out; it writes out each item one at a time. Evaluating it as `$(dir)` just forces all its output to be collected into a single value, ie an array. It's the equivalent of writing `output="$(ls)"; <<< $output head -n3` in bash.

I posted an alternative way to write this in https://news.ycombinator.com/item?id=22963842 which does not need to build the output as an array first and is also more natural to write.


Thanks for the clarification.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: