tag:blogger.com,1999:blog-2590786243514585895.post4713148936183812062..comments2022-03-28T13:41:03.207+01:00Comments on Camltastic!: Tip: Read all lines from a file (the most common OCaml newbie question?)Richard Joneshttp://www.blogger.com/profile/08315526595922432607noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-2590786243514585895.post-90052412402735155882008-09-21T14:53:00.000+01:002008-09-21T14:53:00.000+01:00Let's keep playing :)With the same patch.open ...Let's keep playing :)<BR/><BR/>With the same patch.<BR/><BR/>open Enum<BR/>open IO<BR/>open File<BR/><BR/>with_file_in "file.txt" (<BR/> lines_of |- iter do_something<BR/>)<BR/><BR/>or, if you prefer<BR/><BR/>with_file_in "file.txt" (<BR/> iter do_something -| lines_of<BR/>)<BR/><BR/>or, if you prefer<BR/><BR/>lines_of |- iter do_something |><BR/> with_file_in "file.txt"<BR/><BR/>Although I grant you that this concision comes at the expense of readability, something which the Python version manages to avoid.<BR/><BR/>Of course, I can cheat a little further and use a nice little syntax extension of mine and end up with<BR/><BR/>with file_in "file.txt" as f<BR/> for line in lines_of f<BR/> do_something line<BR/><BR/>Which somehow reminds me of the Python version :)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2590786243514585895.post-16149181369493598612008-09-21T02:23:00.000+01:002008-09-21T02:23:00.000+01:00Oh sure, if you want to go that way...from __futur...Oh sure, if you want to go that way...<BR/><BR/>from __future__ import with_statement<BR/><BR/>with open('file.txt') as f:<BR/> for line in f:<BR/> dosomethingwith(line)Unknownhttps://www.blogger.com/profile/14797636125764350522noreply@blogger.comtag:blogger.com,1999:blog-2590786243514585895.post-67081561429248307672008-09-18T06:28:00.000+01:002008-09-18T06:28:00.000+01:00Actually, with the patch which I hope makes its wa...Actually, with the patch which I hope makes its way to ExtLib, the Python extract becomes<BR/><BR/>open Enum<BR/>open IO<BR/>open File<BR/><BR/>with_file_in "file.txt" (fun f -><BR/> iter (fun line -><BR/> dosomething line<BR/> ) (lines_of f)<BR/>)<BR/><BR/>The extract will also take care of closing the file at the end and/or in case of exception.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2590786243514585895.post-17474502072894624672008-09-17T03:01:00.000+01:002008-09-17T03:01:00.000+01:00>I'm going to say it now: programming fashions ...><I>I'm going to say it now: programming fashions are stupid and counterproductive. The only things that matter are that your program is short, easy to write, easy to maintain and works correctly. How you achieve this has nothing to do with programming fads.</I><BR/>Wisest thing I've heard(read) in a while :-)<BR/>Thank you for this meaningful post [^_^]Khrzphttps://www.blogger.com/profile/10716830769361615066noreply@blogger.comtag:blogger.com,1999:blog-2590786243514585895.post-85567170974816302612008-09-16T23:27:00.000+01:002008-09-16T23:27:00.000+01:00Actually, Python uses a for loop:f = open('fil...Actually, Python uses a for loop:<BR/><BR/>f = open('file.txt')<BR/>for line in f:<BR/> dosomethingwith(line)<BR/>f.close()<BR/><BR/>Or you can use f.readlines() if you're feeling wild and woolly.Unknownhttps://www.blogger.com/profile/14797636125764350522noreply@blogger.com