3 ms·
Awk in Haskell
- snogglethorpe 13y agoI was a bit confused by the title. I thought it was going to be an implementation of AWK written in Haskell, but it's actually an AWK-like text-processing tool that uses Haskell as the language. Not sure whether that's better or worse than traditional AWK. Haskell's certainly a well-respected language, but I found the examples in the README kind of confusing. I don't know much about Haskell in detail, so maybe that's just me, but it does suggest that this lacks some of AWK's readability for the uninitiated....
- emmelaich 13y agoI was also expecting something more Awk-ish than Haskell-ish, but this is still really cool.
- tel 13y agoI felt all of the examples read extremely easily, but I'm a frequent Haskell user. I think that's the primary audience.
- ssadler 13y agoAuthor here. You're right, "Awk in Haskell" is perhaps a bit misleading. The intention is not to replace Awk for the masses, but mainly to provide an Awk-like tool for programers who are more familiar with Haskell. Similar to pyline: http://code.activestate.com/recipes/437932-pyline-a-grep-like-sed-like-command-line-tool/ http://code.activestate.com/recipes/437932-pyline-a-grep-lik....
- tomfitz 13y agoRelatedly, I have an incomplete Haskell port of sed: https://github.com/tomfitzhenry/hs-sed https://github.com/tomfitzhenry/hs-sed It parses a sed script into an AST, then interprets that AST. sed is simple enough that such an interpretor is easy to write. e.g. each input line is only processed once, and in order Maybe one of the more interesting parts is the testing of the parser using specification-based testing. First, define how to generate an arbitrary sed script (in its AST form). Haskell's QuickCheck will then generate 100 random sed scripts, and check that upon being pretty printed (to a sed script in its usual textual form), and then parsed, produces the original AST. Specification-based testing is a nice complement to unit-based testing, that I'm beginning to rely more on, to avoid the tedium of writing so many unit tests. The parser does not yet support nested sed expressions that contain more than one subexpression. The qualifier "suchThat (\xs -> length xs == 1)" at https://github.com/tomfitzhenry/hs-sed/blob/master/tests/PrettyPrinterTest.hs#L26 https://github.com/tomfitzhenry/hs-sed/blob/master/tests/Pre... documents this, and as a side-effect serves as a todo list [which I have since ignored for months :)].
- tomfitz 13y agoAlso, the introduction of a 'Sed' monad was a lightbulb moment for me during this project: https://github.com/tomfitzhenry/hs-sed/commit/0eb7797439e54ab69a31a041f68aefbca22f1ced https://github.com/tomfitzhenry/hs-sed/commit/0eb7797439e54a... In particular, the changes in Execute.hs are where using a monad made this code more clear. If you're new to the usefulness of monads, I don't suggest you read this code, however, and that instead you read: 1. "You Could Have Invented Monads!" http://blog.sigfpe.com/2006/08/you-could-have-invented-monads-and.html http://blog.sigfpe.com/2006/08/you-could-have-invented-monad... 2. "Monad Transformers Step by Step" http://www.cs.virginia.edu/~wh5a/personal/Transformers.pdf http://www.cs.virginia.edu/~wh5a/personal/Transformers.pdf
- dabei 13y agoLooks like a good way to practice Haskell. I've been learning about the language but haven't yet found ways to make use of it in my day job.
- gelisam 13y agoWhile I am flattered that somebody would post one of my repos on Hacker News, any particular reason why the link is to my fork and not to the original repo? ssadler deserves some credit :)
- helloTree 13y agoOr just use ghc -e, e.g. some useful scripts for interact: #!/bin/bash ghc -e "interact ($1)" line processing: #!/bin/bash ghc -e "interact (unlines . map ($1) . lines)"