Re: [racket-users] Example of file i/o?
Since newbies are always listening, I'll just mention something a lot of people here (including Greg) already know. :) Something like `file->lines` is a handy convenience, when you know the file size won't break your system, and/or you're not writing reusable or long-lived code. Good practice for important and reusable code often means doing this serial file processing as iteration (or folds) over the input port, and processing as you go, rather than sucking an entire file into memory first. Not all line-based file-processing tasks are serial like this, but I'd guess that most are (or, even if you have to do some non-serial processing, it can usually be done in a more space-efficient way than representing the entire file in memory). It's good to get comfortable with this. You don't see it only in simple line-oriented file processing, but that's a good playground for learning. Many software frameworks at the moment obscure computational time and space factors (and often hide massive loads of crud piled atop crud atop crud, especially in in client-side Web frameworks). But your Racket programs are one environment in which you can often still have a pretty good idea of what's going on, and grow as a programmer. If any newbies are not convinced, I'll just say "big data" here, as one buzzwordy example. :) When, for example, we suddenly need to invent a way to decompose big data processing to use specialized compute clusters, that's a job for someone who can understand things, not someone who spent all their time only learning how to cargo-cult their way through someone else's crappy Web frameworks. :) -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [racket-users] Example of file i/o?
That's exactly what I was trying to do - thanks! On Wed, Jul 19, 2017 at 12:26 PM, Greg Hendershott < greghendersh...@gmail.com> wrote: > If you frequently want to deal with files as a list of lines, you > could wrap the code Ben showed you in a function: > > (define (file->lines file) > (with-input-from-file file > (λ () > (for/list ([line (in-lines)]) > line > > As it happens, Racket already defines this function for you: > > https://docs.racket-lang.org/reference/Filesystem.html#% > 28def._%28%28lib._racket%2Ffile..rkt%29._file-~3elines%29%29 > -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [racket-users] Example of file i/o?
If you frequently want to deal with files as a list of lines, you could wrap the code Ben showed you in a function: (define (file->lines file) (with-input-from-file file (λ () (for/list ([line (in-lines)]) line As it happens, Racket already defines this function for you: https://docs.racket-lang.org/reference/Filesystem.html#%28def._%28%28lib._racket%2Ffile..rkt%29._file-~3elines%29%29 -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [racket-users] Example of file i/o?
Very good, thank you! On Tuesday, July 18, 2017 at 11:54:14 AM UTC-5, Ben Greenman wrote: > (with-input-from-file "file.txt" > (lambda () > (for ((line (in-lines))) > ))) > > > On Tue, Jul 18, 2017 at 12:46 PM, Leithwrote: > Basic question, but I can't seem to find a good answer. What is an example > of "idiomatic" file I/O using racket? Like, for example, open a file for > reading and do something with every line in the file? > > > > The example here: https://docs.racket-lang.org/guide/io-patterns.html says > "If you want to process individual lines of a file" but then it doesn't open > a file at all. It uses open-input-string. I'm guessing it would be > sufficient to use open-input-file instead? Also, what is the right way to > read a line at a time until EOF? > > > > Thanks! > > > > -- > > You received this message because you are subscribed to the Google Groups > "Racket Users" group. > > To unsubscribe from this group and stop receiving emails from it, send an > email to racket-users...@googlegroups.com. > > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [racket-users] Example of file i/o?
(with-input-from-file "file.txt" (lambda () (for ((line (in-lines))) ))) On Tue, Jul 18, 2017 at 12:46 PM, Leithwrote: > Basic question, but I can't seem to find a good answer. What is an > example of "idiomatic" file I/O using racket? Like, for example, open a > file for reading and do something with every line in the file? > > The example here: https://docs.racket-lang.org/guide/io-patterns.html > says "If you want to process individual lines of a file" but then it > doesn't open a file at all. It uses open-input-string. I'm guessing it > would be sufficient to use open-input-file instead? Also, what is the > right way to read a line at a time until EOF? > > Thanks! > > -- > You received this message because you are subscribed to the Google Groups > "Racket Users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to racket-users+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[racket-users] Example of file i/o?
Basic question, but I can't seem to find a good answer. What is an example of "idiomatic" file I/O using racket? Like, for example, open a file for reading and do something with every line in the file? The example here: https://docs.racket-lang.org/guide/io-patterns.html says "If you want to process individual lines of a file" but then it doesn't open a file at all. It uses open-input-string. I'm guessing it would be sufficient to use open-input-file instead? Also, what is the right way to read a line at a time until EOF? Thanks! -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.