3 ms·
Your shell example beef.txt | grep "lasagna" | sort -n | uniq is a lot more compositional than your Python-style example: search(for='lasagna', in='b
by eru 4y ago
Your shell example
beef.txt | grep "lasagna" | sort -n | uniq
is a lot more compositional than your Python-style example:
search(for='lasagna', in='beef.txt', clear_duplicates=False).sorted()
The former uses a few general purpose functions, the latter seems to use bigger building blocks that aren't as universal.
You could more faithfully mirror the bash example in Haskell, if you wanted to, if you wrote the necessary library functions:
cat "beef.txt" | grep "lasagna" | sortBy numeric | uniq
This would be actual valid Haskell syntax.
- marcosdumay 4y ago> Your shell example ... is a lot more compositional than your Python-style example Yes, the Python equivalent would be: lines("beef.txt").filter(lambda(x): x.contains("lasagna")).sort(type="natural").remove_duplicates() Of course, all of those things would need to be generators. Python accepts that kind of code, but most "Python developers" wouldn't think on it. Haskell is a much more natural fit for replacing Bash. (By the way, Haskell has at least one Bash replacement library that I remember, but it's hard to find).
- dllthomas 4y agoNit, but a single | can't be an operator in Haskell.
- eru 4y agoTrue, I got that piece of the syntax wrong. I guess you can do something very close, though.
- dllthomas 4y agoAbsolutely. Almost every group of symbols can be used as an operator, just (as it happens) not a single | because that is reserved for patterns.
- nequo 4y agoHaskell has `&` in Data.Function. You can try it in ghci: Prelude> f a x = a * x Prelude> g x = x + 2 Prelude> g $ f 3 $ 5 17 Prelude> :m +Data.Function Prelude Data.Function> 5 & f 3 & g 17 It is like `$` but with the arguments flipped: ($) :: (a -> b) -> a -> b (&) :: a -> (a -> b) -> b