top | item 39732845

(no title)

gonzus | 1 year ago

I associate this with what we used to call "one-liner programming": I would write a single line of Perl code (in bash, prefixed with `perl -Mwarnings -MDebug -E'<code here>'`, convince myself the code did what we needed, and then share it around as a paste in irc / chat / whatever. It was amazing what you could achieve with the right modules, and Perl's flexibility allowed your mind to roam freely.

Incidentally, the fact that you cannot write this type of one-liner in any meaningful way with Python is one reason I do dislike that language.

discuss

order

fulafel|1 year ago

About Python, are there practical situations outside IRC where you can't include a newline in there? This ..

  python -c '
  import re, sys
  for line in sys.stdin: print(line.lower().count("foo"))
  '
works in shell interactively (both bash and zsh support multiline history entries well), or in shell scripts, and also in slack/mattermost/matrix/email.

teddyh|1 year ago

You can do that as a one-liner (I have kept the unused “re” module import, to keep it as close to your code as possible):

  python -c 'import re, sys; print("\n".join(str(line.lower().count("foo")) for line in sys.stdin))'
or if you want to avoid building one large output string:

  python -c 'import re, sys; print(*(line.lower().count("foo") for line in sys.stdin), sep="\n")'
if memory consumption is still an issue:

  python -c 'import re, sys; set(print(line.lower().count("foo")) for line in sys.stdin)'
(This creates a useless Set object and throws it away as a side effect.)

hnfong|1 year ago

It's sad that Perl is dying a slow death. Between Python and Perl, both styles of coding are possible.

While Python is my preferred language for most tasks, I still use perl as an ad hoc stream processor with `perl -pe` and nothing really beats it at the task. Anything more than 10 lines and I find python easier to deal with (or at least it's my comfort zone).