def parseLog(file):
list = []
for line in file:
list.append(parseEntry(line))
return list
Or better (more pythonic imho):
def parseLog(file):
for line in file:
yield parseEntry(line)
The second version is lazy (if file reading is lazy), so you can even abort parsing or fix the state on an error. Here are your different versions all reusing parseLog from above and equivalent line count to condition handling:
def parseLogLoudly(file):
for entry in parseLog(file):
if entry.failed:
print "Warning:", entry.message
else
yield entry
def parseLogSilently(file):
for entry in parseLog(file):
if not entry.failed:
yield entry
def parseLogInteractively(file):
for entry in parseLog(file):
if entry.failed:
yield askToFixEntry(entry)
else
yield entry
(Haskell's Either has "Left a" or "Right b". I want something like "Just value" or "Error msg", but you are right Either is nearer than Maybe)
Haskell's Either type is conventionally used exactly like that--pretend that Right val is Just val and Left err is Error err.
The reason it doesn't have those names is because it is more general; you can also use Either to model things like early termination. The generic names just make it clearer that it isn't exclusively for error handling unlike exceptions. In other languages using exceptions for more generic control flow is considered poor style, but in Haskell using Either for non-error cases is completely reasonable.