Hacker News new | past | comments | ask | show | jobs | submit login
An upgrade to attoparsec, Haskell parsing library: more speed, more power (serpentine.com)
105 points by jcurbo on May 31, 2014 | hide | past | favorite | 11 comments



"When I was compiling the list of significant packages using attoparsec, I made a guess that the Unix rev would reverse the order of lines in a file. What it does instead seems much less useful: it reverses the bytes on each line."

  tac (1) - concatenate and print files in reverse


Unfortunately, tac isn't cross-platform. On systems with GNU userland, you can indeed use 'tac', but that isn't available on BSDish systems (including FreeBSD and Mac OS X), where 'tail -r' is what you want instead. And likewise, 'tail -r' doesn't work on systems with a GNU userland, because the tail in GNU coreutils doesn't implement -r (according to the maintainers, because reversing the order of lines in a file is completely out of scope for what 'tail' is supposed to do). I don't believe anything in POSIX will do this cross-platform in a straightforward way, because neither 'tac' nor the '-r' option of tail are in POSIX. That leaves you with the options: platform-testing if/thens, or rolling your own in an awk/etc. one-liner.


ghc -e 'interact $ unlines . map reverse . lines'


Or, on any *nix worth talking about, by default...

    perl -e 'print reverse <>'


Isn't that `rev`, not `tac`? (I think `tac` should be `unlines . reverse . lines`.)


You are correct.

For those not familiar with Haskell,

    unlines . map reverse . lines
is the same thing as (python)

    lambda data : [line[::-1] for line in data]
whereas

    unlines . reverse. lines
is the same as

    lambda data : [line for line in data][::-1]


Well, you need

   '\n'.join(...)
for the "unlines" bit.


I can not say enough about util-linux/coreutils. If you are a linux user you really should take an hour or so to go through the man pages of any of the programs that you are not aware of. The only tool I have never been able to use is ptx. (ptx is permutated indexes: legacy doc index creation[1])

`rev` is an great tool for processing text files from the command line. It is especially useful when you need a quick and easy way to grab the last column of a delimited file when the lines do no have the same number of columns:

  $ cat somefile | rev | cut -f 1 -d DELIMITER | rev
[1]: http://unix.stackexchange.com/questions/92730/usr-bin-ptx-ca...


Worth noting, this post was accidentally published. This release may still be baking.


Nope, I pushed the release this morning once I realised that someone had actually noticed the post during the five minutes it was live at 1am.


Ah, good to know!




Consider applying for YC's Spring batch! Applications are open till Feb 11.

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

Search: