3 ms·
Using a generator I think this does what you want. What does the equivalent haskell code look like? def read_chunked(f, delim=',', size=1024): buf
by parfe 15y ago
Using a generator I think this does what you want. What does the equivalent haskell code look like?
def read_chunked(f, delim=',', size=1024):
buf = []
while True:
data = f.read(size)
if not data:
if buf:
yield ''.join(buf)
break
if delim in data:
spl = data.split(',')
buf.append(spl[0])
yield ''.join(buf)
for s in spl[1:-1]:
yield s
rest = spl[-1]
buf = [rest] if rest else []
else:
buf.append(data)
for word in read_chunked(open("really_big_file", "rb")):
#socket.send(word[::-1])
print word[::-1]
I'm pretty sure I got the gist of what you were after. What do you think?
- Peaker 15y agoWell, this is basically manually implementing Iteratee inline your solution. The idea is that Iteratees allow writing this as: main = run_ $ enumFile someFile $$ splitWhen isComma $$ map reverse $$ iterHandle stdout where isComma = (== ',') . chr . fromIntegral Basically Iteratees are a generalization of the technique you've implemented, but: * Generalized for any error type and support various modes of error handling/propagation (they can go up to the producer, or down towards the deeper consumer as error inputs). * Are generalized to many types of "buffers" * Have a high-performance counterparts of standard "list functions" * Are a standard base for many libraries to extend (http, xml, etc), and are faster, more powerful and more useful than standard strict IO libraries (Haskell's base libs, and Python's stdlib). So both Python and Haskell have standard "strict IO" libraries. Iteratees are really a new idea -- that Python does not do well yet. So I don't think Haskell's progression of the state of the art in IO handling is really evidence that "IO is unsolved in Haskell".
- parfe 15y agoThat is a nice feature. thanks.