4 ms·
> safeRead s = catch (readFile s) $ \_ -> return "" > Contrasting this with Java is left as an exercise for the reader. String safeRead(String fName) { t
by WilliamLP 17y ago
> safeRead s = catch (readFile s) $ \_ -> return ""
> Contrasting this with Java is left as an exercise for the reader.
String safeRead(String fName) {
try { return readFile(fName); }
catch (Exception e) { return ""; }
}
Just the same thing, except replace ASCII garbage with words? (It assumes I have a readFile function, but that's not such a terrible thing to have around...)
- shrughes 17y agoYeah, basically.
- jrockway 17y agoJava's standard library has no way to read a file with one function.
- WilliamLP 17y agoI am allowed to write my own function in Java. Having a basic file read in a static function library is more than reasonable. Should it be in the standard library? Yes, but it not being there isn't really a cost.
- zaphar 17y agoIt is a cost because you now have to carry around your static function with you wherever you code. Either you 1. have it in a utils library that adds to the external dependencies of all your java projects. or 2. you copy and paste it everywhere. Either way it's a cost however small that java has and other languages have. Additionally your example doesn't really demonstrate the same feature the haskell example did, which was a stream that haskell through laziness handles transparently for you. Your readfile static function breaks the stream because it forces you to read the entire file into memory before returning the contents. That was sort of the point and your counter example in java demonstrates it beautifully. An example that did what the haskell example did would require significantly more code.